Appium怎样驱动手机Chrome浏览器

时间:2024-05-19 07:25:53

Appium怎样驱动手机Chrome浏览器:

Appium驱动手机Chrome浏览器与Selenium驱动Web端的Chrome浏览器差不多;

Appium怎样驱动手机Chrome浏览器


import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class ChromeAppTest {
    private AppiumDriver driver;

    @BeforeMethod
    public void beforeMethod() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName","Android");
        //Mobile OS类型
        capabilities.setCapability("platformVersion","7.1.1");
        //Mobile OS版本
        capabilities.setCapability("deviceName","3216e430");
        //adb devices
        capabilities.setCapability("browserName","Chrome");
        //chrome
        capabilities.setCapability("appPackage","com.android.browser");
        //chrome的包名
        capabilities.setCapability("appActivity",".BrowserActivity");
        //chrome的启动页
        capabilities.setCapability("unicodeKeyboard",true);
        //支持中文输入,默认false
        capabilities.setCapability("resetKeyboard",true);
        //重置输入法为系统默认
        capabilities.setCapability("noReset",true);
        //不重新安装apk
        capabilities.setCapability("noSign",true);
        //不重新签名apk

        URL url = new URL("http://127.0.0.1:4723/wd/hub");
        driver = new AndroidDriver(url, capabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }

    @Test
    public void chromeApp() throws InterruptedException {
        String url = "https://m.taobao.com";
        //手机淘宝H5
        driver.get(url);
        sleep(2000);
        driver.findElementById("search-placeholder").click();
        //点击淘宝搜索框
        sleep(2000);
        driver.findElementByName("q").sendKeys("华硕官方旗舰店");
        sleep(2000);
        driver.findElementByClassName("icons-search").click();
        sleep(2000);

        Assert.assertTrue(driver.getPageSource().contains("asus华硕官方旗舰店"));
    }

    @AfterMethod
    public void afterMethod(){
        driver.closeApp();
        driver.quit();
    }
}