Airtest结合tidevice实现IOS自动化测试

这篇博文内容,是基于之前的配置而来的。我们可以先回顾一下之前博文,Windows搭建mac黑苹果系统WebDriverAgent重签名爬坑记

今天来分享下如何通过 tidevice实现IOS自动化测试,在之前的博文,也有介绍过Airtest基本使用。针对Airtest暂且就不过多介绍了,今天主角是tidevice,具体我们来看。

tidevice

了解到tidevice,是在2021 MTSC 上海站 的议题中看到的,新知识,就来琢磨一番。

tidevice简介

在使用工具之前,都得先了解下其特性,这样才能更好的运用。

tidevice是阿里开源的一款工具,项目地址。详细可以看项目中的描述,简单概括就是,该工具基于python语言,可以跨平台、开源脱离Mac进行IOS自动化测试。

tidevice命令

tidevice安装

python版本3.6以上,使用命令

pip3 install -U "tidevice[openssl]"

安装成功如下所示:

如果安装失败,可以使用命令(不过这种方法安装,配对功能就没有了,因为没有办法进行签名)

pip3 install -U tidevice

查看tidevice版本

tidevice version

列出连接设备

tidevice list

tidevice list --json

操作如下所示:

应用管理

安装应用

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

Run XCTest

运行XCTest

注意:在操作运行XCTest时,首先确保手机上安装了可用的WebDriverAgent

tidevice xctest -B $WebDriverAgent重签名的包名

包名可以通过 tidevice applist 命令查看

运行日志如下所示:
C:\Users>tidevice xctest -B com.yihuqingjiu.WebDriverAgentRunner.xctrunner
[I 211208 20:59:32 _device:912] BundleID: com.yihuqingjiu.WebDriverAgentRunner.xctrunner
[I 211208 20:59:32 _device:914] DeviceIdentifier: 0000XXXXX-XXXXXX01
[I 211208 20:59:32 _device:775] SignIdentity: 'Apple Development: XXXXXXXXXX'
[I 211208 20:59:32 _device:843] Launch 'com.yihuqingjiu.WebDriverAgentRunner.xctrunner' pid: 14259
[I 211208 20:59:32 _device:1006] ProductVersion: 15.0.2
[I 211208 20:59:33 _device:963] Test runner ready detected
[I 211208 20:59:33 _device:956] Start execute test plan with IDE version: 29
[I 211208 20:59:33 _device:878] WebDriverAgent start successfully

运行成功后,可以访问:http://localhost:8200/status 查看是否正常返回json数据,如下所示:

按之前的博文,访问:http://localhost:8200/inspector 可以展示手机面板,但现在高系统版本已不支持。

修改监听端口为8200, 并显示调试日志

tidevice xctest -B $WebDriverAgent重签名的包名 -e USB_PORT:8200 --debug

结合Airtest使用,操作到这里就可以使用了,后面讲解。

Relay

转发请求

转发请求到手机,类似于iproxy

tidevice relay 8100 8100

转发并把传输内容print出来

转发并把传输的内容用hexdump的方法print出来

tidevice relay -x 8100 8100

运行WebDriverAgent

运行 XCTest 并在PC上监听8200端口转发到手机8100服务

wdaproxy这个命令会同时调用xctest和relay,另外当wda退出时,会自动重新启动xctest

tidevice wdaproxy -B $WebDriverAgent重签名的包名 --port 8200

启动后你就可以使用Appium 或者 facebook-wda 来运行iOS自动化了

这个点后续再细说。

查看设备信息

设备基本信息

tidevice info

其他操作

rm cat pull push stat tree rmtree mkdir

tidevice fsync -h

其他常用

重启

tidevice reboot

截图

tidevice screenshot screenshot.jpg

输出日志

tidevice syslog

性能采集

命令行查看

tidevice perf -B com.example.demo

Airtest

Airtest连接IOS设备

在运行wda后,在airtest的设备连接窗口,输入命令连接设备,命令如下:

http+usbmux://DeviceIdentifier

DeviceIdentifier 参数在运行wda后,窗口会显示,复制过来即可。连接成功后,窗口展示如下,就可以操作app了。

到这里,我们设备已经连接好了,就可以开始码代码了,具体的代码编写demo官网也有。今天的demo就不细说了,也很简单。

运行代码

简单写了个搜索自己博客的案例,如下所示:

# -*- encoding=utf8 -*-
__author__ = "wenyihuqingjiu"

from airtest.core.api import *
from poco.drivers.ios import iosPoco

auto_setup(__file__)
connect_device("iOS:///http+usbmux://00008110-XXXXXXXX801E")
poco = iosPoco()
snapshot()
poco("Safari浏览器").click()
touch(Template(r"tpl1630501963721.png", record_pos=(0.107, -0.565), resolution=(828, 1792)))
sleep(5.0)
touch(Template(r"tpl1630552947122.png", record_pos=(-0.296, -0.425), resolution=(828, 1792)))
text("温一壶清酒 博客园")
touch(Template(r"tpl1630553104752.png", record_pos=(0.357, -0.43), resolution=(828, 1792)))
touch(Template(r"tpl1639144259796.png", record_pos=(-0.213, -0.623), resolution=(1170, 2532)))
sleep(5.0)
assert_exists(Template(r"tpl1630553184115.png", record_pos=(0.019, -0.488), resolution=(828, 1792)), "进入博客首页")

我们运行代码,来看一下效果,如下所示,画质不是太好

生成报告

代码运行结束后,可以直接生成报告,我们生成报告看一下效果,每一步操作都有记录并截图,如下所示:

报错总结

执行tidevice命令,提示无法连接

tidevice安装后,执行命令 tidevice list ,报错如下所示:

该问题,下载itunes 得到解决。

启动wda,提示Could not start service

启动wda时,会提示Could not start service,报错如下:

该问题是在下载版本zip的时候失败了,所以启动失败。当下载成功的时候,就启动成功了,如下所示:

airtest连接真机失败

通过airtest连接真机时,报错提示如下:

在确定airtest是最新版本的情况下,使用最新webdriveragent重签名了包,再次连接就正常了。

原文地址:https://www.cnblogs.com/hong-fithing/p/15252724.html