appium for mobile web 之使用 ChromeDriver
之前研究了一段时间的appium for native app 相应的总结如下:
appium测试环境搭建 :http://www.cnblogs.com/tobecrazy/p/4562199.html
知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html
appium实现截图和清空EditText:http://www.cnblogs.com/tobecrazy/p/4592405.html
appium 滑动处理:http://www.cnblogs.com/tobecrazy/p/4612133.html
最近有人问我怎么使用web driver,所以特来研究一下
准备工作:1. 安装chrome
编写代码
package com.dbyl.core; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import io.appium.java_client.android.AndroidDriver; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.TimeUnit; public class baidu { AndroidDriver<WebElement> driver; /** * @author Young * @throws IOException */ public void startRecord() throws IOException { Runtime rt = Runtime.getRuntime(); // this code for record the screen of your device rt.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4"); } @BeforeClass(alwaysRun = true) public void setUp() throws Exception { // set up appium DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", "Android Emulator"); capabilities.setCapability("platformVersion", "4.4"); driver = new AndroidDriver<WebElement>(new URL( "http://127.0.0.1:4723/wd/hub"), capabilities); startRecord(); } @Test public void runChromeWebBrowser() { driver.get("http://www.baidu.com"); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); System.out.println(driver.getTitle()); snapshot((TakesScreenshot) driver, "before_search.png"); driver.findElementByXPath("//textarea[@id='index-kw']").sendKeys( "appium"); driver.findElement(By.xpath("//button[@id='index-bn']")).click(); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); System.out.println(driver.getTitle()); snapshot((TakesScreenshot) driver, "after_search.png"); Assert.assertTrue(driver.getTitle().contains("appium")); } /** * This Method create for take screenshot * * @author Young * @param drivername * @param filename */ public static void snapshot(TakesScreenshot drivername, String filename) { // this method will take screen shot ,require two parameters ,one is // driver name, another is file name String currentPath = System.getProperty("user.dir"); // get current work // folder File scrFile = drivername.getScreenshotAs(OutputType.FILE); // Now you can do whatever you need to do with it, for example copy // somewhere try { System.out.println("save snapshot path is:" + currentPath + "/" + filename); FileUtils .copyFile(scrFile, new File(currentPath + "\\" + filename)); } catch (IOException e) { System.out.println("Can't save screenshot"); e.printStackTrace(); } finally { System.out.println("screen shot finished, it's in " + currentPath + " folder"); } } @AfterClass public void afterTestStopDriver() { driver.quit(); } }
这其中,遇到输入框识别的问题,刚开始是使用UIAutomator
发现 web 的element 压根识别不了,最后想到这应该和selenium webdriver一样
所以在识别百度输入框,By xpath
"//textarea[@id='index-kw']"
转载请注明出处:http://www.cnblogs.com/tobecrazy/

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义