与iOS设备通讯的工具:tidevice、idb

tidevice


tidevice(taobao iphone device)是阿里开源的用于与iOS设备通讯的工具,提供以下功能:

  • 截图
  • 获取手机信息
  • ipa包的安装和卸载
  • 根据bundleID 启动和停止应用
  • 列出安装应用信息
  • 模拟Xcode运行XCTest,常用的如启动WebDriverAgent测试(此方法不依赖xcodebuild)
  • 获取指定应用性能(CPU,MEM,FPS)(todo)
  • 支持运行在Mac,Linux,Windows上

项目地址

安装

pip3 install -U "tidevice[openssl]"

python版本必须3.6+

  • 安装校验
$ tidevice version
tidevice version 0.3.3

常用命令

  • 列出连接的设备tidevice listtidevice list --json(json格式输出):udid和name
  • 设备信息
    • 利用tidevice info查看设备信息,比如:设备序列号、系统版本、CPU、本机号码、时间戳、蓝牙地址、MAC 地址等信息
    • 使用--domain + 应用包名来获取其他信息如设备电源信息tidevice info --domain com.apple.mobile.battery --json,更多支持的domain请查看官方文档
  • 转发请求到手机tidevice relay,类似于iproxy如tidevice relay 8100 8100
  • 运行xctesttidevice xctest
# 运行XCTEST
$ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner
...log...
# 修改监听端口为8200, 并显示调试日志
$ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner -e USB_PORT:8200 --debug
  • tidevice wdaproxy命令,wdaproxy这个命令会同时调用xctest和relay命令,另外当wda退出时,会自动重新启动xctest
# 可通过tidevice applist来获取wda的BundleID
# 运行 XCTest 并在PC上监听8200端口转发到手机8100服务
# facebook-wda
$ tidevice wdaproxy -B com.facebook.wda.WebDriverAgent.Runner --port 8200
# appium的wda
$ tidevice wdaproxy -B com.gameappium.WebDriverAgentRunner.xctrunner --port 8200

可通过在浏览器中访问http://localhost:8200/status来查看WDA是否启动成功

  • 应用管理
# 安装应用
$ tidevice install example.ipa

# 指定设备安装--udid
$ tidevice --udid $UDID install https://example.org/example.ipa

# 卸载应用
$ tidevice uninstall com.example.demo

# 启动应用
$ tidevice launch com.example.demo

# 停止应用
$ tidevice kill com.example.demo

# 查看已安装应用
$ tidevice applist
  • 重启及截图
# 重启
$ tidevice reboot

# 截图
$ tidevice screenshot screenshot.jpg
  • 查看日志tidevice syslog
  • 更多请查看帮助或官方文档tidevice -h

自动化测试

  • 前提:安装并运行WDA,可进一步参看WDA介绍
  • 启动后你就可以使用appium或facebook-wda来运行iOS自动化了
# 安装facebook-wda依赖
pip3 install -U facebook-wda
  • 简单示例
import wda
import time

# 连接设备
c1 = wda.USBClient()

# c1 = wda.Client("http://localhost:8200")
print(c.info)

# 极客时间
bundle_id = 'org.geekbang.GeekTime'

# 通过app的bundleId启动App
s = c1.session(bundle_id)

# 休眠5s
s.sleep(5)

# 截图保存到本地
c1.screenshot('sc.png')

# 回到桌面
s.home()

idb


idb(iOS Development Bridge)即iOS版本的adb

项目地址

安装

brew tap facebook/fb
brew install idb-companion
  • idb cli:必须py3.6+
pip install fb-idb

常用命令

