本文介绍如何使用tidevice+WDA+airtest/facebook-wda实现在Windows上进行IOS APP自动化测试

环境准备

Windows Python环境

Python 3.6+

WebDriverAgent安装

下载最新的项目到Mac:https://github.com/appium/WebDriverAgent

$ git clone https://github.com/appium/WebDriverAgent.git

用Xcode打开
Xcode -> Preference,添加账户

Macios 自动化测试 windows ios自动化测试_windows

修改WebDriverAgentRunner这个项目的Bundle ID(不要与原来的重复就行)

Macios 自动化测试 windows ios自动化测试_xcode_02

Product —> Destination —> 选择真机设备

Macios 自动化测试 windows ios自动化测试_xcode_03

Product —> Scheme —> WebDriverAgentRunner(选择Scheme为WebDriverAgentRunner)

Macios 自动化测试 windows ios自动化测试_Macios 自动化测试_04

Product —> Build For —> Testing,将项目运行到真机设备上

Macios 自动化测试 windows ios自动化测试_xcode_05

这时手机就装上了一个名为WebDriverAgentRunner的应用

Macios 自动化测试 windows ios自动化测试_windows_06

注意如果用的是企业证书安装或打包的ipa包进行安装可能需要在通用设置设备管理中添加信任后才能正常运行WDA。(如果没有Mac,可以请IOS的同事帮忙安装)

tidevice库安装
pip install -U tidevice

安装成功后查看版本:

> tidevice version
tidevice version 0.10.1

此时,将手机连接到Windows,就可以查看连接设备信息

> tidevice list
UDID                       SerialNumber    NAME         MarketName      ProductVersion  ConnType
0000xxx-00xx65xx22xx00xx  DNXXW28XXDYV    iPhone18臻享版  iPhone 14                 15.7  usb



> tidevice list --json
[
    {
        "udid": "0000xxx-00xx65xx22xx00xx",
        "serial": "DNXXW28XXDYV",
        "name": "iPhone18臻享版",
        "market_name": "iPhone 14",
        "product_version": "15.7",
        "conn_type": "usb"
    }
]

此外,tidevice还有许多常用指令

# 查看设备信息
tidevice info
# 截图(注意这是将截图保存到xx.jpg文件中,所以该图片是保存在电脑端的当前输指令的目录下,而不是在手机中)
tidevice screenshot xx.jpg
# 启动应用程序
tidevice lanch Bundle ID (Bundle ID表示包名)
如:
tidevice launch tv.danmaku.bilianime
# 停止程序
tidevice kill Bundle ID
# 查看第三方包名(Bundle ID)
tidevice applist
# 重启设备
tidevice reboot

# 关机
tidevice shutdown
# 安装应用

# 安装本地包
tidevice install C:\example.ipa

# 安装远程包
tidevice install https://.....ipa

# 多台手机时,指定uid安装
tidevice -u uid  install C:\test.ipa
# 卸载应用
tidevice uninstall 包名
# 多台手机时,指定uid卸载应用
tidevice -u uuid uninstall 包名
# 查看电池信息
tidevice info --domain com.apple.mobile.battery

tidevice battery
# 更多帮助
tidevice -h
启动WDA驱动器
tidevice -u [udid] wdaproxy -B [wda 的 bundle Id] --port 8100
例:
tidevice -u 1238101-00012345688001E wdaproxy -B com.facebook.wda.xxxnApp --port 8100

启动成功会有如下提示:

Macios 自动化测试 windows ios自动化测试_windows_07

验证启动WDA是否成功

在浏览器中输入http://localhost:8100/,如果有返回信息,则证明WDA启动成功。

http://localhost:8300/status可以查看当前设备的状态,获取与wda通信session id

Macios 自动化测试 windows ios自动化测试_ide_08


同时,启动成功后手机屏幕上会有Automation Running Hold both volume buttons to stop的水印。

完成上述环境配置后,也意味着IOS可以脱离Mac,使用Windows进行自动化操作了。

Python + facebook-wda 自动化

安装facebook-wda

pip install -U facebook-wda
import wda
ct = wda.Client("http://localhost:8100")

# 打印设备信息
print(ct.info)

# 三种打开应用的方式
# ct.session("com.sina.weibo")
# ct.session().app_activate("tv.danmaku.bilianime")
# ct.session().app_launch("com.alipay.iphoneclient")

# 打印当前所在APP的Bundle ID
print(ct.session().app_current())

# 截图
# ct.screenshot(r"C:\Users\sg0100\Desktop\test.jpg")
airtest 自动化

安装airtest,打开后在主界面的右下方 “连接IOS设备” 会自动获取到连接地址,点击连接即可

Macios 自动化测试 windows ios自动化测试_ios_09

Macios 自动化测试 windows ios自动化测试_ios_10

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"tpl1675671163913.png", record_pos=(-0.197, 0.638), resolution=(1170, 2532)))   # 点击【通知】