3-4 1449 web view


native app|纯原声app,Android用Java些,iOS用object c写
----------|---------------------------------------
hybrid app|套用原声应用的外壳,既有原生的UI页面,又通过内置web view组件(相当于内置浏览器),可以访问本地和远程的html文件,Native Hardware Access可以访问本地文件和通过js脚本或html调用本地的函数方法。
web app|只靠浏览器访问使用
微信小程序|微信内层嵌入的组件

模拟器默认支持
真机需要打开app内开关

//必须从您的应用启用web view调试。要启用web view调试,请在web view类上调用静态方法setWebContentsDebuggingEnabled。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES>KITKAT){
    WebView.setWebContentDebuggingEnabled(true)
}
#代码有错误
# 切换到web view
webview = driver.contexts.last
driver.switch_to.context(webview)
#执行操作
driver.find_element(:css,".green_button").click
#切回原生页面
driver.switch_to.context(driver.contexts.first)
driver.quit()

       @Test
    public void testWebview() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver,10);
        WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text=\"交易\"]")));
        System.out.println("等待结束");
        driver.findElementByXPath("//*[@text=\"交易\"]").click();
        Thread.sleep(10000);
        Set<String> contextNames = driver.getContextHandles();
        System.out.println(contextNames);
        System.out.println(contextNames);
        System.out.println(contextNames);
        System.out.println(contextNames);
        System.out.println(contextNames);
        Thread.sleep(3000);
    }

模拟器

使用真机,不会打印web view

使用API demo

 @Test
    public void testWebview() throws InterruptedException{
        WebDriverWait wait = new WebDriverWait(driver,10);
        WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//android.widget.TextView[@text=\"Views\"]")));
        driver.findElementByXPath("//android.widget.TextView[@text=\"Views\"]").click();
        WebElement list = driver.findElement(By.id("android:id/text1"));
        MobileElement webview = list.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+"new UiSelector().text(\"WebView\"));"));
        //webview.click();
        driver.findElementByXPath("//android.widget.TextView[@text=\"WebView\"]").click();
        Thread.sleep(5000);
        System.out.println(driver.getContextHandles());
        System.out.println(driver.getContextHandles());
        System.out.println(driver.getContextHandles());
        System.out.println(driver.getContextHandles());
        Thread.sleep(5000);
    }

使用真机依然只输出

关于web view测试的内容等待补充

FAQ

1.红米note5使用全面屏,控件By.xpath("//*[@text="交易"]")可以找到,但是控件属性显示不可点击。
解决方法:取消全面屏,使用经典导航键。控件可以正常访问。

2.使用真机,driver.getContextHandles();没有打印web view,使用UC也没有打印

猜你喜欢

转载自www.cnblogs.com/csj2018/p/9783524.html
3-4