opensource.google.com

Menu

Test Your Mobile Web Apps with WebDriver - A Tutorial

Friday, October 28, 2011

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.

Get Started

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.


We mentioned above that the WebDriver supports creating advanced gestures to interact with the device. Let's use WebDriver to throw an image across the screen by flicking horizontally, and ensure that the next image in the gallery is displayed.

 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());

Next, let's rotate the screen and ensure that the image displayed on screen is resized.
 assertEquals(landscapeSize, secondImage.getSize()) 
((Rotatable) driver).rotate(ScreenOrientation.PORTRAIT);
assertEquals(portraitSize, secondImage.getSize());

Let's take a look at the local storage on the device, and ensure that the web application has set some key/value pairs.
 // 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

WebDriver has two main components: the server and the tests themselves. The server is an application that runs on the phone, tablet, emulator, or simulator and listens for incoming requests. It runs the tests against a WebView (the rendering component of mobile Android and iOS) configured like the browsers. Your tests run on the client side, and can be written in any languages supported by WebDriver, including Java and Python. The WebDriver tests communicate with the server by sending RESTful JSON requests over HTTP. The tests and server pieces don't have to be on the same physical machine, although they can be. For Android you can also run the tests using the Android test framework instead of the remote WebDriver server.

Infrastructure Setup


At Google, we have wired WebDriver tests to our cloud infrastructure allowing those tests to run at scale and making it possible to have them run in our continuous integration system. External developers can run their mobile tests either on emulators/simulators or real devices for Android and iOS phones and tablets.

Android emulators can run on most OSes because they are virtualized, so we run them on our generic cloud setup. Though there are many advantages to using Android emulators because they emulate a complete virtual device (including the virtual CPU, MMU, and hardware devices), it makes the test environment slower. You can speed up the tests by disabling animations, audio, skins, or even by running in the emulator headless mode. To do so, start the emulator with the options --no-boot-anim, --no-audio, --noskin, and --no-window. If you would like your tests to run even faster, start the emulator from a previously created snapshot image. That reduces the emulator startup time from 2 minutes to less than 2 seconds!

iOS simulators can't be virtualized and hence need to run on Mac machines. However, because iOS simulators don't try to emulate the virtual device or CPU at all, they can run as applications at "full speed," this allows the test to run much faster.

Stay tuned for more mobile feature in Selenium WebDriver, and updates on the Selenium blog. For questions and feedback not only of the Android WebDriver but also its desktop brethren, please join the community.

By Dounia Berrada, Engineer on the EngTools team

OpenMRS welcomes with open arms

Thursday, October 27, 2011

Recently, I had the rare privilege of attending the annual Implementers meeting of OpenMRS, my Google Summer of Code mentoring organization. Thanks largely to the conference sponsorship by Google, OpenMRS was able to fund my week-long visit to Kigali, Rwanda. I traveled over 4,500 miles to meet with core OpenMRS developers, other volunteers, implementers (people with both IT skills and health care experience who work to deploy OpenMRS in their hospital, clinic, laboratory, etc.), service providers and researchers who had gathered there for the conference.

Arriving at Kigali, I was amazed at the diversity and fellowship amongst the community, and of how happy they were to accept me as one of their own. I took part in a pre-conference hackathon and visited the OpenMRS Implementation meeting at Rwanda’s TRAC Plus health clinic. I listened to why implementers from Village Health Works used an Access database for their Burundi clinic, and to Dr. Joaquin Blaya’s work with Interactive Voice Response. Eduardo Jezierski, the CTO of InSTEDD, talked to me about their work in Haiti, while Christopher Bailey of the World Health Organization spoke of his experience working with developing countries.


I also enjoyed a Chinese meal with a group of US-based developers for the AMPATH program in Western Kenya. I visited the Kigali Genocide museum, played cards with a group of research scientists, academics and other developers and spilled my drink all over a director’s laptop. I had dinner with OpenMRS co-founder Dr. Burke Mamlin, brought a drink for co-founder Dr. Paul Biondrich and had breakfast with an MIT graduate who explained why he quit building space satellites and an engineering career to enroll in medical school.

My participation at the conference was an eye opener for several reasons. First of all, it helped me make the transition from Google Summer of Code student to full fledged community member. I stopped being just an offshore volunteer, and understood my organization for what it really was. I saw my project as a community, a group of vibrant, talented and extremely capable people with a wide range of interests in software development, research, medicine, health informatics and public health. I understood that OpenMRS is not “just” OpenMRS, but a massive network of implementers, developers, healthcare workers and other organizations. I saw the dedication and professionalism of community members and their sincere concern to help make the world a better place. I also realized how community members were supporting themselves while enjoying what they do by serving as consultants, developers and service providers.

