Windows上实现iOS APP自动化测试:tidevice + WDA + airtest!

环境准备

本文使用的环境如下:

  1. Python 3.7.6,Python版本需要3.6+

  2. tidevice==0.4.14

  3. WebDriverAgent

  4. airtest==1.2.2

  5. iOS 设备:iPhone12

  6. 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

图片

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:691998057【暗号:csdn999】

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发送命令互不影响

下面是配套资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

最后: 可以在公众号:程序员小濠 ! 免费领取一份216页软件测试工程师面试宝典文档资料。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!

  • 22
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值