Selenium WebDriver is an automatic browser testing tool that provides lightweight and elegant ways to test web applications. As the Android SDK extra, Selenium WebDriver supports Android 2.3 (Gb) and earlier versions.
WebDriver is an end-to-end test that can actually test user behavior. User interaction, such as touch, finger rolling, and long-pressed, also supports some HTML5 features, such as local storage, session storage, and application cache. These tests are part of the Android tests project based on Junit. It can be loaded from Eclipse or through the command line. WebDriver can run on mobile phones, tablets, tablet simulators, or real devices. Once started, WebDriver opens a WebView configuration similar to the Android browser and runs the test.
WebDriver is an Android SDK extra and can be installed through these instructions. User Guide is also available on the Slenium site.
A simple example is provided as follows:
Create an Android project that contains an empty Activity without layout.
1 |
public class SimpleAppActivity extends Activity { |
3 |
public void onCreate(Bundle savedInstanceState) { |
4 |
super .onCreate(savedInstanceState); |
Create an Android test project. WebDriver will create a WebView and automatically set the layout in the main Activity. The following is the Google homepage on Android to query "Los Angeles weather ". Test and verify that Google returns the search result. The first result returns the answer.
01 |
public class SimpleGoogleTest extends ActivityInstrumentationTestCase2<SimpleAppActivity> { |
03 |
public void testGoogleShouldWork() { |
04 |
// Create a WebDriver instance with the activity in which we want the test to run |
05 |
WebDriver driver = new AndroidDriver(getActivity()); |
06 |
// Let’s open a web page |
07 |
driver.get( "http://www.google.com" ); |
09 |
// Lookup for the search box by its name |
10 |
WebElement searchBox = driver.findElement(By.name( "q" )); |
12 |
// Enter a search query and submit |
13 |
searchBox.sendKeys( "weather in san francisco" ); |
16 |
// Making sure that Google shows 11 results |
17 |
WebElement resultSection = driver.findElement(By.id( "ires" )); |
18 |
List<WebElement> searchResults = resultSection.findElements(By.tagName( "li" )); |
19 |
assertEquals( 11 , searchResults.size()); |
21 |
// Let’s ensure that the first result shown is the weather widget |
22 |
WebElement weatherWidget = searchResults.get( 0 ); |
23 |
assertTrue(weatherWidget.getText().contains( "Weather for San Francisco, CA" )); |
This Activity will display WebView on the screen, allowing you to see your web application, once the test code is executed.
The following is an interactive test.
WebDriver supports creating advanced gestures to interact with devices. In this example, an image is discarded horizontally to ensure that the image in the library is displayed.
1 |
WebElement toFlick = driver.findElement(By.id( "image" )); |
2 |
// 400 pixels left at normal speed |
3 |
Action flick = getBuilder(driver).flick(toFlick, 0 , - 400 , FlickAction.SPEED_NORMAL) |
6 |
WebElement secondImage = driver.findElement(“secondImage”); |
7 |
assertTrue(secondImage.isDisplayed()); |
Rotate the screen to ensure that the size of the image displayed on the screen is adjusted.
1 |
assertEquals(landscapeSize, secondImage.getSize()) |
2 |
((Rotatable) driver).rotate(ScreenOrientation.PORTRAIT); |
3 |
assertEquals(portraitSize, secondImage.getSize()); |
You can easily screenshot a bug and debug it.
1 |
File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); |