API来自WDA官方页面:https://github.com/facebook/WebDriverAgent/wiki/Queries
楼主的iPhone自动化测试项目采用浏览器前端页面UI&UE
+ 服务器作为前端和Mac端桥接并管理数据
+ Mac端实现client&管理多台手机
+iPhone端运行WebDriverAgent
架构,使用是client向WDA发送网络请求,调用WDA的API完成对手机的点击、滑动等操作。
网络请求使用NSURLSession(OC);如果用终端测试,可以用curl
(终端用来发送网络请求的工具包)。
终端变量:
DEVICE_URL
set as device URL (eg. http://localhost:8100)
SESSION_ID
set as session id. Returned by start session command e.g. D15E12F6-CA23-4CD4-89F9-E5C5EA6F4FAD. If you want to use WebDriverAgent without launching an app you can use the default session ID reported by the /status endpoint.
JSON_HEADER='-H
"Content-Type: application/json"'
接口测试:
1. 根据bundleId启动iPhone应用
// 请求方法1:curl命令
curl -X POST $JSON_HEADER -d "{\"desiredCapabilities\":{\"bundleId\":\"com.apple.calculator\"}}" http://localhost:7999/session
// 请求方法2:POST
http://localhost:7999/session
// 方法2参数:转成json字符串设置到请求体(HTTPBody)
{
"desiredCapabilities":{
"bundleId":"com.apple.calculator"
}
}
// 返回数据
{
"value":{
"state":"success",
"os":{
"name":"iOS",
"version":"11.4.1"
},
"ios":{
"simulatorVersion":"11.4.1",
"ip":"169.254.184.254"
},
"build":{
"time":"Jul 31 2018 14:59:39"
}
},
"sessionId":"E2431B5B-C14D-409C-890D-A9F2320958CE",
"status":0
}
2. 获取status
// 请求方法1:curl命令
curl -X GET $JSON_HEADER $DEVICE_URL/status
// 请求方法2:GET
http://localhost:7999/status // 7999是iproxy转发到Mac的端口号
// 方法2参数:无
// 返回数据:
{
"value":{
"state":"success",
"os":{
"name":"iOS",
"version":"11.4.1"
},
"ios":{
"simulatorVersion":"11.4.1",
"ip":"169.254.184.254"
},
"build":{
"time":"Jul 30 2018 10:07:54"
}
},
"sessionId":"C09DE29D-030F-4AD8-85E6-C78E5B38DF8D",
"status":0
}
3. 查询当前会话的sessionId
// 思路:先查status,再从中取出sessionId
4. 查询当前会话的sessionId
// 请求方法1:curl命令
curl -X GET $JSON_HEADER http://localhost:7999/session/E2431B5B-C14D-409C-890D-A9F2320958CE
// 请求方法2:GET
http://localhost:7999/session/E2431B5B-C14D-409C-890D-A9F2320958CE // 这一长串是sessionId
// 返回数据
{
"value":{
"sessionId":"E2431B5B-C14D-409C-890D-A9F2320958CE",
"capabilities":{
"device":"iphone",
"browserName":"计算器",
"sdkVersion":"11.4.1",
"CFBundleIdentifier":"com.apple.calculator"
}
},
"sessionId":"E2431B5B-C14D-409C-890D-A9F2320958CE",
"status":0
}
5. 查询控件的elementId
// curl命令
curl -X POST $JSON_HEADER -d "{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeButton[@name='Share']\"}" $DEVICE_URL/session/$SESSION_ID/element
// 举个例子
curl -X POST $JSON_HEADER -d "{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeIcon[@name='微信']\"}" http://169.254.81.109:8100/session/B1C0006C-3C4D-4280-81CC-B7D7F7E44703/elements
// 返回数据
{
"value":{
"ELEMENT":"44B21850-8824-44F2-9208-AEDCCD313F75"
},
"sessionId":"B1C0006C-3C4D-4280-81CC-B7D7F7E44703",
"status":0
}
6. 点击控件(click)
// curl命令
curl -X POST $JSON_HEADER -d "" $DEVICE_URL/session/$SESSION_ID/element/5/click
// 举个例子
curl -X POST $JSON_HEADER -d "" http://169.254.81.109:8100/session/9A354D8F-052B-4A3F-9888-375F45AE11A1/element/0359FE45-3575-4D6F-B6EE-682028AFEC47/click
// 返回数据
{
"status":0,
"id":"BA7CD7DF-E894-4947-963D-6AC935ACF483",
"value":"",
"sessionId":"E2431B5B-C14D-409C-890D-A9F2320958CE"
}
7. 将字符串写入文本框
// curl命令:逐个字符输入
curl -X POST $JSON_HEADER -d "{\"value\":[\"h\",\"e\",\"l\",\"l\",\"o\",\" \",\"w\",\"o\",\"r\",\"l\",\"d\"]}" $DEVICE_URL/session/$SESSION_ID/element/5/value
// curl命令:一次输入整个字符串
curl -X POST $JSON_HEADER -d "{\"value\":\"hello world\"}" $DEVICE_URL/session/$SESSION_ID/element/5/value
// 举个例子
curl -X POST $JSON_HEADER -d "{\"value\":\"hello world\"}" http://169.254.81.109:8100/session/E0F1C5FB-0BD4-4139-A868-428B9A6E3340/element/415BE367-F59A-437F-8515-B149E25831F3/value
// 返回数据
{
"status":0,
"id":"415BE367-F59A-437F-8515-B149E25831F3", // 操作的文本框的elementId
"value":"",
"sessionId":"E0F1C5FB-0BD4-4139-A868-428B9A6E3340"
}
8. 清除文本框内容
// curl命令
curl -X POST $JSON_HEADER -d "" $DEVICE_URL/session/$SESSION_ID/element/5/clear
// 举个例子
curl -X POST $JSON_HEADER -d "" http://169.254.81.109:8100/session/E0F1C5FB-0BD4-4139-A868-428B9A6E3340/element/415BE367-F59A-437F-8515-B149E25831F3/clear
// 返回数据
{
"status":0,
"id":"415BE367-F59A-437F-8515-B149E25831F3",
"value":"",
"sessionId":"E0F1C5FB-0BD4-4139-A868-428B9A6E3340"
}
9. Force Touch(指定控件)
// curl命令
curl -X POST $JSON_HEADER -d "{\"pressure\":\"0.8\",\"duration\":\"2.0\"}" $DEVICE_URL/session/$SESSION_ID//wda/element/forceTouchByCoordinate/:uuid
// 举例子
curl -X POST $JSON_HEADER -d "{\"pressure\":\"0.8\",\"duration\":\"2.0\"}" http://169.254.81.109:8100/session/895B7FD8-A4D5-4D30-BABA-1B2072508CF2/wda/element/forceTouch/0885D0F8-1E64-4B9B-901E-CBEE2CB7F856
// 返回数据
{
"value":{
},
"sessionId":"B1C0006C-3C4D-4280-81CC-B7D7F7E44703",
"status":0
}
9. Force Touch(指定控件&坐标)
10. 设定值(PickerWheel、Slider验证可以)
// curl命令
curl -X POST $JSON_HEADER -d "" $DEVICE_URL/session/$SESSION_ID/element/5/value
// 举个例子
// 如果是PickerWheel,value可以传单个字符串、字符串数组,其中字符串数组在WDA内部传参时会用空格分隔开再赋值给PickerWheel
// 如果是Slider,要求 0.0 <= value <= 1.0
curl -X POST $JSON_HEADER -d "{\"value\":"0.5"}" http://169.254.81.109:8100/session/2C95EB07-DE96-4A4E-BEA0-3075197AC2ED/element/49B852A6-9C5C-4AB6-B3B0-CF02912794CC/value
// 返回数据
{
"value":{
},
"sessionId":"2C95EB07-DE96-4A4E-BEA0-3075197AC2ED",
"status":0
}
待验证的接口
// Fail:根据bundleId、app参数安装应用
// 原因:app参数格式未知
curl -X POST $JSON_HEADER \
-d "{\"desiredCapabilities\":{\"bundleId\":\"com.apple.mobilesafari\", \"app\":\"[host_path]/magicapp.app\"}}" \
$DEVICE_URL/session
// 移除会话并退出应用
curl -X DELETE $JSON_HEADER $DEVICE_URL/session/$SESSION_ID
// 返回主屏幕
curl -X POST $JSON_HEADER -d "" $DEVICE_URL/wda/homescreen
// 屏幕截图
curl -X GET $JSON_HEADER $DEVICE_URL/screenshot
// 超出时间停止应用
curl -X POST $JSON_HEADER -d "{\"duration\":3}" $DEVICE_URL/session/$SESSION_ID/wda/deactivateApp