Quote

"Between stimulus and response there is a space. In that space is our power to choose our response.
In our response lies our growth and freedom"


“The only way to discover the limits of the possible is to go beyond them into the impossible.”


Friday 7 September 2012

Automating Android Browser Testing Using Android WebDriver


If your web application supports android platform, then you need to perform functional verification of your application from the android platform. Just like for a desktop appication you can use the WebDrivers Android avatar to perform automated end-to-end tests that ensure your site works correctly when viewed from the Android browser. The advantage of using the Android WebDriver is that it supports all the core WebDriver APIs plus it also provides mobile specific features, such as finger scroll and long presses and also supports the HTML5 features, such as local storage, session storage, and application cache. Native events are used by the Android Webdriver to interact with the web page which is rendered using the WebView, which is the component used for rendering web pages by the android browser.

Now when we know the basics of the Android WebDriver let us start the installation process. The first question is, What are the pre-requisites for isntalling the Android WebDriver? The setup and exercises that we will be doing require the following:

1) Eclipse with ADT
2) Android SDK installed
3) Android WebDriver
4) Android device emulators or real android devices

Once you have the above requirements ready then the following steps need to be followed to setup end-to-end web application testing on Android:

Step 1: Getting the device or the emulator ready


If you have a physical Android device you can connect the device to your machine using a USB. But if you do not have a physical Android device you can use the Android emulators. The steps to setup the emulator are as follows:

-- Create an Android Virtual Device using the command line: You can use the 'AVD Manager.exe' in the Android SDK to launch a GUI which helps in creating an Android Virtual Device. However, we can perform the same task using the command line as well. Navigate to the directoty ~/android-sdk/tools and run the command:
android create avd -n YourAndroid -t 12 -c 100M 


Select no where you are asked to create a custom hardware profile as it is not required.

After creating the AVD named YourAndroid above, now you can start the emulator using the following command:
emulator -avd YourAndroid &
Once your emulator or the device is ready we are done with the environment setup.

Step 2: Running the tests Using Remote WebDriver Server


The android server is an android application(.apk) which contains an HTTP server. The WebDriver commands from the JUnit tests make a request to the Android Server which sends these requests to the Android WebDriver, which processes and sends an appropriate responce back.

You need to get the serial ID of the device or the emulator to be able to install the Android Server to it. You can run the following command to get the list of available devices:

adb devices

We need to navigate to the ~/android-sdk/platform-tools directory on the command line to execute the above command. The output of the command lists all the available devices, such as:
List of devices attached
emulator-5554   device


Here emulator-5554 is the serial number which needs to be passed to the install command for installing the Android Server. To install the Android Server, you need to first download it from the download link. After downloading the Android Server.apk you run the following command to install the Android Server to the target emulator/device:

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

In our case the SerialID is emulator-5554 and the current server version available on the download page is android-server-2.21.0.apk. Hence we will be running the following command to install the android server to our virtual device having the serial ID emulator-5554:

adb -s emulator-5554 -e install -r android-server-2.21.0.apk

The output of the command would be as follows:

To be able to install applications that are coming from the Android Market/ Playstore you need to configure your device or the emulator. Make sure you have Setting-->Applications-->Unknown Sources checked to be able to install applications from outside the Android Market. Now that the application is installed on the device/emulator we need to start application now. The application can be started from the device GUI or from the command line. The command to start the server application on the device is as follows:
adb -s <serialId> shell am start -a android.intent.action.MAIN -
org.openqa.selenium.android.app/.MainActivity -e debug true

so in our case the command will be:
adb -s emulator-5554 shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity -e debug true

The WebDriver icon on the device GUI shown below can also be used to start the Android Server application:

 After starting the Android Server we need to setup port forwarding so that the requests are sent from the host machine to the emulator. Port forwarding enables the Android Server on the emulator to be available to the host machine over http connection using the URL http://localhost:8080/wd/hub

Run the following command for port forwarding:
adb -s emulator-5554 forward tcp:8080 tcp:8080
Now the emulator will be available at http://localhost:8080/wd/hub.

Step 3: Writing and Executing Tests

Now we can write JUnit tests from eclipse and run them on the Android device. A simple JUnit test is as follows:


import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;

public class TestAndroidWebDriver extends TestCase {
     
      public void testAndroid() throws Exception {

          WebDriver driver = new AndroidDriver();
          driver.get("http://satyamsing.blogspot.com");
          String title = driver.getTitle();
          WebElement e = driver.findElement(By.partialLinkText("Android"));
          e.click();
          e = driver.findElement(By.partialLinkText("Topic 3"));
          e.click();
          assertTrue("Got title: " + title, title.contains("Satyam"));
          driver.quit();

     }

}

Add the above test in your project using the following steps:

1) Create a test project and add selenium and JUnit jars as an external libraries to it
2) Create a class named 'TestAndroidWebDriver' and copy the above code to it
3) Compile and run the JUnit test

You can now see your test running on the Android as follows: