Appium-Python-Client 版本1.2.0
selenium 版本 3.141.0
UI自动化的优点
自动化工具选型
Appium 常用于原生和web和小程序
airtest 常用于游戏测试
appium介绍
Appium是一个移动端的自动化测试框架
原生应用:手机自带的应用
移动网页应用:手机上可以打开的网页应用
混合应用:原生应用嵌套了网页应用
支持ios和安卓操作
跨语言:java,python,nodejs等
跨平台:安卓,ios,windows,mac
底层多引擎可切换
连接mumu模拟器
打开模拟器
adb connect 127.0.0.1:7555
adb devices
第一个脚本
from appium import webdriver
desired_caps={}
desired_caps['platformName']='Android'
desired_caps['platformVersion']='6.0'
desired_caps['deviceName']='emulator-5554'
# com.android.settings/com.android.settings.Settings
desired_caps['appPackage']='com.android.settings'
desired_caps['appActivity']='com.android.settings.Settings'
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
print("启动【设置】应用")
driver.quit()
Appium Inspctor
是appium desktop 的一个功能,可以查找元素,录制脚本
手机端每个软件都有自己的packagename,每个页面有activityname
获取app的信息
1、通过 logcat 日志获取
mac/Linux: adb logcat ActivityManager:I | grep "cmp"
windows: adb logcat ActivityManager:I | findstr "cmp"
后启动目标应用
2、通过 aapt 获取
mac/Linux: aapt dump badging wework.apk | grep launchable-activity
Windows: aapt dump badging wework.apk | findstr launchable-activity
启动应用命令
adb shell am start -W -n <package-name>/<activity-name> -S
启动session
-
platformName
:平台,Android/iOS -
deviceName
:设备名 -
appPackage
:应用的包名 -
appActivity
:应用的页面名 Activity -
noReset
: 防止清空缓存信息
安装apk
1,拖拽到模拟器
2,更多 安装
4,adb install -r apk路径
Capability 简介
- 功能:配置 Appium 会话,告诉 Appium 服务器需要自动化的平台的应用程序
- 形式:键值对的集合,键对应设置的名称,值对应设置的值
- 主要分为三部分
- 公共部分
- ios 部分
- android 部分
公共部分参数配置
键 | 描述 | 值 |
---|---|---|
platformName |
使用的手机操作系统 | iOS,Android,或者 Firefox0S |
platformVersion |
手机操作系统的版本 | 例如 7.1 , 4.4
|
deviceName |
使用的手机或模拟器类型 |
iPhone Simulator , iPad Simulator , iPhone Retina 4-inch , Android Emulator , Galaxy S4 , 等等…. 在 iOS 上,使用 Instruments的 instruments -s devices 命令可返回一个有效的设备的列表。在 Andorid 上虽然这个参数目前已被忽略,但仍然需要添加上该参数 |
automationName |
使用哪个自动化引擎 |
android 默认使用uiautomator2 ,ios 默认使用XCUTest
|
noReset |
在当前 session 下不会重置应用的状态。默认值为 false
|
true , false
|
udid |
连接的真实设备的唯一设备编号 (Unique device identifier) | 例如 1ae203187fc012g
|
Android 部分特有参数配置
“”
键 | 描述 | 值 |
---|---|---|
appActivity | Activity 的名字是指从你的包中所要启动的 Android acticity。他通常需要再前面添加. (例如 使用 .MainActivity 代替 MainActivity) | MainActivity, .Settings |
appPackage | 运行的 Android 应用的包名 | com.example.android.myApp, com.android.settings |
appWaitActivity | 用于等待启动的 Android Activity 名称 | SplashActivity |
unicodeKeyboard | 启用 Unicode 输入,默认为 false | true or false |
resetKeyboard | true or false | |
dontStopAppOnReset | 首次启动的时候,不停止 app | true or false |
skipDeviceInitialization | 跳过安装,权限设置等操作 | true or false |
iOS 独有
键 | 描述 | 值 |
---|---|---|
bundleId |
被测应用的 bundle ID 。用于在真实设备中启动测试,也用于使用其他需要 bundle ID 的关键字启动测试。在使用 bundle ID 在真实设备上执行测试时,你可以不提供 app 关键字,但你必须提供 udid 。 |
例如 io.appium.TestApp
|
autoAcceptAlerts |
当 iOS 的个人信息访问警告 (如 位置、联系人、图片) 出现时,自动选择接受( Accept )。默认值 false | true 或者 false |
showIOSLog |
是否在 appium 日志中显示从设备捕获的任何日志。默认 false | true or false |
{
"platformName": "android",
"deviceName": "emulator-5554",
"appPackage": "io.appium.android.apis",
"appActivity": ".ApiDemos"
}
{
"noReset": "true", // 不清空缓存信息
"dontStopAppOnReset": "true", // 首次启动的时候,不停止app
"skipDeviceInitialization": "true", // 跳过安装,权限设置等操作
"unicodeKeyBoard": "true" // 输入中文
}
启动
# 方式一:
self.driver = webdriver.Remote\
("http://127.0.0.1:4723/wd/hub", desire_cap)
# 方式二:放到后台,热启动,会进入到app的首页
self.driver.launch_app()
定位方法
常用
1,id
driver.find_element_by_id(resource-id属性)
driver.find_element(AppiumBy.ID,“resource:id”)
2,accessibility_id
driver.find_element_by_accessibility_id(content-desc属性值)
driver.find_element(AppiumBy.ACCESSIBILITY_ID,“content-desc:属性值”)
3,xpath
dirver.find_element_by_xpath(xpath属性)
定位工具
1,appium 繁琐,需要启动session
2,uiautomatorviewer 简洁 有adb设备就可以使用 但不能获取xpath
3,webitor 不能和appium一起使用 可以获取xpath,但不能搜索xpath,可以实时刷新,比其他两个要快,可以和airtest一起使用
4,appium client版本1.22以后,appium Inspector分离出来了,可以单独使用,
https://github.com/appium/appium-inspector/releases
元素的常用方法
- 点击方法
element.click()
- 输入操作
element.send_keys('appium')
- 设置元素的值
element.set_value('appium')
- 清除操作
element.clear()
- 是否可见
element.is_displayed()
返回 True/False - 是否可用
element.is_enabled()
返回 True/False - 是否被选中
element.is_selected()
返回 True/False - 获取属性值
get_attribute(name)
元素常用属性
- 获取元素文本
- 格式:element.text
- 获取元素坐标
- 格式:element.location
- 结果:
{'y': 19,'x: 498}
- 获取元素尺寸(高和宽)
- 格式:element.size
- 结果:
{'width':500,'height':22)
、
通过命令行执行appium server时,执行脚本报错
E selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Cannot verify the signature of 'C:\Users\saber\AppData\Roaming\npm\node_modules\appium\node_modules\appium-uiautomator2-server\apks\appium-uiautomator2-server-v4.15.0.apk'. Original error: java.lang.UnsupportedClassVersionError: com/android/apksigner/ApkSignerTool has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
因为sdk的build-tools版本大于30,必须换30以下的
location是元素左上角的位置
size 是元素的长和宽
text 是元素的文本属性
print('location:',search_element.location)
print('size:',search_element.size)
print('text:',search_element.text)
location: {'x': 22, 'y': 64}
size: {'height': 64, 'width': 574}
text: 000905
capility进阶用法
官网
https://appium.io/docs/en/writing-running-appium/caps/#general-capabilities
newCommandTimeout
上一个命令和下一个命令直接等待的时间,默认是60秒,比如上传文件的时间就很长,就需要设置这个参数的时间为5分钟
desired_caps['newCommandTimeout'] = 300
udid
如果连接多设备时,只会读取第一个设备(b34facaf),可以通过udid指定设备,使用adb devices 获取设备id
desired_caps['udid'] = '127.0.0.1:7555'
autoGrantPermissions
安装app时,会有权限的弹框,这个参数可以自动点掉,默认为false,如果 设置了noReset(不重置app),那么这个参数是不生效的
desired_caps['autoGrantPermissions'] = 'true'
测试策略相关
noReset 不重置app:保留缓存
fullReset 完全重置app:卸载app重新安装
dontStopAppOnReset 不杀死进程,通过apppackage和appactivity唤醒app, 默认True
desired_caps['dontStopAppOnReset '] = True
执行
adb shell am start com.xueqiu.android/.common.MainActivity
desired_caps['dontStopAppOnReset '] = False
执行
adb shell am start -S com.xueqiu.android/.common.MainActivity
其中 -S 会先关闭app再唤醒,而不加s(True)则不会关闭
获取toast
toast 一闪而过的 提示框,不是alert