• appium定位WebView页面元素


    使用AndroidDriver/IOSDriver对象的getContextHandles()方法得到所有上下文句柄,其中原生app的句柄会有"NATIVE_APP"字样,webview句柄会有"WEBVIEW"字样。

    确定了webview句柄后,使用对象的context()方法进入webview,如果想返回原生app,使用context()方法,传入原生app句柄即可。

    然后再通过定位方法定位元素。

    安卓4.4之前,需要使用Selendroid,具体代码如下(摘自Appium API文档 https://testerhome.com/topics/3144):

     1 //配置 webdriver 并启动 webview 应用。
     2 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
     3 desiredCapabilities.setCapability("device", "Selendroid");
     4 desiredCapabilities.setCapability("app", "/path/to/some.apk");  
     5 URL url = new URL("http://127.0.0.1:4723/wd/hub");
     6 RemoteWebDriver remoteWebDriver = new RemoteWebDriver(url, desiredCapabilities);
     7 
     8 // 切换到最新的web视图
     9 remoteWebDriver.switchTo().window("WEBVIEW");
    10 
    11 //在 guinea-pig 页面用 id 和 元素交互。
    12 WebElement div = remoteWebDriver.findElement(By.id("i_am_an_id"));
    13 Assert.assertEquals("I am a div", div.getText()); //验证得到的文本是否正确。
    14 remoteWebDriver.findElement(By.id("comments")).sendKeys("My comment"); //填写评论。
    15 
    16 //离开 webview,回到原生应用。
    17 remoteWebDriver.switchTo().window("NATIVE_APP");
    18 
    19 //关闭应用。
    20 remoteWebDriver.quit();
  • 相关阅读:
    HTML学习笔记1
    hadoop的eclipse连接-PC端
    eclipse的菜单栏消失问题解决
    Hadoop的wordcount代码实现
    《机器学习》阅读进度记录
    《金粉世家》
    ubuntu下构建服务器环境-PC 端
    ubuntu安装chrome-PC端
    Discovering-论文
    矩阵取数游戏【NOIP】
  • 原文地址:https://www.cnblogs.com/test-researcher/p/6676686.html
Copyright © 2020-2023  润新知