【发布时间】:2020-06-26 16:42:59
【问题描述】:
我有两个屏幕,登录屏幕和主屏幕,登录屏幕将首先加载。 所以我想在主屏幕中测试元素按钮,但 appium 总是呈现第一个屏幕(登录屏幕)。它会返回错误,因为 appium 无法读取主屏幕中的元素。
这是我的登录屏幕
import React, { Component } from 'react'
import { View, TouchableOpacity, Text, TextInput } from 'react-native'
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
email: ''
}
}
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1, width: '50%', marginBottom: 10 }}
onChangeText={email => this.setState({ email })}
value={this.state.email}
placeholder={'Email'}
accessible={true}
accessibilityLabel={'fieldEmail'}
/>
<TouchableOpacity
accessibilityLabel='buttonLogin'
style={{ width: 100, height: 50, backgroundColor: 'blue', justifyContent: 'center', alignItems: 'center', borderRadius: 5 }}
onPress={() => this.props.navigation.navigate('Home')}>
<Text style={{ color: 'white' }}>Submit</Text>
</TouchableOpacity>
</View>
)
}
}
这是我的主屏幕
import React, { Component } from 'react'
import { View, TouchableOpacity, Text, Alert } from 'react-native'
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
email: ''
}
}
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
accessibilityLabel='buttonHome'
style={{ width: 100, height: 50, backgroundColor: 'blue', justifyContent: 'center', alignItems: 'center', borderRadius: 5 }}
onPress={() => Alert.alert('Notification', 'Welcome home.')}>
<Text style={{ color: 'white' }}>Submit</Text>
</TouchableOpacity>
</View>
)
}
}
这是我的测试文件
/* eslint-disable no-undef */
import wd from 'wd'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000
const PORT = 4723
const config = {
platformName: 'Android',
deviceName: 'Pixel 3a XL API 29',
app: '/Users/gandahalojasa/Documents/Project/Learn/Appium_React_Native/android/app/build/outputs/apk/debug/app-debug.apk'
}
const driver = wd.promiseChainRemote('localhost', PORT)
beforeAll(async () => {
await driver.init(config)
await driver.sleep(4000)
}) // Sometime for the app to load
test('login screen test', async () => {
expect(await driver.hasElementByAccessibilityId('fieldEmail')).toBe(true)
await driver.elementByAccessibilityId('fieldEmail').type('gandarainpanjaitan@gmail.com')
expect(await driver.hasElementByAccessibilityId('buttonLogin')).toBe(true)
const element = await driver.elementByAccessibilityId('buttonLogin')
await element.click()
})
【问题讨论】:
标签: react-native automated-tests appium