Introduction to the Android WebDriver browser Automatic Testing Tool

Source: Internet
Author: User

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 {
2     @Override
3     public void onCreate(Bundle savedInstanceState) {
4         super.onCreate(savedInstanceState);
5     }
6 }

 

 

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 extendsActivityInstrumentationTestCase2<SimpleAppActivity> {
02  
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");
08  
09       // Lookup for the search box by its name
10       WebElement searchBox = driver.findElement(By.name("q"));
11  
12       // Enter a search query and submit
13       searchBox.sendKeys("weather in san francisco");
14       searchBox.submit();
15  
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());
20  
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"));
24     }
25 }

 

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)
4         .build();
5 flick.perform();
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.