$ idb -h
usage: idb [-h] [--log {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
           [--companion COMPANION] [--companion-path COMPANION_PATH]
           [--companion-local] [--no-prune-dead-companion]
           {add-media,approve,boot,clear-keychain,clone,companion,connect,contacts,crash,create,daemon,debugserver,delete,delete-all,describe,disconnect,dsym,dylib,erase,file,focus,framework,install,instruments,kill,launch,list-apps,list-targets,log,open,pull,push,record,screenshot,set-location,shutdown,terminate,ui,uninstall,video,record-video,xctest}
           ...

idb: a versatile tool to communicate with iOS Simulators and Devices

positional arguments:
  {add-media,approve,boot,clear-keychain,clone,companion,connect,contacts,crash,create,daemon,debugserver,delete,delete-all,describe,disconnect,dsym,dylib,erase,file,focus,framework,install,instruments,kill,launch,list-apps,list-targets,log,open,pull,push,record,screenshot,set-location,shutdown,terminate,ui,uninstall,video,record-video,xctest}
    add-media           Add photos/videos to the target
    approve             Approve permissions for an app
    boot                Boots a simulator (only works on mac)
    clear-keychain      Clear the targets keychain
    clone               Erases the simulator (only works on mac)
    companion           commands related to the companion
    connect             Connect to a companion
    contacts            Contacts database operations on target
    crash               Operations on crashes
    create              Creates an iOS Simulator
    daemon              This command is deprecated. the idb daemon is not used anymore.
    debugserver         debugserver interactions
    delete              Deletes (only works on mac)
    delete-all          Deletes all simulators (only works on mac)
    describe            Describes the Target
    disconnect          Disconnect a companion
    dsym                dsym commands
    dylib               dylib commands
    erase               Erases the simulator (only works on mac)
    file                File operations on target
    focus               Brings the simulator window to front
    framework           framework commands
    install             Install an application
    instruments         Run instruments on the device
    kill                Kill the idb daemon
    launch              Launch an application. Any environment variables of the form IDB_X
                         will be passed through with the IDB_ prefix removed.
    list-apps           List the installed apps
    list-targets        List the connected targets
    log                 Obtain logs from the target
    open                Open a URL
    pull                Copy a file inside an application's container
    push                Copy file(s) from local machine to target
    record              Record what the screen is doing
    screenshot          Take a Screenshot of the Target
    set-location        Set a simulator's location
    shutdown            Shuts the simulator down (only works on mac)
    terminate           Terminate a running application
    ui                  UI interactions on target
    uninstall           Uninstall an application
    video (record-video)
                        Record the target's screen to a mp4 video file
    xctest              Operations with xctest on target

optional arguments:
  -h, --help            show this help message and exit
  --log {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Set the logging level
  --companion COMPANION
                        A string of the form HOSTNAME:PORT that will describe the companion connect to.Can also be set with the IDB_COMPANION environment variable
  --companion-path COMPANION_PATH
                        The path to the idb companion binary. This is only valid when running on macOS platforms
  --companion-local     If set, any companion provided via IDB_COMPANION or --companion will be assumed to be running on this host.Even if the companion provided is 'localhost' idb will still assume it is remote.The reason for this is that idb shouldn't assume there are no tunnels from localhost to a remote host.Can also be set with the IDB_COMPANION_LOCAL environment variable
  --no-prune-dead-companion
                        If flagged will not modify local state when a companion is known to be unresponsive

See Also: https://www.fbidb.io/docs/guided-tour

问题解决

某些系统执行命令报错,requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://github.com/iGhibli/iOS-DeviceSupport/raw/master/DeviceSupport/15.4.zip

浏览器访问https://github.com/iGhibli/iOS-DeviceSupport/raw/master/DeviceSupport/15.4.zip,确实没该包,需要手动下载对应的包放到对应目录,如我的手机系统为iOS 15.4.1版本

  1. https://github.com/iGhibli/iOS-DeviceSupport/raw/master/DeviceSupport/找到对应系统的包并下载zip包并修改为15.4.zip
  2. 拷贝到~/.tidevice/device-support/目录下
$ mv 15.4.zip ~/.tidevice/device-support/
  • 还是不能解决参考如下:
    升级tidevice
pip3 install -U tidevice
tidevice version

下载全部镜像

tidevice developer --download-all

iOS 16 需打开开发者模式:设置 -> 隐私与安全性 -> 开发者模式

参考及扩展阅读