Using Selenium to Automate Tests in Android Browser

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:

adb devices

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!

About these ads

19 thoughts on “Using Selenium to Automate Tests in Android Browser

  1. Hello, ive been using selenium in android for my task and i always get this exception :error-communicating-with-the-remote-browser-it-may-have-died, It appears before finding the element. Any idea how to solve this problem? Thanks :D

    • I suppose that basic troubleshooting is already done: verify the port forward from the pc where the android device is plugged to the device itself and verify that the process executing the test can access the pc where the device is and the port used to forward from. Other than that, it should work fine!

      • I’ve already forward it to 8080… i am using an emulator that time by the way. When i test it on a real device,my program worked but not in an emulator. I guess it’s fine since its already working. And Thank you for your reply :)

  2. My aim is to run tests in android emulator using selenium2 downloading android drivers Followed the same steps mentioned in the http://code.google.com/p/selenium/wiki/AndroidDriver a) JDK / JRE installed b) Android SDK (windows) is there c) Created AVD and got the 3.2 version OS target from the SDK manager d) Started the emulator and installed the selenium android server in the emulator ( installed webdriver APK ) e) Got Selenium standalone server installed 127.0.0.1:4444/wd/hub/static/resource/hub.html ( displayed sessions )

    f) Done the port forwarding adb -s emulator-5554 forward tcp:8080 tcp:8080 g) Ran this command adb -s emulator-5554 shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity h) My webdriver started displayed – Webdriver ready

    But the issue is Jetty not get started ( not displayed in emulator as Jetty started )

    And the android server not available at localhost:8080/wd/hub from the host machine. ( tried using ipaddress also .. no joy )

    When I ran my test by changing to driver = new AndroidDriver();

    Failure: SetUp : OpenQA.Selenium.WebDriverException : Unexpected error.

    Error 404 Not Found

    HTTP ERROR: 404 Problem accessing /hub/session. Reason: Not Found Powered by Jetty://

    TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

    Can anyone help why this is happening ? and solution for the same

  3. Can I ask my problem solving?

    I do setting the android web driver and it works.
    but when I use selenium grid with jenkins, I have a problem.

    —— Code Setting —————————————————————————————
    DesiredCapabilities browser = DesiredCapabilities.android();
    browser.setBrowserName(“android”);

    WebDriver driver = null;
    driver = new RemoteWebDriver(new URL(“http://192.168.254.233:4444/wd/hub”), browser);

    ——–Selenium Node———————————————————————————–
    http://192.168.254.230:5555 {platform=ANDROID, browserName=android, maxInstances=1}

    When I run this code, other browser(firefox) launches on PC.

    How can I control Android web driver using selenium grid with Jenkins.

    ref) there is no options for Android Driver in jenkins when register the node.

    • The Android browser does not work remotely as part of a hub:

      DesiredCapabilities browser = DesiredCapabilities.internetExplorer();
      WebDriver driver = new RemoteWebDriver(, browser);
      testMethod(driver, );

      WebDriver driver = new AndroidDriver();
      testMethod(driver, );

      You can see details in this post.

      • oops… I didn’t read this post in detail.
        I see that Android Driver doesn’t work as part of a hub
        Thanks your reply.

        but, when I use other port on PC, It doesn’t work.

        adb -s forward tcp:8080 tcp:8080 << it works
        adb -s forward tcp:8888 tcp:8080 << it doesn't work (error message is UnreachableBrowserException)

        Is it a configuration problem? How can I fix…

      • not sure what could be happening but the first place I would look is to firewall/proxy configuration. maybe you have something blocking port 8888?

  4. Selenium Grid test cases Running in Android Webdriver -By default it has launched Opera browser in windows

    Hi groups,

    While i try to run a Selenium Grid Functional test cases in android Webdriver ,by Default opera browser is launching in windows browesrs
    what i have done means
    1.i have installed Webdriver in Android Device (2.3.3)
    2.once web driver has installed i have clicked those webdriver it ” Shows WEBDRIVER is ready alert message.
    3.i have used these commands for adb connection
    4.i.adb kill-server
    ii.adb start-server
    iii.adb forward tcp:8080 tcp:8080 (is this correct?)

    and my Selenium Grid functional test cases.

    else if (selectedBrowser.equalsIgnoreCase(Constants.ANDROID_DRIVER)){
    try
    {
    log.info(“————-***LAUNCHING ANDROID_DRIVER***————–”);
    capabilities = new DesiredCapabilities();
    capabilities.setBrowserName(“android”);
    System.out.println(“———–checking in ANDROID_DRIVER——-”);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    and

    else if(selectedPlatform.equalsIgnoreCase(“ANDROID”)){
    capabilities.setCapability(CapabilityType.PLATFORM, Platform.ANDROID);
    }
    node.json

    {
    “capabilities”:
    [
    {
    "platform": "ANDROID",
    "browserName": "android",
    "maxInstances": 1,
    "seleniumProtocol": "WebDriver"
    }
    ],
    “configuration”:
    {
    “proxy”: “org.openqa.grid.selenium.proxy.DefaultRemoteProxy”,
    “maxSession”: 5,
    “port”: 1111,
    “host”: “localhost”,
    “register”: true,
    “registerCycle”: 5000,
    “hubHost”: localhost,
    “hubPort”: 4444
    }
    }

    TestNG file:

    so please suggest me to get clear this error!

    /regards
    Anbarasu

    • have you initiated the webdriver app in the Android device before? and have you specified your device id after the -s switch?

  5. adb -s shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity -e debug true

    adb -s forward tcp:8080 tcp:8080

    I used the above to 2 commands to start the webdriver (in debug mode) and then do the forwarding. I also tried without the debug mode and manually starting the webdriver app on the device.

    • no obvious problem, you have the right commands in right order. I can only think on a firewall related issue on your pc or that because an unknown reason the webdriver app was not able to start the daemon in port 8080

  6. I see this problem only with android-server-2.21.0.apk. It works with android-server-2.32.0.apk. But for my purposes (for various reasons) I need 2.21.0. Is there a different way to do it for 2.21.0 or somehting else i can try? BTW, thank you so much for the prompt responses!!

  7. I am on a team building a customer Android browser. I noticed that when you wrote this a year and a half ago, it only supported the default android browser. Has that changed? If so, do you think we could get it to work with our custom browser? Thanks for your help in advance.

    • Hi, as far as I know the Android server only works to automate the Android or Chrome browsers, depending on your base OS version.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s