Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use appium to automate browser on 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.

like image 772
Terry Avatar asked Feb 24 '14 02:02

Terry


People also ask

Will Appium work for mobile browser automation?

In a nutshell, Appium is a mobile test automation framework (with a tool) that works for all: native, hybrid and mobile web apps for iOS and Android. Appium is a great choice for test automation framework as it can be used for all these different app/web types.

What mobile web browsers can I automate in the Android emulator?

Currently the only browser that can be automated in our Android emulators is the stock browser (i.e Browser). The Android stock browser is an Android flavor of 'chromium' which presumably implies that its behavior is closer to that of Google Chrome.

How do I switch to browser in Appium?

For android just using tap on phone's back button (using key event) should do: driver. pressKey(new KeyEvent(AndroidKey. BACK)); For iOS; after tapping on deep link it takes to safari browser.


1 Answers

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.

like image 176
Thirunavukarasu Paramasivam Avatar answered Sep 26 '22 19:09

Thirunavukarasu Paramasivam