Skip to content

Commit

Permalink
Expose the testID to black-box testing frameworks on Android (#29610)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jdeff authored and facebook-github-bot committed Jan 6, 2021
1 parent dc9132e commit 381fb39
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo
}
}
}

// 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
Expand Down Expand Up @@ -425,7 +434,8 @@ public static void setDelegate(final View view) {
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());
}
}
Expand Down

3 comments on commit 381fb39

@sasmit
Copy link

@sasmit sasmit commented on 381fb39 Apr 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdeff
I am on react-native 0.62. So I took your changes locally to node_modules (path:ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java ) and tried to test my application with UI Automator view and unable to get the vlaues for resource-id. Its always empty.

Am I missing something here ?

@jdeff
Copy link
Contributor Author

@jdeff jdeff commented on 381fb39 Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdeff
I am on react-native 0.62. So I took your changes locally to node_modules (path:ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java ) and tried to test my application with UI Automator view and unable to get the vlaues for resource-id. Its always empty.

Am I missing something here ?

@sasmit I don't believe that changing the local java files in node_modules will work. See #29610 (comment) for more information.

@sasmit
Copy link

@sasmit sasmit commented on 381fb39 Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdeff I tried all the steps mentioned in https://github.com/facebook/react-native/wiki/Building-from-source . but still there is some error while building in android studio. Below is the error

Execution failed for task ':ReactAndroid:buildReactNdkLib'.

Process 'command 'C:\Users\das\Downloads\android-ndk-r20b-windows-x86_64\android-ndk-r20b\ndk-build.cmd'' finished with non-zero exit value 2

This is the result of react-native info
info Fetching system and libraries information...
System:
OS: Windows 10 10.0.19042
CPU: (8) x64 Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
Memory: 3.70 GB / 15.82 GB
Binaries:
Node: 12.10.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.10.3 - C:\Program Files\nodejs\npm.CMD
Watchman: Not Found
SDKs:
Android SDK:
API Levels: 28, 29, 30
Build Tools: 28.0.3, 29.0.0, 29.0.2, 30.0.2
System Images: android-28 | Android TV Intel x86 Atom, android-30 | Google APIs Intel x86 Atom
Android NDK: 20.1.5948944
Windows SDK: Not Found
IDEs:
Android Studio: Version 4.1.0.0 AI-201.8743.12.41.6858069
Visual Studio: Not Found
Languages:
Java: 1.8.0_211
Python: 3.9.0
npmPackages:
@react-native-community/cli: Not Found
react: 16.13.1 => 16.13.1
react-native: file:../../fork/react-native => 0.62.2
react-native-windows: Not Found
npmGlobalPackages:
react-native: Not Found

Still anything wrong I am doing , I am new to react-native though

Please sign in to comment.