Airtest1.3.0.1新增iOS设备相关接口解析

发布时间 2023-09-10 11:27:51作者: ☆星空物语☆

Airtest1.3.0.1更新了一些iOS设备相关的接口,下面就让我们看看以后有哪些新功能可以直接用了。

1.install

之前的install()只支持Android装包,以后也同样支持iOS啦

from airtest.core.api import *

# 支持ipa包安装
install(r"D:\demo\qasite.ipa") 
# 也支持通过下载链接安装APP
install("http://www.example.com/test.ipa") 

运行后会输出:

Copying '/Users/szh/Downloads/qasite.ipa' to device... [20.4 MB/s 12s] DONE.
Installing 'com.qasite.wechat' '1.1.0'
- CreatingStagingDirectory (5%)
- ExtractingPackage (15%)
- InspectingPackage (20%)
- TakingInstallLock (20%)
- PreflightingApplication (30%)
- InstallingEmbeddedProfile (30%)
- VerifyingApplication (40%)
- CreatingContainer (50%)
- InstallingApplication (60%)
- PostflightingApplication (70%)
- SandboxingApplication (80%)
- GeneratingApplicationMap (90%)
Complete

2.uninstall

之前的uninstall()只支持Android,以后也同样支持iOS啦

from airtest.core.api import *

# 参数为Bundle ID即iOS包的包名
uninstall("com.qasite.wechat") 

运行后会输出:

Uninstalling 'com.qasite.wechat'
- RemovingApplication (50%)
- GeneratingApplicationMap (90%)
Complete

3.list_app

列出设备已安装应用信息

from airtest.core.api import *
dev = device()

all_app = dev.list_app("all")
print(f"all_app:\n{all_app}")

print(f"system_app:\n{dev.list_app('system')}")

user_app = dev.list_app("user")
print(f"user_app:\n{user_app}")

运行后会报错

Traceback (most recent call last):
  File "/Users/szh/opt/miniconda3/envs/test/lib/python3.8/site-packages/airtest/cli/runner.py", line 75, in runTest
    six.reraise(*sys.exc_info())
  File "/Users/szh/opt/miniconda3/envs/test/lib/python3.8/site-packages/six.py", line 719, in reraise
    raise value
  File "/Users/szh/opt/miniconda3/envs/test/lib/python3.8/site-packages/airtest/cli/runner.py", line 72, in runTest
    exec(compile(code.encode("utf-8"), pyfilepath, 'exec'), self.scope)
  File "/Users/szh/auto_test/suite_test/suite_test.py", line 26, in <module>
    all_app = dev.list_app()
  File "/Users/szh/opt/miniconda3/envs/test/lib/python3.8/site-packages/airtest/core/ios/ios.py", line 47, in wrapper
    return func(self, *args, **kwargs)
  File "/Users/szh/opt/miniconda3/envs/test/lib/python3.8/site-packages/airtest/core/ios/ios.py", line 850, in list_app
    return TIDevice.list_app(self.udid, type=type)
  File "/Users/szh/opt/miniconda3/envs/test/lib/python3.8/site-packages/airtest/core/ios/ios.py", line 67, in wrapper
    return func(*args, **kwargs)
TypeError: list_app() got an unexpected keyword argument 'type'

这是因为Airtest1.3.0.1有个Bug,如果你现在的版本是高于此版本的,那相信网易已经修改了。
我们看/your_python_path/site-packages/airtest/core/ios/ios.py中,第850行,写的是TIDevice.list_app(self.udid, type=type);但110行却是def list_app(udid, app_type="user"),参数名写的不一致,一个是app_type,一个是type,统一一下就行了,比如将850行的type改为app_type。

之后再次运行后会输出:

[('com.apple.Home', '家庭', '6.0'), ('com.apple.shortcuts', '快捷指令', '5.0'), ('com.google.chrome.ios', 'Chrome', '115.5790.130')]

[('com.apple.Home', '家庭', '6.0'), ('com.apple.CTCarrierSpaceAuth', 'CTCarrierSpaceAuth', '1.0'), ('com.apple.shortcuts', '快捷指令', '5.0'),]

