如何在React Native中为Android添加资源ID

15

我想使用Firebase TestLab,但似乎它只能以资源ID为目标。我的元素是这样的:

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

但似乎testID没有映射到resource-id。如果这不是实现的方法,那么我怎样才能在react-native中访问resource-id?如果不能,有什么绕过方式可以在Android Studio中添加resource-id吗?


社区已经讨论过这个问题,最终的解决方案是使用“content-desc”作为关键字。相关问题请参见此处讨论请参见此处 - Oboo Cheng
1
Jason,你找到了如何设置资源ID的解决方案吗? - Kholiavko
@Kholiavko,我发现你可以在Android Studio中打开项目,并像React Native从未存在一样添加它。这不会破坏你的React Native构建,这是好的一面,但这非常耗时。我不建议这样做。最终,我使用了带有良好测试框架的Ignite框架。 - Tianhao Zhou
3个回答

5

虽然我来晚了将近4年,但从React Native 0.64版本开始,testId属性现在映射到Android构建中的resource-id,这意味着依赖于此属性的Android测试框架将正确运行。

以前使用的accessibleLabel的解决方法不应该继续使用,因为它会破坏可访问性。更多关于此更改的理由可以在此处找到


1
哦,终于到了重构的时候了。 - Tianhao Zhou
看起来在所有组件中使用 testId 还有很长的路要走。 - alwaysday1

4

在视图上同时使用accessibleaccessibleLabel(目前来看,这对View、Text和TextInput有效),UiAutomator将从accessibleLabel生成content-desc字段。

目前,我们可以使用content-desc来标识标签。

有关可访问性标签的文档: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>

0

尝试进行调试构建。此解决方案是在这里提出的。

您可以使用此处的答案进行调试构建:

#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

然后在打包后构建APK:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接