Introducing Android WebDriver

October 30th, 2011 by Android Dev

[This post is by Dounia Berrada, an engineer on the EngTools team. — Tim Bray]

Selenium WebDriver is a browser automation tool which provides a lightweight and elegant way for testing web apps. Selenium WebDriver is now available as an SDK extra in the Android SDK, and supports 2.3 (Gingerbread) and onwards!

Whether or not your site is optimized for mobile browsers, you can be sure that users will be accessing it from their phones and tablets. WebDriver makes it easy to write automated tests that ensure your site works correctly when viewed from the Android browser. We’ll walk you through some basics about WebDriver and look at it in action.

WebDriver Basics

WebDriver tests are end-to-end tests that exercise the web application just like a real user would. WebDriver models user interactions with a web page such as finger flicks, finger scrolls and long presses. It can rotate the display and interact with HTML5 features such as local storage, session storage and the application cache. Those tests run as part of an Android tests project and are based on Junit. They can be launched from Eclipse or the command line. WebDriver tests can be wired with a continuous integration system and can run on phone and tablet emulators or real devices. Once the test starts, WebDriver opens a WebView configured like the Android browser and runs the tests against it.

WebDriver is an Android SDK extra and can be installed following these instructions. Once you’ve done that you’ll be ready to write tests! There is a comprehensive WebDriver user guide on the Selenium site, but let’s start with a basic example using www.google.com to give you a taste of what’s possible.

Getting Started

First, create an Android project containing an empty activity with no layout.

public class SimpleAppActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Then create the Android test project that will contain the tests. WebDriver will create the WebView and set the layout automatically in the main Activity.

Let’s write a test that opens the Google home page 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 class SimpleGoogleTest extends ActivityInstrumentationTestCase2<SimpleAppActivity> {

    public void testGoogleShouldWork() {
      // 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! WebDriver will create a WebView with the same configuration as the Android browser in the main UI thread, i.e. the activity thread. The activity will display the WebView on the screen, allowing you to see your web application as the test code is executing.

Interaction Testing

We’ve mentioned that 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());

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

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

Find Out More

If this has whetted your appetite and you’d like to know more, go ahead and install the Android WebDriver, take a look at the documentation on the Selenium project’s wiki, or just browse the javadocs. For questions and feedback not only of the Android WebDriver but also its desktop brethren, please join webdriver@googlegroups.com. For announcements keep an eye on http://seleniumhq.wordpress.com/.

Build the city of your dreams… and then run it in Virtual City Playground (Video)

October 29th, 2011 by Android Dev

Ever want to just build your own city and run that bad boy?  Well, now you can in Virtual City Playground for Android.  This virtual fantasy allows you to build your very own city any way you see fit and then run it in the same manner.  With the ability to add 85 new optional guests and build exciting real landmarks such as the Eiffel Tower, Statue of Liberty and the Sydney Opera, it’s sure to be one of the most happening places around.  Run the city as you see fit, build an empire, make decisions that you believe will overall benefit the environment.  You can also earn achievements as you go on with your game play.  The developer touts that there are over 95 new achievements to be had.  You can even upload your success to Facebook for all of your friends and family to see.  If you’re a huge fan of FarmVille, then you’ll love this.     Read More…

Build the city of your dreams… and then run it in Virtual City Playground (Video)


[Android Quick Tip] How To Install The Amazon App Store On Your Android Device

October 29th, 2011 by Android Dev

You’ve finally broken down and want to see what the Amazon App Store is all about? Well you’re in luck, here’s a quick tutorial for how to install the app on your phone or tablet.

Let’s start off with a basic understanding of the app itself. The Amazon App Store is an alternative to Google’s own Market. Using your Amazon.com account, you can see many of the apps you’d find in the Market as well as Amazon-exclusive apps. There are thousands of apps available to go along with many being submitted on a daily basis. When you see an app that you are interested in, then you go ahead and download the app. The process is similar to downloading an app in the Market, but has a few slight differences which is where this tutorial will come in and help you.   Read More…

[Android Quick Tip] How To Install The Amazon App Store On Your Android Device


[Video] Samsung Shows off Galaxy Y Pro in New Commercial

October 29th, 2011 by Android Dev

Here’s yet another new Samsung Galaxy device for ya, the Galaxy Y Pro. With a full QWERTY keyboard and built in social networking capabilities, this phone will be targeted at those looking for an entry-level device with a price tag to match.

Known as the Galaxy Txt in Netherlands, it packs a full QWERTY keyboard, a 2.6″ QVGA touchscreen display, 3MP camera, 1200mAh battery, Wi-Fi with Wi-Fi direct support, Bluetooth 3.0, Android 2.3 Gingerbread, a microSD card slot and GPS.

We can expect this device to come in with a rather low price tag, reports are saying Netherlands will sell it for around €163 ($230) unlocked. Check out the official commercial below and let us know what you think.

Click here to view the embedded video.

[Video] Samsung Shows off Galaxy Y Pro in New Commercial


Nippon Announces New ‘Invisible Glass’ – Removes Annoying Glare

October 29th, 2011 by Android Dev

Booyah! What you see above is the possible future for our beloved smartphones and tablets - for all technology with screens for that matter!

What you are looking at is two variations of glass, the left being untreated and glare ridden, while the right, treated and virtually glare free. This new technology called “invisible glass” was developed by Nippon Electric Glass and hopefully device manufacturers team up with these geniuses sometime in the near future.

Studies show that regular glass reflects 8 percent of all light while the remaining 92 percent passes directly through it. This new “invisible glass” creates a huge difference and only reflects 0.5 percent of light, rendering the surface virtually glare-free. The glass is constructed with over 30 layers of special film on each side, each layer measured in mere nanometers. Read More…

Nippon Announces New ‘Invisible Glass’ – Removes Annoying Glare