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

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 =;
WebDriver driver = new RemoteWebDriver(new URL(
    "http://<pc-ip>:<pc-port>/wd/hub"), browser);

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!


26 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 πŸ˜€

    1. 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!

      1. 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 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 ( 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 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 =;

    WebDriver driver = null;
    driver = new RemoteWebDriver(new URL(“”), browser);

    ——–Selenium Node———————————————————————————– {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.

    1. 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.

      1. 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…

      2. 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)){
    {“————-***LAUNCHING ANDROID_DRIVER***————–“);
    capabilities = new DesiredCapabilities();
    System.out.println(“———–checking in ANDROID_DRIVER——-“);
    catch (Exception e)


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

    “platform”: “ANDROID”,
    “browserName”: “android”,
    “maxInstances”: 1,
    “seleniumProtocol”: “WebDriver”
    “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!


    1. 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 -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.

    1. 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.

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

  8. Hello,

    I am working on mobile web site which has the following feature:

    1. A page with map widget with address edit.
    2. Enter post code/address in the edit and the location is searched and selected.

    I am trying to automate the above scenario.

    When i open my mobile web app on firefox/chrome browser on mobile device, i get a popup “do you want to share your location with ‘’ ” – after accepting this popup the map is loaded and i am able to search the locations using address

    I have created a script using selendroid, but the selendroid mobile view app doesn’t show any pop-up, map is also not loaded.

    It seems the location is not by default shared with the selendroid web view app, Kindly let me know if there is a way to achieve this.

    Best Regards,
    Nishant Taksande

    1. I’ve never faced that use case before so I don’t have hint or tip. Hopefully other readers may have and share with you.

  9. Is running tests on android firefox browser now possible? If so, could you help me out with the Java code. As I am trying to open firefox browser on android but my PC firefox browser opens up.

    The code I am using:
    WebDriver driver = null;
    DesiredCapabilities caps = null;
    caps = DesiredCapabilities.firefox();
    driver = new FirefoxDriver(caps);

  10. I tried to test web app on Android emulator (ADV) with Selenium, but no success so far.
    Here is what I have done and what I got:
    1. Generate java code from Selenium IDE
    2. Run the script for desktop browsers(Firefox, Safari, Chrome and IE), test is successful.
    3. Run the same script on Chrome as emulator, test is successful.
    4. Run the same script on ADV, it always get element not found error. Funny thing is that a button that is not clicked in the script is clicked in ADV.

    The question is why webpage elements are available in desktop browsers, but not in chrome browser in ADV and wrong button is clicked?


    1. can u please send me the code which u have used for the chrome browser in emulator. And also send me the configuration of the EMULATOR which you have used.Because in my case Chrome is not opening .
      it’s urgent please respond quickly.

  11. ya,i am not getting the exact code for opening firefox in Emulator or please provide the code.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s