appium for mobile web 之使用 ChromeDriver

來源:互聯網
上載者:User

標籤:

                    知乎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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.