From 381fb395ad9d2d48717a5d082aaedbecdd804554 Mon Sep 17 00:00:00 2001 From: Jayme Deffenbaugh Date: Tue, 5 Jan 2021 23:20:56 -0800 Subject: [PATCH] Expose the testID to black-box testing frameworks on Android (#29610) Summary: There has been a long-standing issue where black-box testing frameworks like Appium and Xamarin UITest have not been able to access the `testID` view prop for Android (see https://github.com/facebook/react-native/issues/7135). A natural place for this to be exposed is via a view's `resource-id`. The `resource-id` is what I have used when working on UIAutomator-based tests for native Android apps and is a non-localized, development-only identifier. As mentioned in the linked ticket, you can dynamically set the resource-id using the view's AccessibilityNodeInfo. This change simply checks to see if a testID is provided for a view and then exposes it through the view's accessibility node delegate. ## Changelog [Android] [Fixed] - Fixes https://github.com/facebook/react-native/issues/7135, https://github.com/facebook/react-native/issues/9942, and https://github.com/facebook/react-native/issues/16137. Display the `testID` as the `resource-id` for black-box testing frameworks Pull Request resolved: https://github.com/facebook/react-native/pull/29610 Test Plan: I used the `uiautomatorviewer` tool to verify that the resource-id is populated with the `testID` of a few different views of the RNTester app. Screen Shot 2020-08-10 at 3 38 27 PM Screen Shot 2020-08-10 at 3 40 41 PM Reviewed By: JoshuaGross Differential Revision: D25799550 Pulled By: fkgozali fbshipit-source-id: e64ff1b90fb66b427fce7af533aa94792cfbcad3 --- .../react/uimanager/ReactAccessibilityDelegate.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java index 22b82bed0ca..f371e341f17 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java @@ -246,6 +246,15 @@ public class ReactAccessibilityDelegate extends AccessibilityDelegateCompat { } } } + + // Expose the testID prop as the resource-id name of the view. Black-box E2E/UI testing + // frameworks, which interact with the UI through the accessibility framework, do not have + // access to view tags. This allows developers/testers to avoid polluting the + // content-description with test identifiers. + final String testId = (String) host.getTag(R.id.react_test_id); + if (testId != null) { + info.setViewIdResourceName(testId); + } } @Override @@ -425,7 +434,8 @@ public class ReactAccessibilityDelegate extends AccessibilityDelegateCompat { if (!ViewCompat.hasAccessibilityDelegate(view) && (view.getTag(R.id.accessibility_role) != null || view.getTag(R.id.accessibility_state) != null - || view.getTag(R.id.accessibility_actions) != null)) { + || view.getTag(R.id.accessibility_actions) != null + || view.getTag(R.id.react_test_id) != null)) { ViewCompat.setAccessibilityDelegate(view, new ReactAccessibilityDelegate()); } }