【问题标题】:Automation testing react native app with appium自动化测试将本机应用程序与 appium 反应
【发布时间】: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


    【解决方案1】:

    您是否使用其他工具验证 ElementID 进行确认?例如Android UIautomatorviewer 到 Appium Desktop Inspector? 这是两者的有用示例。 http://www.automationtestinghub.com/appium-inspector/ http://www.automationtestinghub.com/appium-desktop-inspector-inspect-mobile-elements/

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 2018-01-15
      • 2020-12-07
      • 1970-01-01
      • 2017-10-18
      • 2019-12-07
      • 2017-12-26
      • 2015-11-15
      • 1970-01-01
      相关资源
      最近更新 更多