在react-native中利用appium进行自动化集成测试

本篇文章简单介绍如何在react-native中使用appium进行e2e自动化测试。

#Appium与WebDriver

Appium是一个用来对移动设备App进行集成测试的工具。
WebDriver是用任意语言模拟浏览器行为的标准,主要用在Selenium系列的工具,用来对web项目进行集成测试。

#ReactNative中的实现

Appium可以用 uiautomator 进行页面元素的查找和操作,用react-native写的Android页面,为了方便查找可以设置accessibilityLabel.
如下的一个Component实现:

1
2
3
4
5
6
7
8
9
<TouchableNativeFeedback
background={TouchableNativeFeedback.Ripple("red", true)}
accessibilityLabel="clickZone"
onPress={counter}
>
<View style={DynamicStyle.AccessibilityLabel.clickZone}>
<Text>Some Text</Text>
</View>
</TouchableNativeFeedback>

其中给一个可点击的TouchableNativeFeedback设置了accessibilityLabel="clickZone".

  • Appium脚本的实现
    在Appium脚本中可以进行View的查找和点击:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
desired_caps = {
'platformName': 'Android',
'deviceName': 'Meizu MX5',
'appPackage': 'com.androidcode',
'appActivity': '.MainActivity',
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

sleep(5)
try:
## 用accessibilityLabel定位元素
el = driver.find_element_by_accessibility_id('clickZone')
assert el
## 模拟点击3下
map(lambda x: el.click(), range(0, 3))

## 用Text属性定位元素
els = driver.find_elements_by_android_uiautomator('new UiSelector().textContains("Some")')
assert els
logging.warning(els[0].text)
except Exception, e:
logging.error(e, exc_info=True)

driver.quit()