简介
appium 通过使用--nodeconfig
服务器参数,可以将appium服务器注册到本地Selenium grid (setup docs),同时在不同设备的浏览器、操作系统和机器上运行测试。最大程度用于兼容性测试。关于selenium grid的使用,参考上一篇 selenium grid。
前置条件
- 已安装配置appium(点击下载)
- 已配置Android SDK
- 配置虚拟设备或真机
- 已安装node.js
- 已下载 selenium-server-standalone
- ADB 命令可用(查看已连接设备:
adb devices
)
使用
step 1:启动hub
命令行运行: java -jar selenium-server-standalone-3.141.59.jar -role hub
之后你可以浏览器打开查看:http://localhost:4444/wd/hub/">http://localhost:4444/wd/hub/
step 2:启动node
首先要创建node json配置文件(GitHub),详情参考:nodeconfig(appium_node_S4.json):
{
"capabilities": [
{
"deviceName": "520381b347dd148b",
"version": "7.0",
"maxInstances": 3,
"platformName": "ANDROID"
}
],
"configuration": {
"cleanUpCycle": 2000,
"timeout": 30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"url": "http://10.212.42.180:4723/wd/hub",
"host": "10.212.42.180",
"port": 4723,
"maxSession": 6,
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": "10.212.42.180"
}
}
另一台设备的node json配置文件(GitHub),(appium_node_S5.json):
{
"capabilities": [
{
"deviceName": "192.168.56.102:5555",
"version": "4.4.2",
"maxInstances": 3,
"platformName": "ANDROID"
}
],
"configuration": {
"cleanUpCycle": 2000,
"timeout": 30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"url": "http://10.212.42.180:4724/wd/hub",
"host": "10.212.42.180",
"port": 4724,
"maxSession": 6,
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": "10.212.42.180"
}
}
注册node节点到hub,如果是server版的appium,直接用appium命令就可以了,类似:appium -p 4733 --nodeconfig /path/to/nodeconfig-android.json
;但是如果安装的是桌面版的话(这里安装的是Windows的桌面版),就需要用node.js来启动,首先确定node.js的安装路径(找好node和appium的路径),然后命令行运行注册S4:node.exe C:\Users\your-user-name\AppData\Local\Programs\Appium\resources\app\node_modules\appium\build\lib\main.js --nodeconfig C:\temp\appium_node_S4.json -p 4723
,你将会看到:
另开一个命令行注册S5:node.exe C:\Users\your-user-name\AppData\Local\Programs\Appium\resources\app\node_modules\appium\build\lib\main.js --nodeconfig C:\temp\appium_node_S5.json -p 4724
注册两个之后,你将会看到:
打开链接看到已经注册好的节点:http://localhost:4444/grid/console">http://localhost:4444/grid/console
代码样例
代码下载GitHub
# coding=utf-8
import unittest
from appium import webdriver
import time
import os
PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))
class AndroidSimpleTest(unittest.TestCase):
def setUp(self):
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.0',
'deviceName': '520381b347dd148b',
"app": PATH("./test-app.apk"),
# 声明中文
"unicodeKeyboard": 'True',
# 声明中文,否则不支持中文
"resetKeyboard": 'True',
# 执行时不重新安装包
'noReset': 'True',
}
self.driver = webdriver.Remote('http://localhost:4444/wd/hub', desired_caps)
def tearDown(self):
self.driver.quit()
def test_something(self):
self.driver.find_element_by_accessibility_id("buttonStartWebviewCD").click()
input_field = self.driver.find_element_by_id('name_input')
time.sleep(1)
input_field.clear()
input_field.send_keys('Appium User')
self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"Send me your name!\")").click()
self.driver.implicitly_wait(4)
self.assertTrue(self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"This is my way of saying hello\")").is_displayed())
self.driver.find_element_by_id("goBack").click()
if __name__ == '__main__':
unittest.main()
运行时连接远程driver直接用的URL为http://localhost:4444/wd/hub
,上面例子运行的是S4,如果想运行S5,则需修改desired_caps
,将其配置修改为和appium_node_S5.json
一致即可。
参考
- https://www.zymr.com/configuring-appium-node-servers-with-selenium-grid/
- https://appiumpro.com/editions/54
- http://appium.io/docs/en/advanced-concepts/grid/