Automation Testing for Android Mobile Web Applications & Steps to setup environment

Android WebDriver allows to run automated end-to-end tests that ensure your site works correctly when viewed from the Android browser. Android WebDriver supports all core WebDriver APIs, and in addition to that it supports mobile spacific and HTML5 APIs. Android WebDriver models many user interactions such as finger taps, flicks, finger scrolls and long presses. It can rotate the display and interact with HTML5 features such as local storage, session storage and application cache.

The current apk will only work with Gingerbread (2.3.x), Honeycomb (3.x), Ice Cream Sandwich (4.0.x) and later.
  • Note that there is an emulator bug on Gingerbread that might cause WebDriver to crash.

Install the Android SDK:

Download the Android SDK, and unpack it to ~/android_sdk/. NOTE: The location of the Android SDK must exist in ../android_sdk, relative to the directory containing the Selenium repository.

Android WebDriver test can run on emulators or real devices for phone and tablets.

Setup Emulator

To create an emulator, you can use the graphical interface provided by the Android SDK, or the command line. Below are instructions for using the command line.

First, let's create an Android Virtual Device (AVD):

$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. For an exhaustive list of targets, run:

./android list targets

Make sure the target level you selected corresponds to a supported SDK platform.
-c: specifies the SD card storage space.
When prompted "Do you wish to create a custom hardware profile [no]" enter "no".

Now, start the emulator. This can take a while, but take a look at the FAQ below for tips on how to improve performance of the emulator.

$./emulator -avd my_android &

Setup the Device

Simply connect your Android device through USB to your machine.

Using the Remote Server

This approach has a client and a server component. The client consists of your typical Junit tests that you can run from your favorite IDE or through the command line. The server is an Android application that contains an HTTP server. When you run the tests, each WebDriver command will make a RESTful HTTP request to the server using JSON according to a well defined protocol. The remote server will delegate the request to Android WebDriver, and will then return a response.

Install the WebDriver APK

Every device or emulator has a serial ID. Run this command to get the serial ID of the device or emulator you want to use:

$~/android_sdk/platform-tools/adb devices

Download the Android server from http://code.google.com/p/selenium/downloads/list. To install the application do:

$./adb -s <serialId> -e install -r  android-server.apk

Make sure you are allowing installation of application not coming from Android Market. Go to Settings -> Applications, and check "Unknown Sources".
Start the Android WebDriver application through the UI of the device or by running this command:

$./adb -s <serialId> shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity

Now we need to setup the port forwarding in order to forward traffic from the host machine to the emulator. In a terminal type:

$./adb -s <serialId> forward tcp:8080 tcp:8080

This will make the android server available at http://localhost:8080/wd/hub from the host machine. You're now ready to run the tests. Let's take a look at some code.

Run the Tests

import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;
public class OneTest extends TestCase {
  public void testGoogle() throws Exception {
    WebDriver driver = new AndroidDriver();
    
    // And now use this to visit Google
    driver.get("http://www.google.com");
    
    // Find the text input element by its name
    WebElement element = driver.findElement(By.name("q"));
    
    // Enter something to search for
    element.sendKeys("Cheese!");
    
    // Now submit the form. WebDriver will find the form for us from the element
    element.submit();
    
    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());
    driver.quit();
  }
}

To compile and run this example you will need the selenium-java-X.zip (client side piece of selenium). Download the selenium-java-X.zip from our download page, unzip and include all jars in your IDE project. For Eclipse, right click on project -> Build Path -> Configure Build Path -> Libraries -> Add External Jars

Hope this will be useful for steup and test your application under Android