真机 app 测试
真机app测试和模拟器app测试,区别在于需要签名证书、需要在DesiredCapabilities设置签名
DesiredCapabilities代码设置如下:
DesiredCapabilities caps=new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
caps.setCapability(MobileCapabilityType.UDID, "DDAC13B0-786D-4DC7-A920-4BEAF56CD616");
caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "xcuitest");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "ios");
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone6s");
//不涉及编译不需要配置这个
caps.setCapability("xcodeOrgId", "96NJEQL7Y2");
caps.setCapability("xcodeSigningId", "iPhone Developer");
driver=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
因为真机器测试需要签名wda,所以需要声明xcodeOrgId和xcodeSigningId.其中xcodeOrgId
是开发者签名证书编号id,xcodeSigningId一般固定为iPhone Developer
真机浏览器测试
ios-webkit-debug-proxy 安装
google的safari调试中转工具
https://github.com/google/ios-webkit-debug-proxy
需要先安装libimobiledevice工具
安装命令:brew install libimobiledevice --HEAD
安装命令:brew install ios-webkit-debug-proxy
ios-webkit-debug-proxy 使用
修改 udid 为目标设备的 udid 并确认 remote-debugger 使用 27753 端口。
idevice_id -l可以获取udid
开启代理:ios_webkit_debug_proxy -c 0e4b2f612b65e98c1d07d22ee08678130d345429:27753 -d
查看版本号:ios_webkit_debug_proxy --v
真机设置 Safari 的 Web 检查器
需要在真机上开启Web检查器,具体路径:设置-Safari-高级-打开Web检查器
appium 使用 ios_webkit_debug_proxy 遇到问题
在appium log中有一段日志是自动开启ios_webkit_debug_proxy代理
Starting ios_webkit_debug_proxy at port 27753 on device 86616cbaa40e52d3f9236ec982dd6f1e933a44bd
2018-08-20 02:34:34:450 - info: [debug] [BaseDriver] Event 'iwdpStarted' logged at 1534732474450 (10:34:34 GMT+0800 (CST))
2018-08-20 02:34:34:451 - info: [debug] [XCUITest] Started ios_webkit_debug proxy server at: http://localhost:27753
到了后面就开启超时了,导致脚本挂了
TimeoutError: operation timed out
2018-08-20 02:34:42:498 - error: [XCUITest] at afterTimeout (/usr/local/lib/node_modules/appium/node_modules/bluebird/js/release/timers.js:46:19)
2018-08-20 02:34:42:499 - error: [XCUITest] at Timeout.timeoutTimeout [as _onTimeout] (/usr/local/lib/node_modules/appium/node_modules/bluebird/js/release/timers.js:76:13)
解决方案:npm install -g appium@beta,升级appium版本
单独执行命令:ios_webkit_debug_proxy -c 86616cbaa40e52d3f9236ec982dd6f1e933a44bd:27753 -d
报错:Could not connect to lockdownd. Exiting
解决方案:
brew update
brew reinstall --HEAD libimobiledevice
brew reinstall -s ios-webkit-debug-proxy
正常的appium启动ios_webkit_debug_proxy日志,会得到Responding响应结果
[iOS] Starting ios_webkit_debug_proxy at port 27753 on device 86616cbaa40e52d3f9236ec982dd6f1e933a44bd
2018-08-20 03:50:08:543 - info: [debug] [BaseDriver] Event 'iwdpStarted' logged at 1534737008543 (11:50:08 GMT+0800 (CST))
2018-08-20 03:50:08:543 - info: [debug] [XCUITest] Started ios_webkit_debug proxy server at: http://localhost:27753
2018-08-20 03:50:08:545 - info: [Appium] New XCUITestDriver session created successfully, session a8ebd45b-bc11-4eee-96ad-a572b0dcb558 added to master session list
2018-08-20 03:50:08:545 - info: [debug] [BaseDriver] Event 'newSessionStarted' logged at 1534737008545 (11:50:08 GMT+0800 (CST))
2018-08-20 03:50:08:546 - info: [debug] [W3C] Responding to client with driver.createSession() result:
{"capabilities":{"webStorageEnabled":false,"locationContextEnabled":false,"browserName":"","platform":"MAC",
"javascriptEnabled":true,"databaseEnabled":false,"takesScreenshot":true,"networkConnectionEnabled":false,
"platformName":"ios","app":"com.iOS.Demo.xinxi","automationName":"xcuitest","deviceName":"iPhone
sim","startIWDP":true,"udid":"86616cbaa40e52d3f9236ec982dd6f1e933a44bd","usePrebuiltWDA":true}}
appium 中 iOS WebKit 调试代理文档
http://appium.io/docs/cn/advanced-concepts/ios-webkit-debug-proxy/
DesiredCapabilities 设置
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
desiredCapabilities.setCapability(MobileCapabilityType.UDID, udid);
desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "xcuitest");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "ios");
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone sim");
desiredCapabilities.setCapability("startIWDP", true);
URL url = new URL("http://127.0.0.1:4723/wd/hub");
AppiumDriver driver = new AppiumDriver(url, desiredCapabilities);
startIWDP表示开启ios-webkit-debug-proxy工具
简单业务代码
业务代码和seleium定位元素、操作元素基本相同
// 浏览网页和定位页面元素取得id。
driver.get("http://www.baidu.com");
Thread.sleep(2000);
driver.findElement(By.id("index-kw")).sendKeys("tester");
driver.findElement(By.id("index-bn")).click();
Thread.sleep(2000);
真机测试 app 中的 webview 测试
切换 webview
iOS webview测试和Android测试webview测试类似,进入到web页面后,需要从原生页面切换到切换webview.
ios_webkit_debug_proxy 代理
依然需要开启ios_webkit_debug_proxy代理工具
简单代码如下
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.UDID, udid);
desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "xcuitest");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "ios");
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone sim");
desiredCapabilities.setCapability("app", "com.iOS.Demo.xinxi");
desiredCapabilities.setCapability("startIWDP", true);
desiredCapabilities.setCapability("usePrebuiltWDA", true); //不再次安装WDA
AppiumDriver driver=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put("direction", "down");
scrollObject.put("name", "Web View");
js.executeScript("mobile: scroll", scrollObject);
//点击Web View
driver.findElementByAccessibilityId("Web View").click();
Thread.sleep(10000);
//打印原生page source
System.out.println(driver.getPageSource());
//切换到最后一个context,一般是webview
driver.context(driver.getContextHandles().toArray()[1].toString());
//打印webview的page source
System.out.println(driver.getPageSource());
//用xpath定位找元素点击
driver.findElement(By.xpath("//*[@id=\"section-1-hero-position-1\"]/div/div[2]/a[1]")).click();
safari 调试 iPhone app web 页面
1.在查看app中的web页面元素,通常用appium desktop工具,但是这个工具缺点在于,启动慢、展示页面元素也慢
2.pc上safari浏览器可以实时查看app中的web页面元素,仅仅需要打开safari浏览器中的开发选项.这个功能类似Android的chrome的inspect功能.
具体使用文档如下:https://blog.csdn.net/guoyuyanmen/article/details/52485439