基于 Appium 的 iOS 自动化
一、Appium 环境搭建(macOS 10.15)
1、安装 Appium-server
(1) 安装 node.js
方法1:brew install node
方法2:下载pkg文件安装( 推荐)
检查是否安装成功:
➜ /Users
node -v
v12.11.1
➜ /Users
npm -v
6.14.7
(2)安装 appium
➜ /Users
npm install -g appium
2、替换并配置 WebDriverAgent
(1)GitHub 下载 WebDriverAgent
(2)替换本地 Appium-server 下的 WebDriverAgent
Appium-server 下 WebDriverAgent 路径:
/usr/local/lib/node_modules/appium/node_modules/appium-webdriveragent
(3) 配置 WebDriverAgent → 查看 详细配置教程
3、安装 Appium-Client
Python https://github.com/appium/python-client
Java https://github.com/appium/java-client
4、启动自动化环境
(1)启动 Appium (→ 完整参数说明)
appium --session-override --relaxed-security --log-timestamp -g appium.txt
(2)启动 WebDriverAgent (→ 参考 详细配置教程-配置部分)
Xcode - WebDriverAgent - product - test
二、iOS UI自动化简述
1、UI自动化会做什么?
- 元素定位
- 元素操作
- 校验
2、iOS 怎么做UI自动化?(基于Appium)
(1)怎么安装包?
- 安装 ipa 包
ideviceinstaller -i xxx.ipa
- 卸载应用
ideviceinstaller -U [bundleID]
- 查看设备安装了哪些应用
➜ /Users
ideviceinstaller -l
com.ss.iphone.ugc.Aweme, “132010”, “抖音”
com.apple.TestFlight, “37”, “TestFlight”
com.kkmh.WebDriverAgentRunner.xctrunner, “1”, “WebDriverAgentRunner-Runner”
com.tencent.ied.app.comic, “12054”, “腾讯动漫”
com.tencent.mttlite, “8.9.1.4014”, “QQ浏览器”
- 查看设备udid
➜ /Users
> idevice_id -l
7d65ad26a4e27ce38adda4d7a794cf1bfc08c9b7
- 把设备里的崩溃日志都取出来
idevicecrashreport -u 7d65ad26a4e27ce38adda4d7a794cf1bfc08c9b7 -e -k ~/Downloads/temp
Copy: /periodic-io-microstackshot-report-2020-10-16-154441.ips
Copy: /mttlite-2020-10-21-132243.ips
Copy: /LowBatteryLog-2020-10-07-182630.ips
Copy: /periodic-microstackshot-report-2020-10-30-092621.ips
Copy: /stacks+routined-2020-10-12-151256.ips
Copy: /JetsamEvent-2020-09-23-104111.ips
Copy: /mttlite-2020-10-21-135024.ips
Copy: /Analytics-2020-09-23-080316.ips.ca
Copy: /JetsamEvent-2020-09-28-070134.ips
Copy: /JetsamEvent-2020-09-23-164411.ips
Copy: /periodic-io-microstackshot-report-2020-10-27-152037.ips
Copy: /stacks+routined-2020-10-30-185706.ips
Done.
(2)怎么启动应用?怎么定位元素?怎么操作元素?
通过 WebDriverAgent 实现。
WebDriverAgent is a WebDriver server implementation for iOS that can be used to remote control iOS devices.
It allows you to launch & kill applications, tap & scroll views or confirm view presence on a screen.
WebDriverAgent 基于 XCTest ,那 XCTest 实现了什么:
- class XCUIApplication → 启动应用、结束应用
- class XCUIElementQuery → 查询元素
- protocol XCUIElementAttributes → 暴露UI元素属性
- class XCUIElement → 查询元素状态、输入、点击、双击、滑动、两指操作等
- class XCUIDevice → 模拟物理按键,设备方向,siri交互
三、基于页面滑动的自动化实践
自动化实践:列表页不断上滑,验证页面稳定性
1、UI脚本思路
- 操作:进入列表页,不断进行上滑操作
- UI脚本缺点:查询控件慢
- 解决:去掉通过脚本查询控件,换成手动点开列表页;之后再由UI脚本进行滑动操作。
2、UI脚本实现(基于Appium + WebDriverAgent)
- 手动点击进入列表页(主要增加手动操作时间,直接写了 20s)
import time
time.sleep(20)
- 实现向上滑动功能
def swipe(self):
telPa = Appium.getDriver().get_window_size()
self.width = telPa["width"]
self.height = telPa["height"]
x1=int(self.width * 1/2)
y1=int(self.height * 3/4)
y2=int(self.height * 1/4)
Appium.getDriver().swipe(x1, y1, x1, y2, 100)
- 使用滑动功能
t = 0
while t< 100000:
self.swipe()
t+=1
print("现在t的值是:"+str(t))
参考文档
Appium文档:https://github.com/appium/appium/tree/master/docs
WebDriverAgent:https://github.com/facebookarchive/WebDriverAgent
XCTest:https://developer.apple.com/documentation/xctest
libimobiledevice:https://github.com/libimobiledevice/libimobiledevice
ideviceinstaller:https://github.com/libimobiledevice/ideviceinstaller