Skip to content

Commit 381fb39

Browse files
jdefffacebook-github-bot
authored andcommitted
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 #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 <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Android] [Fixed] - Fixes #7135, #9942, and #16137. Display the `testID` as the `resource-id` for black-box testing frameworks Pull Request resolved: #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. <img width="912" alt="Screen Shot 2020-08-10 at 3 38 27 PM" src="https://user-images.githubusercontent.com/875498/89838534-55044100-db20-11ea-9be2-ba507a81f6fb.png"> <img width="1096" alt="Screen Shot 2020-08-10 at 3 40 41 PM" src="https://user-images.githubusercontent.com/875498/89838542-5897c800-db20-11ea-9895-462c6fea1130.png"> Reviewed By: JoshuaGross Differential Revision: D25799550 Pulled By: fkgozali fbshipit-source-id: e64ff1b90fb66b427fce7af533aa94792cfbcad3
1 parent dc9132e commit 381fb39

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo
246246
}
247247
}
248248
}
249+
250+
// Expose the testID prop as the resource-id name of the view. Black-box E2E/UI testing
251+
// frameworks, which interact with the UI through the accessibility framework, do not have
252+
// access to view tags. This allows developers/testers to avoid polluting the
253+
// content-description with test identifiers.
254+
final String testId = (String) host.getTag(R.id.react_test_id);
255+
if (testId != null) {
256+
info.setViewIdResourceName(testId);
257+
}
249258
}
250259

251260
@Override
@@ -425,7 +434,8 @@ public static void setDelegate(final View view) {
425434
if (!ViewCompat.hasAccessibilityDelegate(view)
426435
&& (view.getTag(R.id.accessibility_role) != null
427436
|| view.getTag(R.id.accessibility_state) != null
428-
|| view.getTag(R.id.accessibility_actions) != null)) {
437+
|| view.getTag(R.id.accessibility_actions) != null
438+
|| view.getTag(R.id.react_test_id) != null)) {
429439
ViewCompat.setAccessibilityDelegate(view, new ReactAccessibilityDelegate());
430440
}
431441
}

0 commit comments

Comments
 (0)