准备工作
技术原理
手机端的WDA Runner(WebDriverAgent)类似于appium测试框架中的 UIAutomator Server,将从客户端接收到的控制命令转换为XCUITest 相关API操作,实现对应用界面的控制。WebDriverAgent应用需要通过xcode编译安装。
usbmuxd是苹果提供的一个服务,用于USB和TCP协议之间的转换,实现在没有网络的情况下也可以连接设备,iTunes和Xcode就使用到了这个服务,所以在环境安装中需要在windows上安装一个iTunes。
WDA的启动使用阿里开源的tidevice工具。
Language Bindings为不同语言的测试脚本,比如基于python语言的facebook-wda,基于go语言的gwda等。
环境抢建
iOS 设备安装 wda
安装Xcode
下载WebDriverAgent
安装iTunes
下载安装Windows 版 iTunes方法:下载地址
iTunes安装成功后,连接上你的iPhone:
安装python,windows10安装python详细过程
安装pip
官网下载pip安装包,官网地址:https://pypi.org/project/pip/
下载pip的tar包,如图:
解压后,通过cmd进入解压目录,执行命令:python setup.py install
安装tidevice
打开cmd,输入命令: pip3 install -U "tidevice[openssl]",如图表示安装成功
查看tidevice版本号验证是否安装成功,打开cmd,输入命令:tidevice version或tidevice -v,如图
安装git,下载地址
安装weditor(本人n次安装失败,找到了源码安装方法)
先用git下载源码,git clone https://github.com/openatx/weditor
基于源码安装pip3 install -e weditor
连接测试
手机安装WebDriverAgent
启动驱动器
tidevice wdaproxy -B com.facebook.WebDriverAgentRunner.xctrunner --port 8100
启动weditor
python -m weditor,会自动打开一个浏览器,选择iOS
python命令测试
import wda
wda.DEBUG = True
c = wda.Client('http://localhost:8100')
c = wda.Client() # 读取环境变量DEVICE_URL,如果没有使用默认地址:http://localhost:810
c.home() #点击home键
s = c.session("com.Litta.LittaCoach") # 打开app
用例编写
元素定位
s(name='我的',className='Button')
s(nameContains='账号设置',index=0)
s.xpath('//Table/Cell[2]/Button[1]/StaticText[1]')
s(className='XCUIElementTypeTextField')
s(predicate='name BEGINSWITH "账号设置"')
元素事件
s(name='我的',className='Button').tap
s(nameContains='账号设置',index=0).click()
s(name="我的").tap_hold(2.0)
text操作
s.xpath('//ScrollView/Other[1]/Other[1]').set_text('1')
s(className='XCUIElementTypeTextField').set_text('18651660882')
s(className='XCUIElementTypeTextField').clear_text()
异常
assert s(nameContains='账号设置',index=0).wait(1.0)
打印元素信息
ele = s(predicate='name BEGINSWITH "账号设置"') print(ele.name)
ele = s(predicate='name BEGINSWITH "账号设置"') print(ele.text)
屏幕截图
s.screenshot().save("test.png")
手势滑动swipe
s.swipe(fx, fy, tx, ty, duration=0.5) # 从(fx, fy)滑到(tx, ty),坐标值可以是绝对值或者百分比,duration单位秒
s.swipe_left()
s.swipe_right()
WDA教程
https://github.com/openatx/facebook-wda
https://blog.51cto.com/u_15649298/5479308
https://blog.csdn.net/QQqun810119819/article/details/120939806