[('com.ownbook.notes', '爱思全能版', '2.2.0'), ('com.google.chrome.ios', 'Chrome', '115.5790.130')]

4.iOS剪切板功能:get_clipboard、set_clipboard

from airtest.core.api import *

set_clipboard("qasite")
text = get_clipboard()
print(f'剪贴板内容:{text}')

运行后会输出:qasite

主要可以用到:

  • 点击一键复制XXX后,以后可以获取剪切板与文本进行比对了;

  • 输入复制内容:先设置剪切板,再在app上长按,选粘贴

5.tidevice相关接口

其实就是封装了tidevice的命令,常用接口如下:

  • devices :列出USB连接的所有设备的 UDID 列表

from airtest.core.ios.ios import TIDevice

udid = TIDevice.devices()
print(f"TIDevice.devices():\n{udid}")

输出

['f83a2d08deb8c22ce6338e35328f5cfcaaf5d3f4']

如果是要查看当前连接的iOS设备,直接这样获取即可:print(dev.udid)

  • list_app :列出手机上安装的应用列表,支持对类型进行筛选,包括 user/system/all
    上面Airtest接口中已有,用那个就行了

  • list_wda :列出手机上安装的所有WDA的 bundleID
    一般我们也不会同时装好多个wda吧,基本不用

  • device_info :获取手机信息

print(f"TIDevice.device_info():\n{TIDevice.device_info(udid)}")
#  输出
{'productVersion': '15.7.3', 'productType': 'iPhone9,2', 'modelNumber': 'MNRL2', 'serialNumber': 'F2LSGNHG52', 'timeZone': 'Asia/Shanghai', 'uniqueDeviceID': 'f83a2d08deb8caaf5d3f4', 'marketName': 'iPhone 7 Plus'}
  • install_app :安装ipa包,支持本地路径或URL
    上面Airtest接口中已有,用那个就行了

  • uninstall_app:卸载 bundle_id 对应的包体
    上面Airtest接口中已有,用那个就行了

  • start_app :启动 bundle_id 对应的包体
    原有Airtest接口中已有,用那个就行了

  • stop_app :停止 bundle_id 对应的包体
    原有Airtest接口中已有,用那个就行了

  • ps :获取当前的进程列表
    想获取app包名时,也可以用此方法

print(f"TIDevice.ps():\n{TIDevice.ps(udid)}")
# 输出
[{'pid': 4848, 'name': 'StoreKitUIService', 'bundle_id': 'com.apple.ios.StoreKitUIService', 'display_name': 'iTunes'}, {'pid': 217, 'name': 'Spotlight', 'bundle_id': 'com.apple.Spotlight', 'display_name': '搜索'}, {'pid': 5176, 'name': 'TestFlight', 'bundle_id': 'com.apple.TestFlight', 'display_name': 'TestFlight'}, {'pid': 7699, 'name': 'AppStore', 'bundle_id': 'com.apple.AppStore', 'display_name': 'App Store'}, {'pid': 5050, 'name': 'SafariViewService', 'bundle_id': 'com.apple.SafariViewService', 'display_name': 'Safari浏览器'}, {'pid': 6800, 'name': 'Preferences', 'bundle_id': 'com.apple.Preferences', 'display_name': '设置'}]
  • ps_wda :获取当前启动中的WDA列表
    暂时没想到使用场景

  • xctest:启动WDA

import threading
import time
from airtest.core.ios.ios import TIDevice

wda_bundle_id = TIDevice.list_wda(self.udid)[0]
# 创建一个线程,执行xctest
t = threading.Thread(target=TIDevice.xctest, args=(self.udid, wda_bundle_id), daemon=True)
t.start()
time.sleep(5)
ps_wda = TIDevice.ps_wda(self.udid)
print(ps_wda)
self.assertIn(wda_bundle_id, ps_wda)
time.sleep(5)
# 终止线程
t.join(timeout=3)

 

---------------------------------------------------------------------------------

关注微信公众号即可在手机上查阅,并可接收更多测试分享~