Running tests on an Android simulator


The android driver allows us to execute our tests against an Android browser. This can be a simulator or a real device. This recipe will walk us through the steps we have to do to run our tests in a simulator.


Before we can register our simulator we have to download the android SDK (Software Development Kit) from the following location:


We can divide this section into three parts: setup the emulator, install the WebDriver APK and finally run the test.

Setup the emulator

Navigate to the tools directory and create an Android Virtual Device.

$cd ~/android_sdk/tools/
$./android create avd -n my_android -t 12 -c 100M

-n: specifies the name of the AVD -t: specifies the platform target -c: specifies the SD card storage space

We can list the targets with the following command to check if the creation succeeded:

./android list targets

Finally we can start the emulator with the following command:

$./emulator -avd my_android &

Install the WebDriver APK

  1. We need to retrieve the serial id with the following command: /android_sdk/platform-tools/adb devices
  2. Download the Android server from and save it in the platform-tools directory. To install the application enter: $./adb -s <serialId> -e install -r android-server.apk
  3. Start the Android WebDriver application,by running this command: $./adb -s <serialId> shell am start -a android.intent.action.MAIN -n
  4. Now we need to setup the port forwarding in order to forward traffic from the host machine to the emulator. Enter the following in the terminal: $./adb -s <serialId> forward tcp:8080 tcp:8080

Run the test

Now we have our environment setup we can run our tests. The test will look like this:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileAndroidDevice {
    private static WebDriver driver;
    public void setUp() throws Exception {
        driver = new AndroidDriver();
    public void tearDown() {
    public void measurePerformance() throws InterruptedException {
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
        Assert.assertTrue(searchHeader.contains("ipod nano"));