airtest之使用tidevice工具轻松连接iOS

上期回顾:airtestIDE连接iOS真机


以下基于
python3.8;airtestIDE1.2.9;airtest1.1.10;pocoui1.0.82;iOS14.4

tidevice是今年年初阿里开源的测试iOS手机的自动化工具。该工具能够用于与iOS设备进行通信, 提供以下功能

  • 截图

  • 获取手机信息

  • ipa包的安装和卸载

  • 根据bundleID 启动和停止应用

  • 列出安装应用信息

  • 模拟Xcode运行XCTest,常用的如启动WebDriverAgent测试(此方法不依赖xcodebuild)

  • 获取指定应用性能(CPU,MEM,FPS)

该工具支持运行在Mac,Linux,Windows上

工具地址为:
https://github.com/alibaba/taobao-iphone-device

其亮点为:
第一,在Windows上也可以启动苹果手机的WebDriverAgent从而进行自动化(脱离了Xcode);
第二,不用搭建复杂的环境,装了tidevice,只要一条命令即可。

安装tidevice

需要Python3.7及以上版本

pip install tidevice

列出iOS设备

~ tidevice list
List of apple devices attached
00008020-00047421023300 iPhoneXr

~ tidevice list --json
[
    {
        "udid": "00008020-00047421023300",
        "name": "iPhoneXr"
    }
]
 

查看已安装应用BundleID

~ tidevice applist
com.KLab.TestAirTest TestAirTest 0.1
com.apple.TestFlight TestFlight 3.0.1
com.facebook.Facebook Facebook 300.0
com.appium.wda.xctrunner WebDriverAgentRunner-Runner 1.0
 

可以看到最后那个应用就是上篇文章airtestIDE连接iOS真机在Xcdoe上安装的WebDriverAgentRunner。网上说的Windows上测试iOS,脱离Mac的前提,也是你要先用Mac在Xcode上装上WebDriverAgentRunner。或者把WebDriverAgentRunner打包成ipa,然后装手机上。

Run XCTest

# com.appium.wda.xctrunner是上一步获取的BundleID

~ tidevice -u UDID xctest -B com.appium.wda.xctrunner
[I 210508 11:49:31 _device:990] BundleID: com.appium.wda.xctrunner
[I 210508 11:49:31 _device:992] DeviceIdentifier: 00008020-00047421023300
[I 210508 11:49:31 _device:834] SignIdentity: 'Apple Development: 6666666@qq.com (GU49R95D)'
[I 210508 11:49:31 _device:840] CFBundleExecutable: WebDriverAgentRunner-Runner
[I 210508 11:49:31 _device:871] AppContainer: /private/var/mobile/Containers/Data/Application/6B1FBDC0-F331-46F5-B13D-0DE4E8E4B4
[I 210508 11:49:31 _device:911] Launch 'com.appium.wda.xctrunner' pid: 442
[I 210508 11:49:31 _device:1086] ProductVersion: 14.4
[I 210508 11:49:31 _device:1041] Test runner ready detected
[I 210508 11:49:31 _device:1033] Start execute test plan with IDE version: 29
[I 210508 11:49:32 _device:947] WebDriverAgent start successfully
 

这步相当于上篇文章在Xcdoe中启动WebDriverAgentRunner,如果只连着一台手机,-u UDID可以省略不写。

在airtestIDE里连接iOS手机

上篇文章中用Xcode启动WebDriverAgentRunner后,在airtestIDE里输入的连接字符串是:
http://127.0.0.1:8100
刚刚我们用tidevice启动WebDriverAgentRunner后,需要在airtestIDE里输入的连接字符串是:
http+usbmux://00008020-00047421023300
后面那一串数字是之前获取的手机的udid

另一种启动方式

运行WebDriverAgent

tidevice wdaproxy -B com.appium.wda.xctrunner --port 8100

# 上面这条命令相当于以下2个命令:
tidevice xctest -B com.appium.wda.xctrunner
iproxy 8100 8100
 

测试Unity游戏

同上篇文章,执行完上面的步骤后,如果是要测试Unity游戏,还需要执行

iproxy 5001 5001

其他tidevice命令

# 查看版本号
$ tidevice version
0.1.0

# 安装应用
$ tidevice install example.ipa

# 指定设备安装
$ tidevice --udid $UDID install https://example.org/example.ipa

# 卸载应用
$ tidevice uninstall com.example.demo

# 启动应用
$ tidevice launch com.example.demo

# 停止应用
$ tidevice kill com.example.demo

# 查看已安装应用
$ tidevice applist

# 查看运行中的应用
$ tidevice ps
$ tidevice ps --json output as json

# 运行XCTEST
$ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner
# 运行XCTEST,修改监听端口为8200, 并显示调试日志
$ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner -e USB_PORT:8200 --debug

# 转发请求到手机,类似于iproxy
$ tidevice relay 8100 8100
# 转发并把传输的内容用hexdump的方法print出来
$ tidevice relay -x 8100 8100

# 运行 XCTest 并在PC上监听8200端口转发到手机8100服务
# wdaproxy这个命令会同时调用xctest和relay,另外当wda退出时,会自动重新启动xctest
# 不支持运行企业证书签名的WDA
$ tidevice wdaproxy -B com.facebook.wda.WebDriverAgent.Runner --port 8200

# 查看设备信息
$ tidevice info

# 查看设备电源信息
$ tidevice info --domain com.apple.mobile.battery --json
{
    "BatteryCurrentCapacity": 53,
    "BatteryIsCharging": true,
    "ExternalChargeCapable": true,
    "ExternalConnected": true,
    "FullyCharged": false,
    "GasGaugeCapability": true,
    "HasBattery": true
}

# 重启
$ tidevice reboot

# 截图
$ tidevice screenshot screenshot.jpg

# 性能采集 (TODO)
# $ tidevice perf -o fps,mem,cpu -B com.example.demo

# 输出日志 same as idevicesyslog
$ tidevice syslog

---------------------------------------------------------------------------------

关注微信公众号即可在手机上查阅,并可接收更多测试分享~

 
原文地址:https://www.cnblogs.com/songzhenhua/p/14800690.html