Friday, August 05, 2011

Android Application Testing Guide: Q&A

Q:Diego, if we were to test something that is asynchronous, like in my case I'm wanting to test if a webview loads an URL how would I go about waiting for the webpage to finish loading in my webview ?
Posted by Pedro Veloso.

A:Actually this is a very interesting question as there are many ways and you should be cautious about WebView semantics and how some of the WebViewClient methods are called.

For example onPageFinished may be invoked wether the page was successfully loaded or there was an error. So, you may need a different approach if your intention is to test if an url was successfully loaded.

In this code snippet I'm using a MockWebViewClient to detect error conditions and simply waiting some time for the page to load. We could also iterate over a period of time checking if the value has changed instead of just waiting but we are keeping this as simple as possible. This is also assuming you have an Activity holding the WebView and it has the required getters.

package com.example.aatg.webview.test;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.Suppress;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.aatg.webview.AndroidHelloWebViewActivity;

 * @author diego
public class AndroidHelloWebViewActivityTests extends
        ActivityInstrumentationTestCase2<AndroidHelloWebViewActivity> {

    private static final String VALID_URL = "";
    private static final String INVALID_URL = "";

    private static final long TIMEOUT = 5000;

    private AndroidHelloWebViewActivity mActivity;
    private WebView mWebView;
    private MockWebViewClient mMockWebViewClient;

     * @param name
    public AndroidHelloWebViewActivityTests() {

    /* (non-Javadoc)
     * @see android.test.ActivityInstrumentationTestCase2#setUp()
    protected void setUp() throws Exception {
        mActivity = getActivity();
        mWebView = mActivity.getWebView();
        mMockWebViewClient = new MockWebViewClient();

    /* (non-Javadoc)
     * @see android.test.ActivityInstrumentationTestCase2#tearDown()
    protected void tearDown() throws Exception {

    public final void testLoadValidUrl() {

    public final void testLoadInvalidUrl() {

    private void assertLoadUrl(String url) {
        assertTrue(!(mWebView.getProgress() < 100));

    private void sleep() {
        try {
        } catch (InterruptedException e) {
            fail("Unexpected timeout");

    private class MockWebViewClient extends WebViewClient {
        boolean mError;

        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            mError = true;

Hope this helps.


Unknown said...

can i send the keycodes in to the webpage?
like click on editbox or buttons in that webpage..

Ajay said...

Diego, If I want to run an automation tests which requires a phone reboot in between, then after phone reboot how could be the automation start from the point it stops before reboot?

Android app development said...

This is one the interesting post.I like your blog content.this is one of the nice post.
Android app developers

Android developer said...

I accept this absolutely is excellent information. Most of peoples will accede with you. so i would like to thanks for creating this blog.

Android developer