[英]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.我试图使用 firebase TestLab,但它似乎只能针对资源 ID。 I had my elements like this:我有这样的元素:
<Input {...props} testID="usernameIput" />
But it seems that the testID is not mapped to the resource-id.但似乎 testID 没有映射到 resource-id。 If this is not the way to do it, then how can I get access to resource-id within react-native?如果这不是这样做的方法,那么我怎样才能在 react-native 中访问 resource-id? If I can't what is the work around to add resource-id in android studio?如果我不能在 android studio 中添加 resource-id 的工作是什么?
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.晚了将近 4 年,但从 React Native 0.64 开始, testId
属性现在映射到 Android 构建中的resource-id
,这意味着依赖此属性的 Android 测试框架将正常运行。
The former workaround that used the accessibleLabel
shouldn't be used going forward as it actually mangled accessibility.以前使用accessibleLabel
的解决方法不应该继续使用,因为它实际上破坏了可访问性。 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
.在您的视图上同时使用accessible
和accessibleLabel
(到目前为止,似乎适用于 View、Text、TextInput),UiAutomator 将从accessibleLabel
生成content-desc
字段。
For now, we can use content-desc to identify tags.现在,我们可以使用 content-desc 来识别标签。
DOC on accessiblity label: https://facebook.github.io/react-native/docs/accessibility.html#accessibilitylabel-ios-android .可访问性标签上的 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>
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:然后在捆绑后构建 APK:
$ 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 .我尝试在“FloatingTextInput”下添加accessibleLabel和accessible下面的testID,它是占位符。 Also added the props under the "FloatingTextInput" component.还在“FloatingTextInput”组件下添加了道具。 But still it is not getting reflected in content-desc for " //android.widget.TextView " class xpath.但是它仍然没有反映在“ //android.widget.TextView ”类xpath的内容描述中。 Does it only work for " //android.widget.EditText "?它只适用于“ //android.widget.EditText ”吗? if yes, then how to use xpath if the placeholder has //android.widget.TextView class?如果是,那么如果占位符具有//android.widget.TextView类,那么如何使用 xpath?
Please find the below code snippet of your reference.请找到您参考的以下代码片段。 I tried two ways.我尝试了两种方法。 One with view and second with FloatingtextInput一个带有视图,第二个带有 FloatingtextInput
With view component:使用视图组件:
<View style={customStyle.textInputContainer} testID="NameYourGoal" accessibleLabel="NameYourGoal" accessible>
With FloatingTextInput :使用 FloatingTextInput :
<FloatingTextInput testID="NameYourGoal" accessibleLabel="NameYourGoal" accessible />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.