React Native + Appium iOS elementByAccesibilityId.text() [英] React Native + Appium iOS elementByAccesibilityId.text()
问题描述
我正在使用 Appium + Jest 为 Android 和 iOS 编写 React Native 测试.
I'm writing tests for React Native using Appium + Jest for both Android and iOS.
这是我的 React Native 元素:
Here's my React Native element:
<Text accessibilityLabel={'emailError'}>Invalid email</Text>
这是我为提取 React Native Text
元素的值而创建的 Promise 链:
Here's the Promise chain I created to extract the value of a React Native Text
element:
driver.elementByAccessibilityId('emailError')
.then(error => error.text())
.then(errorText => expect(errorText).toBe('Invalid email'))
在 Android 上,此测试通过.在 iOS 上,此测试失败,并且 errorText === 'emailError'
.
On Android, this test passes. On iOS, this test fails, and errorText === 'emailError'
.
这是为什么,是否有跨平台提取文本的解决方案?
Why is this, and is there a solution that works cross-platform for extracting text?
推荐答案
我遇到了同样的问题.在 iOS 上使用 testID
而不是 accessibilityLabel
对我有用.对于 Android,accessible: true
和 accessibilityLabel
应该可以解决问题.但是:你不能只设置它们.如果 accessibilityLabel
和 testID
都设置了,它将不再适用于 iOS.
I had the same issue. Using testID
instead of accessibilityLabel
worked for me on iOS. For Android, accessible: true
and accessibilityLabel
should do the trick. But: You can't just set them all. If accessibilityLabel
and testID
are both set, it won't work on iOS anymore.
iOS 示例:
<Text testID={'emailError'}>Invalid email</Text>
Android 示例:
Example for Android:
<Text accessible={true} accessibilityLabel={'emailError'}>Invalid email</Text>
像 https://github.com/Tipsi/react-native-testid 可以帮你根据平台设置正确的属性.
A library (or just create your own function) like https://github.com/tipsi/react-native-testid can help you with setting the correct attributes based on the platform.
这篇关于React Native + Appium iOS elementByAccesibilityId.text()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!