appium定位WebView页面元素
生活随笔
收集整理的這篇文章主要介紹了
appium定位WebView页面元素
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用AndroidDriver/IOSDriver對(duì)象的getContextHandles()方法得到所有上下文句柄,其中原生app的句柄會(huì)有"NATIVE_APP"字樣,webview句柄會(huì)有"WEBVIEW"字樣。
確定了webview句柄后,使用對(duì)象的context()方法進(jìn)入webview,如果想返回原生app,使用context()方法,傳入原生app句柄即可。
然后再通過定位方法定位元素。
?
安卓4.4之前,需要使用Selendroid,具體代碼如下(摘自Appium API文檔 https://testerhome.com/topics/3144):
1 //配置 webdriver 并啟動(dòng) webview 應(yīng)用。 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()); //驗(yàn)證得到的文本是否正確。 14 remoteWebDriver.findElement(By.id("comments")).sendKeys("My comment"); //填寫評(píng)論。 15 16 //離開 webview,回到原生應(yīng)用。 17 remoteWebDriver.switchTo().window("NATIVE_APP"); 18 19 //關(guān)閉應(yīng)用。 20 remoteWebDriver.quit();?
轉(zhuǎn)載于:https://www.cnblogs.com/test-researcher/p/6676686.html
總結(jié)
以上是生活随笔為你收集整理的appium定位WebView页面元素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 提高 webpack 构建 Vue 项目
- 下一篇: 不要在构造和析构函数中调用虚函数