Yes, it’s true. With Selenium you can automate UI tests for Android browsers.
Validating how a web application behaves in multiple browsers is a growing need, as users require using any browser of their choice to consume applications. Moreover, this need spans to mobile devices: applications are demanded to be ubiquituous, and so our tests should be.
To my (pleasant) surprise, it’s very easy to run automated tests in Android browsers. Selenium includes an Android driver that supports most of the Android browsers, both simulated and in physical devices.
Continue reading to know more.
Selenium and Android
The main difference with running Selenium tests in an Android browser, is that the Android devices cannot, at least for now, participate in an existing Grid. For each device we will have a dedicated server and to access it we will need to configure a port forward between the PC where the device is attached and the device itself. This also applies to simulated devices.
Another limitation is that only the built-in Android browser is supported. At least for now, if you have installed Firefox or Opera in your droid they cannot be used to re-execute tests with Selenium.
First of all, you need to download and install the latest Android SDK from here. Our testing devices should be in ‘developer mode’: USB debugging must be enabled, mock locations must be allowed and the installation of applications outside the Market must be allowed. If you don’t know how to configure your devices, maybe these pages can be of help:
Once the device is ready, attach it to the PC with the USB cable. You should see, depending on Android version, an icon indicating that debugging is activated.
Installing the Selenium Android Server
Next, move to platform-tools subdir inside Android SDK installation, and execute this command:
This command will show a list of devices that are available. Identify the one you want to use to run the tests and take note of its id.
Now, it’s time to upload the server to the device. Download the server Android application from Selenium site here and execute the following command:
adb -s <device-id> -e install -r <path-to-selenium>/android-server-2.20.apk
Once installed, start the server with the following command:
adb -s <device-id> shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity
Finally, we need to expose this server to the Selenium clients running the tests adding a port forward. Identify a PC local port that is not in use and execute this command:
adb -s <device-id> forward tcp:<pc-port> tcp:8080
After the port forward is enabled, your tests can access the Android device by instantiating a remote driver to the PC by IP and port. The request will be forwarded to the server in the Android device, which will execute the tests and return the results back to the client.
You can even reuse existing tests to be executed in Android if you follow the best practice to pass a driver to a method (not annotated with JUnit @Tes) which contains the test logic:
DesiredCapabilities browser = DesiredCapabilities.android(); WebDriver driver = new RemoteWebDriver(new URL( "http://<pc-ip>:<pc-port>/wd/hub"), browser); testMethod(driver);
If you want to know more about setting up a Selenium Grid and writing tests with Selenium WebDriver, you can take a look at some of my previous posts:
Some Final Comments on Connectivity
For the tests to be successful, don’t forget that:
- The Android device must have visibility on the application under test.
- The Selenium client running the tests must have visibility on the PC where the Android device is attached to.
- The port forward between the PC and the Android device must be active.
And with these final comments we are done for now. Happy testing!