我正在使用 Appium + Jest 为 Android 和 iOS 编写 React Native 测试。
这是我的 React Native 元素:
<Text accessibilityLabel={'emailError'}>Invalid email</Text>
这是我为提取 React Native Text
元素的值而创建的 Promise 链:
driver.elementByAccessibilityId('emailError')
.then(error => error.text())
.then(errorText => expect(errorText).toBe('Invalid email'))
在 Android 上,此测试通过。在 iOS 上,此测试失败,并且 errorText === 'emailError'
。
这是为什么,是否有跨平台的解决方案来提取文本?
最佳答案
我遇到了同样的问题。使用 testID
而不是 accessibilityLabel
在 iOS 上对我有用。对于 Android,accessible: true
和 accessibilityLabel
应该可以解决问题。但是:你不能只设置它们。如果同时设置了 accessibilityLabel
和 testID
,它将不再适用于 iOS。
iOS 示例:
<Text testID={'emailError'}>Invalid email</Text>
Android 示例:
<Text accessible={true} accessibilityLabel={'emailError'}>Invalid email</Text>
像https://github.com/tipsi/react-native-testid 这样的库(或创建您自己的函数)可以帮助您根据平台设置正确的属性。
关于reactjs - React Native + Appium iOS elementByAccessibilityId.text() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642256/