Windows上实现iOS APP自动化测试:tidevice + WDA + airtest!
·
环境准备
本文使用的环境如下:
-
Python 3.7.6,Python版本需要3.6+
-
tidevice==0.4.14
-
WebDriverAgent
-
airtest==1.2.2
-
iOS 设备:iPhone12
-
Windows系统版本:Windows 10 家庭中文版
使用USB连接设备,检测tidevice是否安装成功:
1 |
$ tidevice list |
airtest iOS 自动化
1. 启动WDA
获取设备的udid:
1 |
$ tidevice list |
查看wda的bundle id:
1 |
$ tidevice applist |
使用tidevice启动iOS设备上的WDA应用
1 |
$ tidevice -u [设备 udid] wdaproxy -B [wda 的 bundle Id] --port 8100 |

2. airtest 连接设备
如果要使用airtest IDE来编写自动化测试脚本,先连接 iOS 设备。
打开Airtest IDE,输入地址:
1 2 3 |
http+usbmux://00008101-000255021E08001E # 或者 http://localhost:8100/ |
00008101-000255021E08001E 是手机的udid,8100是使用tidevice启动WDA时设置的端口号。


可以使用init_device()或者connect_device()方法连接iOS设备:
1 2 3 4 5 6 |
# 方法1
init_device(platform="IOS",uuid="http://localhost:8100/")
# 方法2
connect_device("ios:///http://127.0.0.1:8100")
# 方法3
init_device(platform="IOS",uuid="http+usbmux://00008101-000255021E08001E")
|
3. airtest 自动化
连接上iOS设备后就可以编写测试用例了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#!/usr/bin/python3
# @Time: 2021/9/26 13:55
# @File: test_ios_airtest.py
from airtest.core.api import *
from airtest.report.report import simple_report
# auto_setup(__file__, logdir=True, devices=["ios:///http://127.0.0.1:8100",])
auto_setup(__file__, logdir=True)
init_device(platform="IOS",uuid="http://localhost:8100/")
start_app("com.apple.Preferences") # 打开【设置】
touch(Template(r"tpl1632398524727.png", record_pos=(-0.34, 0.236), resolution=(1125, 2436))) # 点击
# generate html report
simple_report(__file__)
|
poco iOS自动化
UI 元素可通过在AirtestIDE的Poco 辅助窗查看,注意要选择iOS。

下面是一个示例脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#!/usr/bin/python3
# @Time: 2021/9/26 13:56
# @File: test_ios_poco.py
from airtest.core.api import *
from airtest.report.report import simple_report
from poco.drivers.ios import iosPoco
# 连接设备、初始化日志路径
auto_setup(__file__, logdir=True)
init_device(platform="IOS",uuid="http://localhost:8100/")
# 打开【设置】
start_app("com.apple.Preferences")
# 初始化ios poco
poco = iosPoco()
# 点击
poco("通用").click()
poco("关于本机").click()
# 断言
assert poco('软件版本').attr('value') == "14.8"
# generate html report
simple_report(__file__)
|
查看生成的报告:

和facebook-wda库一起使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
from airtest.core.api import *
from .start_wda import StartWDA
import wda
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from poco.drivers.ios import iosPoco
class TestFacebookWDA():
def setup(self):
self.udid = "00008101-000255021E08001E"
self.wda_bundle_id = "com.facebook.WebDriverAgentRunner.test1.xctrunner"
self.port = 8100 # 8100为启动WDA设置的端口号
self.app_name = "com.apple.Preferences"
# 启动WDA
self.wda = StartWDA()
self.wda.stop_wda(self.port)
self.wda.start_wda(self.udid, self.wda_bundle_id, self.port)
# airtest初始化连接设备
init_device(platform="IOS", uuid=f"http://localhost:{self.port}/")
# poco初始化
self.poco = iosPoco()
# facebook-wda连接设备
self.c = wda.Client(f'http://localhost:{self.port}')
self.c.session().app_activate(self.app_name) # 打开设置
# start_app("com.apple.Preferences")
self.c.implicitly_wait(3.0)
def teardown(self):
self.c.session().app_terminate(self.app_name) # 退出设置
def test_demo(self):
self.c.swipe_up()
time.sleep(1)
self.c(name="通用").click()
time.sleep(1)
self.poco("关于本机").click()
assert self.poco('软件版本').attr('value') == "14.8"
ele = self.c(name="型号名称", className="XCUIElementTypeCell").wait(timeout=3.0)
assert ele.value == "iPhone 12 mini"
|
airtest和facebook-wda初始化连接设备(创建session)后,它们向WDA发送命令互不影响
如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】


更多推荐
所有评论(0)