Test Your Mobile Web Apps with WebDriver - A Tutorial
Friday, October 28, 2011 | 5:00 PM
Labels: android, selenium, webdriver
Mobile testing has come a long way since the days when testing mobile web applications was mostly manual and took days to complete. Selenium WebDriver is a browser automation tool that provides an elegant way of testing web applications. WebDriver makes it easy to write automated tests that ensure your site works correctly when viewed from an Android or iOS browser.
For those of you new to WebDriver, here are a few basics about how it helps you test your web application. WebDriver tests are end-to-end tests that exercise a web application just like a real user would. There is a comprehensive user guide on the Selenium site that covers the core APIs.
Now let’s talk about mobile! WebDriver provides a touch API that allows the test to interact with the web page through finger taps, flicks, finger scrolls, and long presses. It can rotate the display and provides a friendly API to interact with HTML5 features such as local storage, session storage and application cache. Mobile WebDrivers use the remote WebDriver server, following a client/server architecture. The client piece consists of the test code, while the server piece is the application that is installed on the device.
WebDriver for Android and iPhone can be installed following these instructions. Once you’ve done that, you will be ready to write tests. Let’s start with a basic example using www.google.com to give you a taste of what’s possible.
The test below opens www.google.com on Android and issues a query for “weather in san francisco”. The test will verify that Google returns search results and that the first result returned is giving the weather in San Francisco.
public void testGoogleCanGiveWeatherResults() {
// Create a WebDriver instance with the activity in which we want the test to run.
WebDriver driver = new AndroidDriver(getActivity());
// Let’s open a web page
driver.get("http://www.google.com");
// Lookup for the search box by its name
WebElement searchBox = driver.findElement(By.name("q"));
// Enter a search query and submit
searchBox.sendKeys("weather in san francisco");
searchBox.submit();
// Making sure that Google shows 11 results
WebElement resultSection = driver.findElement(By.id("ires"));
List<WebElement> searchResults = resultSection.findElements(By.tagName("li"));
assertEquals(11, searchResults.size());
// Let’s ensure that the first result shown is the weather widget
WebElement weatherWidget = searchResults.get(0);
assertTrue(weatherWidget.getText().contains("Weather for San Francisco, CA"));
}
Now let's see our test in action! When you launch your test through your favorite IDE or using the command line, WebDriver will bring up a WebView in the foreground allowing you to see your web application as the test code is executing. You will see www.google.com loading, and the search query being typed in the search box.

WebElement toFlick = driver.findElement(By.id("image"));
// 400 pixels left at normal speed
Action flick = getBuilder(driver).flick(toFlick, 0, -400, FlickAction.SPEED_NORMAL)
.build();
flick.perform();
WebElement secondImage = driver.findElement(“secondImage”);
assertTrue(secondImage.isDisplayed());
assertEquals(landscapeSize, secondImage.getSize())
((Rotatable) driver).rotate(ScreenOrientation.PORTRAIT);
assertEquals(portraitSize, secondImage.getSize());
// Get a handle on the local storage object
LocalStorage local = ((WebStorage) driver).getLocalStorage();
// Ensure that the key “name” is mapped
assertEquals(“testUser”, local.getItem(“name”));
What if your test reveals a bug? You can easily take a screenshot for help in future debugging:
File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
High Level Architecture



5 comments:
JokeyRhyme said...
Any word on when an X86-based emulator will be available? I understand there will always be reasons why the ARM emulation is required (such as for NDK testing), but surely an X86 emulator would be able to take better advantage of host hardware.
There are X86 instructions in many AMD and Intel CPUs that help reduce the performance hit when executing an X86 guest OS. I imagine this would significantly improve the performance of Dalvik-only Android applications such an emulator.
October 28, 2011 5:36 PM
hackmyass said...
great article !!
thanx
October 31, 2011 11:52 PM
Tester said...
I have a question.
Have you managed to use WebDriver for selecting options from a select list(example: in google advance search page, select French from the languages list)?
I never managed to do it and for me this is a showstopper.
Thank you
November 15, 2011 6:00 AM
Kishore said...
I have used webdriver for iphone simulator, a web application layout testing. it was very good and helpful. we were able to automate. but we were not able to take screenshots in iphone simulator. wehn i used the takesccreenshot method, it says method is not implemeted error is thrown.
is there any way we can take screenshots in iphone simulator?
Regards,
Kishore.
November 27, 2011 9:55 PM
Paryushan said...
Hi,
I am new to mobile automation and was able to go the simple google test using AndroidDriver. Now I want to try the similar test on iOS, I tried reading about iPhoneDriver, but could not get much information about the same. Can you please help me.
Thanks,
Paryushan
January 4, 2012 4:40 AM
Post a Comment