Appium测试脚本-Python
Appium的Client端的编写支持多种主流语言,这里我以Python语言为例,来说明如何编写Appium的Client端测试脚本。
项目地址:https://github.com/appium/python-client
安装
pip install Appium-Python-Client -i https://pypi.python.org/simple/
另外,官方提供了Android和iOS的两个测试脚本的例子:
# Android environment
from appium import webdriver
# Options are only available since client version 2.3.0
# If you use an older client then switch to desired_capabilities
# instead: https://github.com/appium/python-client/pull/720
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
options = UiAutomator2Options()
options.platformVersion = '10'
options.udid = '123456789ABC'
options.app = PATH('../../../apps/test-app.apk')
# Appium1 points to http://127.0.0.1:4723/wd/hub by default
self.driver = webdriver.Remote('http://127.0.0.1:4723', options=options)
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='item')
el.click()
# iOS environment
from appium import webdriver
# Options are only available since client version 2.3.0
# If you use an older client then switch to desired_capabilities
# instead: https://github.com/appium/python-client/pull/720
from appium.options.ios import XCUITestOptions
from appium.webdriver.common.appiumby import AppiumBy
options = XCUITestOptions()
options.platformVersion = '13.4'
options.udid = '123456789ABC'
options.app = PATH('../../apps/UICatalog.app.zip')
# Appium1 points to http://127.0.0.1:4723/wd/hub by default
self.driver = webdriver.Remote('http://127.0.0.1:4723', options=options)
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='item')
el.click()
更多的文档介绍可以访问下面的链接学习: https://appium.github.io/python-client-sphinx/
启动并连接Server端
这里分两种方式来连接App进行测试:真机 and 模拟器
真机测试(Android)
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
# 配置
options = UiAutomator2Options()
options.platform_name = 'Android' # App平台
options.device_name = 'DEVICE_ABC' # 真机的名称
options.app_package = 'com.xxx.xxx' # App包名
options.app_activity = 'com.xxx.xxx.ui.MainActivity' # App的主Activity
# 通过Appium Server端建立和真机的连接
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options=options)
# 模拟操作
driver.implicitly_wait(10)
driver.find_element(by=AppiumBy.ID, value='com.xxx.xxx:id/pBtn').click()
模拟器测试(夜神模拟器)
“夜神模拟器”是一款主推游戏的手机端模拟器,但是用来做App测试,也很方便。 官网地址:https://www.yeshen.com/ 按照自己的操作系统,下载对应的客户端安装即可使用。
启动“夜神模拟器”后,还需要通过adb命令来连接该模拟器,才能进行测试脚本的执行。默认的连接命令如下:
$ adb connect 127.0.0.1:62001
连接成功后,通过如下命令,可以查看“夜神模拟器”的状态:
$ adb devices
List of devices attached
127.0.0.1:62001 device
而且“127.0.0.1:62001”也将作为Appium连接客户端的"device_name",完整的连接代码如下:
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
# 配置
options = UiAutomator2Options()
options.platform_name = 'Android' # App平台
options.device_name = '127.0.0.1:62001' # “夜神模拟器”的名称
options.app_package = 'com.xxx.xxx' # App包名
options.app_activity = 'com.xxx.xxx.ui.MainActivity' # App的主Activity
# 通过Appium Server端建立和真机的连接
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options=options)
# 模拟操作
driver.implicitly_wait(10)
driver.find_element(by=AppiumBy.ID, value='com.xxx.xxx:id/pBtn').click()
模拟命令
- 查找元素:
driver.find_element(by, value)
by
: 查看的方式,对应的值可以参考类 AppiumBy,例如,按照id查找,则是AppiumBy.ID,按照xpath路径查找,则是AppiumBy.XPATH;
value
:对应的值
示例代码:
# 查找id为com.xxx:id/btn的按钮元素
driver.find_element(by=AppiumBy.ID, value='com.xxx:id/btn')
# 查找xpath为abc/def/xxxx.ImageView的元素
driver.find_element(by=AppiumBy.XPATH, value='abc/def/xxxx.ImageView')
- 点击事件:
driver.find_element(by=by, value=value).click()
- 滑动事件:
driver.swipe(x1, y1, x2, y2, t)