模拟器测试
1、打开Appium桌面客户端

点击Start Server启动Appium服务端
2、设置Desired Capabilities,运行测试

查看可用的模拟器
xcrun simctl list devices
点击Appium桌面端右上角的搜索按钮,设置Desired Capabilities
参数说明:
{"platformName":"ios",//运行平台
"deviceName":"iPhone 8",// 设备名
"platformVersion":"12.2",//系统版本
"bundleId":"com.apple.mobilesafari",// AppbundleId,这里使用iOS自带的safari浏览器测试}
点击Start session,开始测试
第一次启动,Appium客户端会运行WebDriverAgent bootstrap script安装依赖,这里要等一会
依赖安装成功之后,appium 会启动 iOS 设备上的 Safari 浏览器,并弹出 appium inspector 窗口
可能遇到的错误

image.png
原因:xcode缺少组件,打开xcode会弹出如下提示,点击安装即可

iOS真机环境配置
真机运行iOS测试,需要安装两个软件
1、安装libimobiledevice,这是用于连接 iOS 设备的开源工具,类似于 Android 的 ADB
brew install libimobiledevice --HEAD
2、安装ios-deploy,这是支持使用命令行管理 iOS 设备 app 的工具
npm install -g ios-deploy
基本配置
安装WebDriverAgent到真机
这里只讲手动配置(每次Appium更新,都需要重新安装一次WebDriverAgent),自动配置请参考这里
将真机连接电脑
进入WebDriverAgent工程所在的目录
打开终端,cd到下面目录
/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
双击WebDriverAgent.xcodeproj打开WebDriverAgent工程
设置签名证书(这里使用免费App Id的自签名证书):
打开工程TARGETS,选择WebDriverAgentLib->General,修改Bundle Identify为com.yfm.wda.lib
image.png
选择IntegrationApp->General,修改Bundle Identify为com.yfm.wda.integrationApp
image.png
选择WebDriverAgentRunner->Build Settings,修改Bundle Identify为com.yfm.WebDriverAgentRunner
image.png
证书设置完之后,使用command+U运行WebDriverAgentRunner到真机上
运行完之后,真机上会多个WebDriverAgentRunner的App,App启动之后马上退出,这是正常的
控制台会输出如下信息:

image.png
WebDriverAgentRunner启动后会在设备开启一个单独的进程运行
这里可能碰到一个问题,Server URL的端口号是0
解决方案:#661
修改Open the appium-xcuitest-driver/WebDriverAgent/WebDriverAgentLib/Routing/FBWebServer.m, and set the port to 8100 in line 123.
执行以下命令,将 iOS 设备上 WebDriverAgentRunner 监听的 8100 端口映射到 macOS 本地的 8100 端口
iproxy 8100 8100 iOS设备udid
在浏览器输入http://192.168.2.6:8100/status,可查看WebDriverAgentRunner的运行状态

image.png
设备上运行
重复模拟器设置步骤,运行Appium桌面端,
查看设备信息
xcrun simctl list devices
设置Desired Capabilities
{"platformName":"ios",//
平台"deviceName":"yfm-iPhone",// 设备名"platformVersion":"10.3.3",//
设备系统版本"bundleId":"com.taobao.tmall",//
天猫App"udid":"ad7cda4b4e7e7201865f5420b340566dd256038e"// 设备udid}
这里设置的bundleId为天猫App的bundleId
运行成功后,可看到下图

image.png
注:这里可使用Appium查看任意App的布局
pycharm 环境测试
pycharm 安装appium插件
点击pycharm菜单下的preferences,在project Interpereter 下安装

新建python 文件内容如下:
from appiumimport webdriver
desired_caps = {}
desired_caps['platformName'] ='ios'
desired_caps['platformVersion'] ='12.2'
desired_caps['deviceName'] ='iPhone 8'
desired_caps['bundleId'] ='com.apple.mobilesafari'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
运行后的效果: