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
- Android Device USB Drivers
- ADB/Fastboot Tool (Either the Minimal ADB and Fastboot or provided by the Android SDK)
- Google ChromeDriver binary.
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.
Leave a Reply