Selenium ChromeDriver for Android

Selenium is well known with automation testing of web applications, but a lot of businesses are moving their attention away from the desktop and onto the mobile platform.

The Selenium ChromeDriver allows Selenium tests to be run in a Chrome browser. The best part, is that the Chrome browser on Android devices are compatible.

The following is a quick start guide to allow Selenium tests to be executed on an Android device with Chrome.

Requirements

1. On the Android device, navigate to the Developer Options and turn on Android debugging mode.

2. Connect the device to the computer.

3. Open a Command Prompt and run the following adb command to list the connected devices.

> adb devices

4. Take note of the device serial represented by a series of characters and digits, e.g. ZX9G355SPR .

If the device is not listed, ensure the Android USB drivers are installed correctly, restart both the device and computer and try again.

Also make sure when installing the the Android USB drivers that MTP is enabled.

5. Create a new Java project that has the Selenium libraries included.

6. Add the following to setup the ChromeDriver to open the session on the connected device.

public WebDriver getMobileChromeDriver() {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption(
"androidPackage", "com.android.chrome");
chromeOptions.setExperimentalOption("androidDeviceSerial", 
"android-device-id");
return new ChromeDriver(chromeOptions);
}

Ensure that the path to the chromedriver.exe is set and the device serial is set for the androidDeviceSerial option.

7. Create a test and set the Selenium WebDriver to the defined ChromeDriver instance.

@Test
public void test() {
WebDriver driver = getMobileChromeDriver();
driver.get("http://www.google.com");
}

8. Run the test and a Chrome browser session will open on the device and navigate to Google.

Now tests written using Selenium can be run seamlessly on the desktop and the mobile device without any additional modifications.


Posted

in

by

Comments

One response to “Selenium ChromeDriver for Android”

  1. Neha Avatar
    Neha

    HI there,
    I followed the test to launch the chrome browser in my mobile device ,but I am getting the following error:
    ChromeDriver was started successfully.
    [java] org.openqa.selenium.WebDriverException: unknown error: Failed to clear data for com.android.chrome on device : Error: java.lang.SecurityException: PID 2127 does not have permission android.permission.CLEAR_APP_USER_DATA to clear data of package com.android.chrome2021-05-02 19:48:55,504 ERROR – Exception thrown while opening browser in WebdriverCommonFixtures
    [java] Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:48’

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.