One of the core principles of Appium is that you shouldn’t have to change your app to test it. In line with that methodology, it is possible to test hybrid web apps (e.g., the UIAWebView elements in an iOS app) the same way you can with Selenium for web apps. There is a bit of technical complexity required so that Appium knows whether you want to automate the native aspects of the app or the web views, but thankfully, we can stay within the WebDriver protocol for everything.css
Here are the steps required to talk to a web view in your Appium test://测试混合应用的步骤html
context
again with the native context id to leave the web frame.1 // java 2 // assuming we have a set of capabilities 3 driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 4 5 Set<String> contextNames = driver.getContextHandles(); 6 for (String contextName : contextNames) { 7 System.out.println(contextNames); //prints out something like NATIVE_APP \n WEBVIEW_1 8 } 9 driver.context(contextNames.toArray()[1]); // set context to WEBVIEW_1 10 11 //do some web testing 12 String myText = driver.findElement(By.cssSelector(".green_button")).click(); 13 14 driver.context("NATIVE_APP"); 15 16 // do more native testing if we want 17 18 driver.quit();
1 # python 2 # assuming we have an initialized `driver` object for an app 3 4 # switch to webview 5 webview = driver.contexts.last 6 driver.switch_to.context(webview) 7 8 # do some webby stuff 9 driver.find_element(:css, ".green_button").click 10 11 # switch back to native view 12 driver.switch_to.context(driver.contexts.first) 13 14 # do more native testing if we want 15 16 driver.quit()
Appium comes with built-in hybrid support via Chromedriver. Appium also uses Selendroid under the hood for webview support on devices older than 4.4. (In that case, you’ll want to specify "automationName": "selendroid"
as a desired capability).//低于4.4的安卓版本,须要用Selendroid测试混合应用。其他的版本,经过内置的Chromedriver测试混合应用。java
Make sure setWebContentsDebuggingEnabled is set to true as described in the remote debugging docs.//若是无法访问,就看这里。python
Once you’ve set your desired capabilities and started an appium session, follow the generalized instructions above.android
To interact with a web view appium establishes a connection using a remote debugger. When executing against a simulator this connection is established directly as the simulator and the appium server are on the same machine.//同一个电脑上的iOS模拟器与appium server会直接创建链接。ios
Once you’ve set your desired capabilities and started an appium session, follow the generalized instructions above.git
When executing against a real iOS device appium is unable to access the web view directly. Therefore the connection has to be established through the USB lead. To establish this connection we use the ios-webkit-debugger-proxy.//iOS真机经过代理与appium server取得链接。github
For instruction on how to install and run ios-webkit-debugger-proxy see iOS webkit debug proxy documentation.web
Now you can start an appium test session and follow the generalized instructions above.chrome