繁体   English   中英

如何在 react-native 中将 resource-id 添加到 android

[英]How to add resource-id to android in react-native

我试图使用 firebase TestLab,但它似乎只能针对资源 ID。 我有这样的元素:

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

但似乎 testID 没有映射到 resource-id。 如果这不是这样做的方法,那么我怎样才能在 react-native 中访问 resource-id? 如果我不能在 android studio 中添加 resource-id 的工作是什么?

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

以前使用accessibleLabel的解决方法不应该继续使用,因为它实际上破坏了可访问性。 可以在此处找到此更改的进一步理由。

在您的视图上同时使用accessibleaccessibleLabel (到目前为止,似乎适用于 View、Text、TextInput),UiAutomator 将从accessibleLabel生成content-desc字段。

现在,我们可以使用 content-desc 来识别标签。

可访问性标签上的 DOC: 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>

尝试构建调试。 这里提出了这个解决方案

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

#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`

我尝试在“FloatingTextInput”下添加accessibleLabel和accessible下面的testID,它是占位符。 还在“FloatingTextInput”组件下添加了道具。 但是它仍然没有反映在“ //android.widget.TextView ”类xpath的内容描述中。 它只适用于“ //android.widget.EditText ”吗? 如果是,那么如果占位符具有//android.widget.TextView类,那么如何使用 xpath?

请找到您参考的以下代码片段。 我尝试了两种方法。 一个带有视图,第二个带有 FloatingtextInput

  1. 使用视图组件:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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