如何使用appium在android上自动化浏览器?

时间:2014-02-24 02:37:55

标签: android browser appium

我想使用appium在Android手机上自动化浏览器,但我不知道如何设置功能。<​​/ p>

首先,我已在开发者选项中的Android设备上启用USB调试。

其次,adb运行良好,我可以看到设备ID。

第三,我从Appium for Windows启动了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;
        }
    }
}

非常感谢。

4 个答案:

答案 0 :(得分:4)

使用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();
}

注意:使用selenium版本2.48.2(最新版本)使android驱动程序运行时没有任何错误。

答案 1 :(得分:0)

尝试下一步:

@Before
public void setUp() throws Exception {
    DesiredCapabilities  capabilities = new DesiredCapabilities();
    capabilities.setCapability("device","Android");
    capabilities.setCapability("app", "Chrome");
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
    capabilities.setCapability(CapabilityType.VERSION, "4.3");
    capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

它应该是Chrome驱动程序的午餐。

答案 2 :(得分:0)

您可以尝试使用此功能类型: CapabilityType.BROWSER_NAME ---“浏览器”

这段代码正在我的结束:

@BeforeMethod
public void setUp() throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "Samsung Galaxy S4");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "Browser");
    capabilities.setCapability("platformVersion", "4.4.4");
    capabilities.setCapability(MobileCapabilityType.TAKES_SCREENSHOT, "true");
    driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("http://www.google.com");
}

答案 3 :(得分:0)

试试这个

            DesiredCapabilities mobCapabilities = new DesiredCapabilities();

            mobCapabilities.SetCapability(CapabilityType.BrowserName,
                AppConfig.MobileBrowser.Equals("chrome") ? MobileBrowserType.Chrome : MobileBrowserType.Browser);
            mobCapabilities.SetCapability(MobileCapabilityType.DeviceName, AppConfig.DeviceName);
            mobCapabilities.SetCapability(MobileCapabilityType.PlatformName, AppConfig.Platform);
            mobCapabilities.SetCapability(MobileCapabilityType.PlatformVersion, AppConfig.PlatformVersion);

 // init driver
            driver = new AndroidDriver<AndroidElement>(
                new Uri("http://127.0.0.1:4723/wd/hub"),
                mobCapabilities);
    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));