One of the goals of Google Summer of Code is to encourage students to contribute to open source projects. I believe that my experience at the conference highlights something that is an important part of the Google Summer of Code experience – helping students integrate into their project’s community so that they are more likely to stay involved long after the program deadlines have passed. I think program administrators could help facilitate this transition from student to community member by introducing more flexible methods of student evaluation and by giving more weight to community participation in the evaluations.

By Suranga Kasthurirathne, Google Summer of Code student and OpenMRS community member

ScriptCover: Javascript coverage analysis tool

Wednesday, October 26, 2011

We are pleased to announce the open source release of a Javascript coverage analysis tool called ScriptCover. It is a Chrome extension that provides line-by-line Javascript code coverage statistics for web pages in real time without user modification of the site. Results are collected when the page loads and continue to be updated as users interact with the page. These results can be viewed in real time through a reporting tool which highlights the executed lines of code for detailed analysis. ScriptCover is useful when performing manual and automated testing and in understanding and debugging complex code.

Short report in Chrome extension popup, detailing both overall scores and per-script coverage.


Sample of annotated source code from the detailed report. First two columns are line number and number of times each instruction has been executed.


We envision many potential features and improvements for ScriptCover, e.g.:
  • support other coverage metrics, e.g. path coverage and condition coverage
  • support richer reports and exporting to HTML and XML
  • submit Javascript coverage statistics to a server and analyze combined statistics for selected users, dates, etc.
  • map user actions to related Javascript code
Want to get involved with ScriptCover and make it better? Join the team! To get started, visit our project page, join the community, read documentation and download the code.

By Ekaterina Kamenskaya, Software Engineer in Test, Google

A Spectrum of Results - All Good

Sunday, October 23, 2011

We are pleased to announce the final results of this year's OpenICC participation in the Google Summer of Code program. OpenICC mentored two students directly and one student through collaboration with the openSUSE organization. All three of our students successfully met their project goals and completed their colour management projects.

Yiannis Belias worked on the API stabilization for Oyranos Colour Management System II project. The new classes, code generator improvements, and tools he worked on will be integrated into the Oyranos master branch in upcoming months. This project helps in stabilizing the CMS core, which covers a great foundation of functionality.

Joseph Simon worked on the XCPD project. The goal of his project was to implement a prototype printing dialog based on The Linux Foundation's Common Printing Dialog (CPD) project code to allow a standards based color managed PDF based printing workflow. The PDF spool file created by the modified CPD follows the PDF/X specification for embedding user side colour managed content and color related remote printer configuration information for a complete solution to printing color managed content in a standard Linux/Unix environment.

Sebastian Oliva implemented an ICC device profile database, called taxi that is intended to share vendor and user created ICC profiles across platforms in an automated fashion. The online database is designed to cover metadata about the device driver calibration status alongside the characterization information in the corresponding ICC profiles. The project idea and student slot was provided by the openSUSE distribution project and mentored by an OpenICC member.

Many thanks to all the students for their great work, all the people who helped in shaping the basic ideas and devising the projects for the program, Google for providing the stipends for the students, and open SUSE for inviting the students to the European openSUSE Conference.

By Kai-Uwe Behrmann, OpenICC Org Administrator for Google Summer of Code

UPDATE: More Hack4Transparancy for everyone

Monday, October 17, 2011

A few weeks ago we told you about the Hack4Transparancy event, bringing techies together November 8th and 9th in the European Parliament for an all-expenses-paid good time eating, talking, and making important data accessible to everyone.

Well, now we’ve got more exciting news. We’ve broadened the scope of the event and extended the application deadline for those wishing to make data on Internet performance visible and meaningful.

What’s changed?
The application deadline for the Internet Quality track has been extended through noon, CET, Friday October 21st (that's this coming Friday).
To diversify the skill-set of interested hackers, we’ve added a data visualization option to the Internet Quality track.
We’ve expanded the criteria -- now, eligible hackers from anywhere in the world can apply.
And, we’ve increased the prize money. One winning team or individual on each track will now receive €5.000,00.

Now, sharpen your coding and data visualization skills, and send in your application! Winners will be notified the week of October 24.

By Marco Pancini, Google Sr. Policy Counsel, Brussels
.