前面我们已经安装好了WebdriverAgent, 现在可以用Facebook官方提供的API来进行一些操作
WDA API官方页面:
如果在终端运行的话可以使用curl来调用API,需要搭配json_header + device_url + api组成,其中
json_header一般是: "Content-Type: application/json"
device_url一般是: http://localhost:8100/*
下面我们试试调用这些API
功能: 查询可服务状态(Checking Service Status)API: curl -X GET $JSON_HEADER $DEVICE_URL/status
使用示范:
Jackeys-MacBook-Pro:~ jackey$ curl -X GET -H "Content-Type: application/json" -d "" http://localhost:8100/status{ "value" : { "state" : "success", "os" : { "name" : "iOS", "version" : "11.2.1" }, "ios" : { "simulatorVersion" : "11.2.1", "ip" : "192.168.1.5" }, "build" : { "time" : "Dec 25 2018 11:48:43" } }, "sessionId" : "FB941AE9-551D-437E-A376-191DDDB354FB", "status" : 0}Jackeys-MacBook-Pro:~ jackey$
功能:打开会话并开启APP(Session handling)API: curl -X POST $JSON_HEADER -d "{\"desiredCapabilities\":{\"bundleId\":\"com.apple.mobilesafari\"}}" $DEVICE_URL/session
使用示范:
Jackeys-MacBook-Pro:~ jackey$ curl -XPOST -H "Content-Type: application/json" -d "{\"desiredCapabilities\":{\"bundleId\":\"NOVA.ProductDemo\"}}" http://localhost:8100/session{ "value" : { "sessionId" : "C7844276-1CB0-4165-9621-65B9B1E10E64", "capabilities" : { "device" : "iphone", "browserName" : "ProductDemo", "sdkVersion" : "11.2.1", "CFBundleIdentifier" : "NOVA.ProductDemo" } }, "sessionId" : "C7844276-1CB0-4165-9621-65B9B1E10E64", "status" : 0}Jackeys-MacBook-Pro:~ jackey$
功能:查询当前会话(Querying current session)API: curl -X GET $JSON_HEADER $DEVICE_URL/session/$SESSION_ID
使用示范:
Jackeys-MacBook-Pro:~ jackey$ curl -X GET -H "Content-Type: application/json" -d "" http://localhost:800/session/C7844276-1CB0-4165-9621-65B9B1E10E64{ "value" : { "sessionId" : "C7844276-1CB0-4165-9621-65B9B1E10E64", "capabilities" : { "device" : "iphone", "browserName" : "ProductDemo", "sdkVersion" : "11.2.1", "CFBundleIdentifier" : "NOVA.ProductDemo" } }, "sessionId" : "C7844276-1CB0-4165-9621-65B9B1E10E64", "status" : 0}Jackeys-MacBook-Pro:~ jackey$
功能:关闭会话并关闭程序(Removing session and kill application)API: curl -X DELETE $JSON_HEADER $DEVICE_URL/session/$SESSION_ID
使用范例:
Jackeys-MacBook-Pro:~ jackey$ curl -X DELETE -H "Content-Type: application/json" -d "" http://localhos:8100/session/C7844276-1CB0-4165-9621-65B9B1E10E64{ "value" : { }, "sessionId" : "406A1A5B-0164-4DA2-BFD9-051155F8B4B2", "status" : 0}Jackeys-MacBook-Pro:~ jackey$
功能:打开inspectorAPI: $DEVICE_URL/inspector
使用范例:
http://localhost:8100/inspector
功能:返回home screenAPI: curl -X POST $JSON_HEADER -d "" $DEVICE_URL/wda/homescreen
使用范例:
Jackeys-MacBook-Pro:~ jackey$ curl -X POST -H "Content-Type: application/json" -d“” http://localhost:8100/wda/homescreen{ "value" : "Error Domain=com.facebook.WebDriverAgent Code=1 \"Timeout waiting until SpringBoard is visible\" UserInfo={NSLocalizedDescription=Timeout waiting until SpringBoard is visible}", "sessionId" : "967066CF-E504-4DAA-B18D-86A0EF0AD53A", "status" : 13}Jackeys-MacBook-Pro:~ jackey$
功能:指定时间后退出app API: curl -X POST $JSON_HEADER -d "{\"duration\":3}" $DEVICE_URL/session/$SESSION_ID/wda/deactivateApp
更多参考: