Android Webview的测试

1.查看当前的所有窗口:

        Set contexts= driver.getContextHandles();

 

        System.out.println(contexts);

打印出当前所有的窗口

 

 

Set<String> contextNames = driver.getContextHandles();

            for (String contextName : contextNames) {

                 print(contextName);

                 if (contextName.contains("WEBVIEW")) {

                          driver.context(contextName);

}

}

 

 

2.切换到webview

 

        for(int i=0;i<=contexts.size();i++)

        {   

        String s=(String) contexts.toArray()[i];

        if(s.contains("WEBVIEW"))

        {

             System.out.println(s);

             System.out.println("准备切换到webview");

             //driver.context("s"); // set context to WEBVIEW_1

             driver.switchTo().window(s);

          }

      }

 

 

 

多个webview的情况:

 选择最终打开的webview即可,switch to 到目标windowsHandle即可

Set<StringcontextNames driver.getContextHandles();

         for (String contextName contextNames) {

                       print(contextName);

                       if (contextName.contains("com.kascend.chushou")) {

                                driver.context(contextName);

                                Thread.sleep(500);

                                Set<StringwindowsHandles driver.getWindowHandles();

                                for (String windowsHandle windowsHandles{

                                          print("windowsHandle名: " windowsHandle);

                                          driver.switchTo().window(windowsHandle);

                   }

              }

}

 

 

 

3.切换回主程序:

        driver.context("NATIVE_APP");

 

 

 

获取不到webview的原因:

1、系统不是 4.4 及以上(4.4以下的 webview 没有使用 chrome 内核,appium 只有在 selendroid 模式下支持)
2、使用的不是系统的 webview 。webview 内核可以有很多的,而且很多应用(例如微信)出于兼容性或者其他原因会使用第三方内核(微信用的是腾讯自家的 QQ 浏览器内核)。但 appium 目前只支持系统内置的 webview 。
3、应用没有开启 webview 的 debug 选项。在模拟器上由于使用的是开发版系统,所以默认是打开了 debug 的,但真机除了亲儿子基本默认都关闭,需要增加代码来打开。详细可以看官方文档:https://developer.chrome.com/devtools/docs/remote-debugging#debugging-webviews

 

posted @ 2015-10-16 09:58  melody_emma  阅读(779)  评论(0编辑  收藏  举报