appium之Context切換


原文地址http://blog.csdn.net/Mandypan/article/details/51396302

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);
}

}

 

Context切換
driver.getContextHandles();
driver.context("WEBVIEW");
driver.context("NAVTIVEAPP");


查看當前所有的窗口:
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");

 

頁面切換

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(); //關閉當前窗口
      }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM