简体   繁体   中英

How to add resource-id to android in react-native

I was trying to use firebase TestLab but it seems that it can only target resource-id. I had my elements like this:

<Input {...props} testID="usernameIput" />

But it seems that the testID is not mapped to the resource-id. If this is not the way to do it, then how can I get access to resource-id within react-native? If I can't what is the work around to add resource-id in android studio?

Came to this nearly 4 years late but as of React Native 0.64 the testId attribute is now mapped to resource-id in Android builds which means Android testing frameworks that rely on this attribute will operate correctly.

The former workaround that used the accessibleLabel shouldn't be used going forward as it actually mangled accessibility. A further rationale for this change can be found here .

Use both accessible and accessibleLabel on your views (so far, seems to work on View, Text, TextInput) and UiAutomator will generate content-desc field from accessibleLabel .

For now, we can use content-desc to identify tags.

DOC on accessiblity label: https://facebook.github.io/react-native/docs/accessibility.html#accessibilitylabel-ios-android .

<Text
    testID="btnText"   //works for iOS
    accessibilityLabel="btnText"    //works for android & iOS content-description
    accessible
  >
    {text}
  </Text>

Try building for debug. This solution was proposed here

You can build for debug using the answer here :

#React-Native 0.49.0+
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug

#React-Native 0-0.49.0
react-native bundle --dev false --platform android --entry-file index.android.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug

Then to build the APK's after bundling:

$ cd android
#Create debug build:
$ ./gradlew assembleDebug
#Create release build:
$ ./gradlew assembleRelease #Generated `apk` will be located at `android/app/build/outputs/apk`

I have tried adding the accessibleLabel and accessible below testID under "FloatingTextInput" which is placeholder . Also added the props under the "FloatingTextInput" component. But still it is not getting reflected in content-desc for " //android.widget.TextView " class xpath. Does it only work for " //android.widget.EditText "? if yes, then how to use xpath if the placeholder has //android.widget.TextView class?

Please find the below code snippet of your reference. I tried two ways. One with view and second with FloatingtextInput

  1. With view component:

     <View style={customStyle.textInputContainer} testID="NameYourGoal" accessibleLabel="NameYourGoal" accessible>
  2. With FloatingTextInput :

     <FloatingTextInput testID="NameYourGoal" accessibleLabel="NameYourGoal" accessible />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM