React Native + Appium iOS elementByAccessibilityId.text()

dav*_*aya 3 appium reactjs jestjs e2e-testing react-native

我正在使用 Appium + Jest 为 Android 和 iOS 编写 React Native 测试。

这是我的 React Native 元素:

<Text accessibilityLabel={'emailError'}>Invalid email</Text>
Run Code Online (Sandbox Code Playgroud)

这是我创建的用于提取 React Native 元素的值的 Promise 链Text

driver.elementByAccessibilityId('emailError')
      .then(error => error.text())
      .then(errorText => expect(errorText).toBe('Invalid email'))
Run Code Online (Sandbox Code Playgroud)

在 Android 上,此测试通过。在 iOS 上,此测试失败,并且errorText === 'emailError'.

为什么会这样?是否有跨平台的解决方案来提取文本?

小智 5

我遇到过同样的问题。在 iOS 上使用testID而不是accessibilityLabel对我有用。对于 Android 来说,accessible: true应该accessibilityLabel可以解决问题。但是:您不能全部设置。如果accessibilityLabeltestID都设置了,它将不再在 iOS 上运行。

iOS 示例:

<Text testID={'emailError'}>Invalid email</Text>
Run Code Online (Sandbox Code Playgroud)

安卓示例:

<Text accessible={true} accessibilityLabel={'emailError'}>Invalid email</Text>
Run Code Online (Sandbox Code Playgroud)

像https://github.com/tipsi/react-native-testid这样的库(或者只是创建您自己的函数)可以帮助您根据平台设置正确的属性。