原文地址http://blog.csdn.net/Mandypan/article/details/51396302java
Context切換
driver.getContextHandles();//獲取頁面的context
driver.context("WEBVIEW");
driver.context("NATIVEAPP");
eg:
public void GetResource_Android(){
set<String> contextSet=wd.getContextHandles();
for(String context : contextSet){
System.out.println(context);
}android
}windows
Context切換
driver.getContextHandles();
driver.context("WEBVIEW");
driver.context("NAVTIVEAPP");this
查看當前全部的窗口:
Set<String> contextNames=driver.getContextHandles();
System.out.print(contextNames);
切換到Webview
<pre name="code"class="java">driver.context("WEBVIEW");
driver.findElementByID("wd");
切換到NativeAPP
<pre name="code" class="java">driver.context("NATIVE_APP");
demo
/**
* Switch to NATIVE_APP or WEBVIEW
* @param sWindow window name
*/
private void switchToWindow(String sWindow) {
LogManager.getLogger(this.getClass()).info("Swith to window: " + sWindow);
Set<String> contextNames = driver.getContextHandles();
LogManager.getLogger(this.getClass()).info("Exists windows: " + contextNames.toString());
for (String contextName : contextNames) {
if (contextName.contains(sWindow)) {
driver.context(contextName);
break;
}
}
}
switchToWindow("WEBVIEW_com.test.android");
switchToWindow("NATIVE_APP");url
頁面切換spa
String pWindUrl = driver.getCurrentUrl(); 獲取當前窗口的url
//跳至新打開的窗口
if (driver.getWindowHandles().size() != 2) {fail(); }
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle); 遍歷跳轉到每個打開的窗口
if (driver.getCurrentUrl().equals(pWindUrl)){ 若是這個窗口的url是pWindUrl則代表是當前的窗口
driver.close(); //關閉當前窗口
}.net
}code