本文介绍了如何在Android上使用appium自动化浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用appium来自动化android手机上的浏览器,但是我不知道如何设置功能.

首先,我在开发人员选项中启用了Android设备上的USB调试功能.

第二,adb运行良好,我可以看到设备ID.

第三,我从Windows的Appium启动了Appium.exe,并通过JAVA编写了一些代码,但是我不知道如何在Android浏览器上设置该功能.

public class Test {
    private WebDriver driver;

    @Before
    public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //------------I don't know how to set the capability------------//
        capabilities.setCapability(CapabilityType.VERSION, "2.3.7");
        capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
        //--------------------------------------------------------------//
        driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @Test
    public void testcase_001() throws Exception{
        driver.get("http://www.google.com");
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lst-ib")));
        WebElement keyword = driver.findElement(By.name("lst-ib"));
        keyword.sendKeys("appium");
        driver.findElement(By.id("btnK")).click();
        Thread.sleep(5000);
     }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
        private RemoteTouchScreen touch;
        public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
            super(remoteAddress, desiredCapabilities);
            touch = new RemoteTouchScreen(getExecuteMethod());
        }
        public TouchScreen getTouch() {
            return touch;
        }
    }
}

非常感谢.

解决方案

使用Android驱动程序尝试以下代码:

import io.appium.java_client.android.AndroidDriver;

public class Test {
private AndroidDriver;

@Before
public void setUp() throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "MOTO G 2");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
    capabilities.setCapability("platformVersion", "5.0.2");
    capabilities.setCapability("appPackage", "com.android.chrome");
    capabilities.setCapability("appActivity","com.google.android.apps.chrome.ChromeTabbedActivity");
    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testcase_001() throws Exception{
    driver.get("http://www.google.com");

    WebElement keyword = driver.findElementByName("q");
    keyword.sendKeys("appium");
    driver.findElement(By.id("btnK")).click();
    Thread.sleep(5000);
 }

@After
public void tearDown() throws Exception {
    driver.quit();
}

注意:使用硒版本2.48.2(最新版本)可使android驱动程序正常运行.

I want to use appium to automate browser on android phone,but I don't know how to set the capability.

First, I have enabled USB debugging on my Android device in the developer options.

Second, adb was working well, i can see the device id.

Third, I started Appium.exe from Appium for windows and writed some code by JAVA, but I don't know how to set the capability on Android browser.

public class Test {
    private WebDriver driver;

    @Before
    public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //------------I don't know how to set the capability------------//
        capabilities.setCapability(CapabilityType.VERSION, "2.3.7");
        capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
        //--------------------------------------------------------------//
        driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @Test
    public void testcase_001() throws Exception{
        driver.get("http://www.google.com");
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lst-ib")));
        WebElement keyword = driver.findElement(By.name("lst-ib"));
        keyword.sendKeys("appium");
        driver.findElement(By.id("btnK")).click();
        Thread.sleep(5000);
     }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
        private RemoteTouchScreen touch;
        public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
            super(remoteAddress, desiredCapabilities);
            touch = new RemoteTouchScreen(getExecuteMethod());
        }
        public TouchScreen getTouch() {
            return touch;
        }
    }
}

Many thanks.

解决方案

Try this code using Android Driver:

import io.appium.java_client.android.AndroidDriver;

public class Test {
private AndroidDriver;

@Before
public void setUp() throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "MOTO G 2");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
    capabilities.setCapability("platformVersion", "5.0.2");
    capabilities.setCapability("appPackage", "com.android.chrome");
    capabilities.setCapability("appActivity","com.google.android.apps.chrome.ChromeTabbedActivity");
    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testcase_001() throws Exception{
    driver.get("http://www.google.com");

    WebElement keyword = driver.findElementByName("q");
    keyword.sendKeys("appium");
    driver.findElement(By.id("btnK")).click();
    Thread.sleep(5000);
 }

@After
public void tearDown() throws Exception {
    driver.quit();
}

Note: Use selenium version 2.48.2 (latest version) to make the android driver run without any error.

这篇关于如何在Android上使用appium自动化浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 10:06