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']"

appium for mobile web 之使用 ChromeDriver

上一篇:history命令显示出详细时间


下一篇:Redis 6.0 新特性 ACL 介绍