之前研究了一段時間的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']"