DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

  1. DZone
  2. Refcards
  3. Selenium 2.0: Using the Webdriver API to Create Robust User Acceptance Tests
refcard cover
Refcard #125

Selenium 2.0: Using the Webdriver API to Create Robust User Acceptance Tests

Automate User Acceptance Tests With Selenium 2.0

Covers the background of Selenium 2.0, WebDriver architecture, installation, driver implementation, page interaction models and mobile support.

Download Refcard
Free PDF for Easy Reference
refcard cover

Written By

author avatar Matt Stine
Senior Principal Software Engineer, Private
Table of Contents
► What is Selenium 2.0? ► Installation ► Driver Implementations ► Selenium RC Emulation ► Page Interaction Model ► Page Object Pattern ► Remote WebDriver ► Mobile Device Support
Section 1

What is Selenium 2.0?

By Matt Stine

Selenium 2.0 is a tool that makes the development of automated tests for web sites and web applications easier. It represents the merger of the original Selenium project with the WebDriver project. WebDriver contributes its object-oriented API for Document Object Model (DOM) interaction and browser control as well as its various browser driver implementations.

The WebDriver approach to browser control differs significantly from the Selenium approach. Whereas Selenium runs as a JavaScript application inside the targeted browser, WebDriver drives the browser directly through the most sensible means available. For Firefox, this meant using JavaScript wrapped in an XPCOM component; however, for Internet Explorer, this meant using C++ to drive IE’s Automation APIs.

The merger of these two projects allows each technology’s strengths to mitigate the other’s weaknesses. For example, Selenium has good support for most common use cases, but WebDriver opens up many additional possibilities via its ability to step outside of the JavaScript sandbox. Going forward, both of these APIs will live side by side in a synergistic relationship, allowing automated test developers to easily leverage the right tool for the job.

Selenium 2.0 currently supports writing cross-browser tests in Java (and other languages on the JVM including Groovy), Python, Ruby, and C#. It currently allows developers to target Firefox, Internet Explorer, and Chrome with automated tests. It also can be used with HtmlUnit for “headless” application testing. Furthermore, driver implementations exist for the iOS (iPhone/iPad), Android, and BlackBerry platforms.

The remainder of this Refcard will focus on the WebDriver contributions to the Selenium 2.0 project and will take a brief look at how to leverage the Selenium API via WebDriver.

Architecture

The WebDriver team chose to focus on four primary architecture concerns:

  1. The User: WebDriver drives the browser from your end user’s point of view.
  2. A “Best Fit” Language: each browser has a language that is most natural to use from an automation perspective. The various drivers are implemented as much as possible in that language.
  3. A Layered Design: developers should be able to write their tests in the supported language of their choice, and these tests should work with all driver implementations. Therefore, the developer API exists as a thin wrapper around the core of each driver.
  4. driver implementations
  5. Reduction in Cost of Change: share as much code as possible between the driver projects. This is accomplished via the use of “Automation Atoms,” which are JavaScript libraries implementing various read-only DOM query tasks. These Atoms are used for two purposes. The first is to compose a monolithic driver (such as the FireFox driver) written primarily in JavaScript. The second is to augment existing drivers written in other languages with tiny fragments of highly compressed JavaScript. This augmentation will reduce execution and parsing time.
Section 2

Installation

Java

From http://code.google.com/p/selenium/downloads/list download the following two files and add them to your classpath (version numbers are the latest as of this writing):

  • selenium-server-standalone-2.0a7.jar contains the Selenium server (required to use Selenium 1.x classes) and the RemoteWebDriver server, which can be executed via java -jar.
  • selenium-java-2.0a7.zip contains the Java language bindings for Selenium 2.0. All of the necessary dependencies are bundled.

Java (Maven)

Add the following to your pom.xml:

​x
1
​
2
<dependency>
3
   <groupId>org.seleniumhq.selenium</groupId>
4
   <artifactId>selenium</artifactId>
5
   <version>2.0a7</version>
6
</dependency>
7
​
7
1
​
2
<dependency>
3
   <groupId>org.seleniumhq.selenium</groupId>
4
   <artifactId>selenium-server</artifactId>
5
   <version>2.0a7</version>
6
</dependency>
7
​

Ruby

Run the following command:

3
1
​
2
gem install selenium-webdriver
3
​

Python

Run one of the following commands:

3
1
​
2
easy_install selenium
3
​

or

3
1
​
2
pip install selenium
3
​

C#

Download selenium-dotnet-2.0a6.zip and add references to WebDriver.Common.dll and WebDriver.Firefox.dll (or your browser of choice) to your Visual Studio project.

Section 3

Driver Implementations

There are currently four official driver implementations for desktop browsers:

Firefox

The Firefox driver is the most mature of the browser-based drivers.

From Java:
3
1
​
2
WebDriver driver = new FirefoxDriver();
3
​
From Ruby:
3
1
​
2
driver = Selenium::WebDriver.for :firefox
3
​
From Python:
4
1
​
2
from selenium.firefox.webdriver import WebDriver
3
driver = WebDriver()
4
​
From C#:
3
1
​
2
IWebDriver driver = new FirefoxDriver();
3
​

Internet Explorer

The Internet Explorer driver has been tested and is known to work on IE 6, 7, and 8 on W indows XP and Vista. Compared to the other drivers, it is relatively slow.

From Java:
3
1
​
2
WebDriver driver = new InternetExplorerDriver();
3
​
From Ruby:
3
1
​
2
driver = Selenium::WebDriver.for :internet_explorer
3
​
From Python:
4
1
​
2
from selenium.ie.webdriver import WebDriver
3
driver = WebDriver()
4
​
From C#:
3
1
​
2
IWebDriver driver = new InternetExplorerDriver();
3
​

Chrome

The Chrome driver is comparatively new. Because Chrome is based on Webkit, you may be able to verify that your application works in other Webkit-based browsers such as Safari. However, because Chrome uses V8 JavaScript engine rather than the Safari Nitro engine, you may experience some differences in behavior.

From Java:
3
1
​
2
WebDriver driver = new ChromeDriver();
3
​
From Ruby:
3
1
​
2
driver = Selenium::WebDriver.for :chrome
3
​
From Python:
5
1
​
2
From Python:
3
from selenium.chrome.webdriver import WebDriver
4
driver = WebDriver()
5
​
From C#:
3
1
​
2
IWebDriver driver = new ChromeDriver();
3
​

HtmlUnit

The HtmlUnit driver is the fastest and most lightweight of the group. It is the only pure Java implementation, so it is the only solution that will run anywhere the JVM is available. Because it utilizes HtmlUnit to interact with your application instead of driving an actual browser, it carries along the associated baggage:

  • You cannot visually inspect what’s going on during your test.
  • HtmlUnit uses Rhino as its JavaScript+DOM implementation. No major browser does this, and it is disabled by default.
  • If you do enable JavaScript, the HtmlUnit Driver will emulate the behavior of Internet Explorer.

From Java:

3
1
​
2
WebDriver driver = new HtmlUnitDriver();
3
​

Although it isn’t recommended, it is possible to configure the HtmlUnitDriver to emulate a specific browser:

3
1
​
2
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
3
​
Section 4

Selenium RC Emulation

It is possible to emulate Selenium Remote Control (RC) using the WebDriver Java implementation on any of its supported browsers. This allows you to maintain both WebDriver and Selenium test assets side by side and to engage in an incremental migration from Selenium to WebDriver. In addition, running the normal Selenium RC server is not required.

12
1
​
2
WebDriver driver = new FirefoxDriver();
3
​
4
String baseUrl = “http://downforeveryoneorjustme.com/”;
5
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
6
​
7
selenium.open(“http://downforeveryoneorjustme.com/”);
8
selenium.type(“domain_input”, “google.com”);
9
selenium.click(“//div[@id=’container’]/form/a”);
10
​
11
assertTrue(selenium.isTextPresent(“It’s just you”));
12
​

On the downside, you may notice some inconsistencies in behavior since the entire Selenium API is not implemented (eg., keyPressNative is unsupported) and Selenium Core is emulated. You may also experience slower performance in some cases.

Section 5

Page Interaction Model

After navigating to a page…

3
1
​
2
driver.get(“http://www.google.com”)
3
​

…WebDriver offers a plethora of means for interacting with the elements on that page via its findElement methods. These methods accept an argument of type By, which defines several static methods implementing different means of element location:

Locators

Locator Example (Java)
id attribute By.id(“myElementId”)
name attribute By.name(“myElementName”)
XPATH By.xpath(“//input[@id=’myElementId’]”)
Class name By.className(“even-table-row”)
CSS Selector By.cssSelector(“h1[title]”)
Link Text By.linkText(“Click Me!”)
By.partialLinkText(“ck M”)
Tag Name By.tagName(“td”)

In each case, findElement will return an instance of WebElement, which encapsulates all of the metadata for that DOM element and allows you to interact with it.

WebElement Methods

Method Purpose
clear() Clears all of the contents if the element is a text entity.
click() Simulates a mouse click on the element.
getAttribute(String name) Returns the value associated with the provided attribute name (if present) or null (if not present).
getTagName() Returns the tag name for this element.
getText() Returns the visible text contained within this element (including subelements) if not hidden via CSS.
getValue() Gets the value of the element’s “value” attribute.
isEnabled() Returns true for input elements that are currently enabled; otherwise false.
isSelected() Returns true if the element (radio buttons, options within a select, and checkboxes) is currently selected; otherwise false.
sendKeys(CharSequence…
keysToSend)
Simulates typing into an element.
setSelected() Select an element (radio buttons, options within a select, and checkboxes).
submit() Submits the same block if the element if the element is a form (or contained within a form). Blocks until new page is loaded.
toggle() Toggles the state of a checkbox element.

In addition to this set of methods designed for interacting with the element in hand, WebElement also provides two methods allowing you to search for elements within the current element’s scope:

Method Purpose
findElement(By by) Finds the first element located by the provided method (see Locators table).
findElements(By by) Finds all elements located by the provided method.

WebDriver provides a support class named Select to greatly simplify interaction with select elements and their association options:

Method Purpose
selectByIndex(int index)/
deselectByIndex(int index)
Selects/deselects the option at the given index.
selectByValue(String value)/
deselectByValue(String value)
Selects/deselects the option(s) that has a value matching the argument.
selectByVisibleText(String text)/
deselectByVisibleTest(String text)
Selects/deselects the option(s) that displays text matching the argument.
deselectAll() Deselects all options.
getAllSelectedOptions() Returns a List<WebElement> of all selected options.
getFirstSelectedOption() Returns a WebElement representing the first selected option.
getOptions() Returns a List<WebElement> of all options.
isMultiple() Returns true if this is a multi-select list; false otherwise.

You can create an instance of Select fr om a WebElement that is known to represent a select element:

4
1
​
2
WebElement element = driver.findElement(By.id(“mySelect”));
3
Select select = new Select(element);
4
​

If the element does not represent a select element, this constructor will throw an UnexpectedTagNameException.

Interacting with Rendered Elements

If you’re driving an actual browser such as Firefox, you also can access a fair amount of information about the rendered state of an element by casting it to RenderedWebElement. This is also how you can simulate mouse-hover events and perform drag-and-drop operations.

4
1
​
2
WebElement element = driver.findElement(By.id(“header”));
3
RenderedWebElement renderedElement = (RenderedWebElement) element;
4
​

RenderedWebElement Methods

Method Purpose
dragAndDropBy(int moveRightBy,
int moveDownBy)
Drags and drops the element moveRightBy pixels to the right and moveDownBy pixels down. Pass negative arguments to move left and up.
dragAndDropOn(RenderedWeb
Element element)
Drags and drops the element on the supplied element.
getLocation() Returns a java.awt.Point representing the top left-hand corner of the element.
getSize() Returns a java.awt.Dimension representing the width and height of the element.
getValueOfCssProperty(String
propertyName)
Returns the value of the provided property.
hover() Simulates a mouse hover event over the element.
isDisplayed() Returns true if the element is currently displayed; otherwise false.
Section 6

Page Object Pattern

The Page Object pattern is a useful tool for separating the orthogonal concerns of testing the logical functionality of an application from the mechanics of interacting with that functionality. The Page Object pattern acts as an API to an HTML page and describes to the test writer the “services” provided by that page, while at the same time not exposing the underlying implementation. The Page Object itself is the only entity that possesses deep knowledge of the HTML’s structure.

Consider the Google home page. The user is able to enter search terms and perform two possible actions: to conduct the search or to automatically navigate to the top hit (“I’m Feeling Lucky). We could model this with the following Page Object (Java):

26
1
​
2
public class GoogleHomePage {
3
  private final WebDriver driver;
4
  public GoogleHomePage(WebDriver driver) {
5
        this.driver = driver;
6
​
7
        //Check that we’re actually on the Google Home Page.
8
        if (!”Google”.equals(driver.getTitle())) {
9
        throw new IllegalStateException(“This isn’t Google’s Home Page!”);
10
        }
11
  }
12
​
13
  public GoogleResultsPage search(String searchTerms) {
14
        driver.findElement(By.name(“q”)).sendKeys(searchTerms);
15
        driver.findElement(By.name(“btnG”)).submit();
16
​
17
        return new GoogleResultsPage(driver);
18
  }
19
  public UnknownTopHitPage imFeelingLucky(String searchTerms) {
20
        driver.findElement(By.name(“q”)).sendKeys(searchTerms);
21
        driver.findElement(By.name(“btnI”)).submit();
22
​
23
        return new UnknownTopHitPage(driver);
24
  }
25
}
26
​

Hot Tip

These examples assume the behavior of the Google home page prior to the introduction of Google Instant. For them to work properly, create a custom FireFox profile turn instant off in that profile, and then run your tests using that profile by setting the system property “firefox.browser.profile” to the name of your custom profile.

To learn more about the Page Object pattern, visit: http://code.google.com/p/selenium/wiki/PageObjects

WebDriver provides excellent support for implementing Page Object’s via its PageFactory. The PageFactory supports a “convention over configuration” approach to the Page Object pattern. By utilizing its initElements method, the driver element location code can be removed from the previous Page Object:

30
1
​
2
 public class GoogleHomePage {
3
        private final WebDriver driver;
4
        private WebElement q;
5
        private WebElement btnG;
6
        private WebElement btnI;
7
​
8
        public GoogleHomePage(WebDriver driver) {
9
          this.driver = driver;
10
​
11
        //Check that we’re actually on the Google Home Page.
12
        if (!”Google”.equals(driver.getTitle())) {
13
          throw new IllegalStateException(“This isn’t Google’s Home Page!”);
14
        }
15
  }
16
  public GoogleResultsPage search(String searchTerms) {
17
        q.sendKeys(searchTerms);
18
        btnG.submit();
19
​
20
        return new GoogleResultsPage(driver);
21
  }
22
​
23
  public UnknownTopHitPage imFeelingLucky(String searchTerms) {
24
        q.sendKeys(searchTerms);
25
        btnI.submit();
26
​
27
        return new UnknownTopHitPage(driver);
28
  }
29
}
30
​

A fully initialized version of this object can then be cr eated and used as in the following code snippet:

7
1
​
2
WebDriver driver = new HtmlUnitDriver(); //or your choice of driver
3
driver.get(“http://www.google.com”);
4
GoogleHomePage page = PageFactory.initElements(driver, GoogleHomePage.
5
class);
6
page.search(“WebDriver”);
7
​

This version of initElements will instantiate the class. It works only when there is a one-argument constructor that accepts a WebDriver instance or when the default no-argument constructor is present.

4
1
​
2
AnotherPageObject page = AnotherPageObject(“testing”, 1, 2, 3, driver);
3
PageFactory.initElements(driver, page);
4
​

We can further clean up the code by providing meaningful names for the form elements and then using annotations to declaratively specify the location strategy:

37
1
​
2
public class GoogleHomePage {
3
        private final WebDriver driver;
4
​
5
        @FindBy(name = “q”)
6
        private WebElement searchBox;
7
​
8
        @FindBy(name = “btnG”)
9
        private WebElement searchButton;
10
​
11
        @FindBy(name = “btnI”)
12
        private WebElement imFeelingLuckyButton;
13
​
14
        public GoogleHomePage(WebDriver driver) {
15
          this.driver = driver;
16
​
17
          //Check that we’re actually on the Google Home Page.
18
          if (!”Google”.equals(driver.getTitle())) {
19
                throw new IllegalStateException(“This isn’t Google’s Home Page!”);
20
          }
21
        }
22
​
23
        public GoogleResultsPage search(String searchTerms) {
24
          searchBox.sendKeys(searchTerms);
25
          searchButton.submit();
26
​
27
          return new GoogleResultsPage(driver);
28
        }
29
​
30
        public UnknownTopHitPage imFeelingLucky(String searchTerms) {
31
          searchBox.sendKeys(searchTerms);
32
          imFeelingLuckyButton.submit();
33
​
34
          return new UnknownTopHitPage(driver);
35
        }
36
}
37
​

The @FindBy annotation supports the same location methods supported by the programmatic API (except for CSS selectors):

@FindBy Annotation Location Methods

Locator Examples
id attribute @FindBy(how = How.ID, using = “myElementId”)
@FindBy(id = “myElementId)
name attribute @FindBy(how = How.NAME, using = “myElementName”)
@FindBy(name = “myElementName”)
id or name
attribute
@FindBy(how = How.ID_OR_NAME, using = “myElement”)
XPATH @FindBy(how = How.XPATH, using = “//input[@id=’myElementId’]”)
@FindBy(xpath = “//input[@id=’myElementId’]”)
Class name @FindBy(how = How.CLASS_NAME, using=”even-table-row”)
@FindBy(className = “even-table-row”)
Link Text @FindBy(how = How.LINK_TEXT, using=”Click Me!”)
@FindBy(linkText = “Click Me!”)
Partial Link Text @FindBy(how = How.PARTIAL_LINK_TEXT, using=”ck M”)
@FindBy(partialLinkText = “ck M”)
Tag Name @FindBy(how = How.TAG_NAME, using=”td”)
@FindBy(tagName = “td”)

XPath Support

WebDriver makes every effort to use a browser’s native XPath capabilities wherever possible. For those browsers that do not have native XPath support, WebDriver provides its own implementation. If you’re not familiar with the behavior of the various engines, this can lead to surprising results.

Driver Tag/Attribute Names Attribute Values Native XPath Support
HtmlUnitDriver Lower-cased As they appear in HTML Yes
InternetExplorerDriver Lower-cased As they appear in HTML No
FirefoxDriver Case insensitive As they appear in HTML Yes

Consider the following example:

4
1
​
2
<input type=”text” name=”pizza”/>
3
<INPUT type=”text” name=”pie”/>
4
​

The following behavior will result:

Section 7

Remote WebDriver

WebDriver provides excellent capabilities around driving browers located on remote machines. This allows the tests to run in one environment while simultaneously driving a browser in a completely different environment. In turn, running your tests in a continuous integration environment deployed on a Linux system while driving Internet Explorer on various flavors of Microsoft Windows is a straightforward proposition.

Driver Servlet

The RemoteWebDriver consists of a client and server. The server is simply a Java Servlet running within the Jetty Servlet Container (but you can deploy it to your container of choice). This servlet interacts with the various browsers. The client is an instance of RemoteWebDriver, which communicates with the Server via a JSON-based wire protocol.

Using RemoteWebDriver

First, download selenium-server-standalone-2.0a7.jar and run it on the machine you want to drive: java -jar selenium-server-standalone-2.0a7.jar

Next, implement a client in your language of choice:

35
1
​
2
package com.deepsouthsoftware.seworkshop;
3
import java.net.URL;
4
import org.openqa.selenium.*;
5
import org.junit.*;
6
import org.openqa.selenium.remote.*;
7
​
8
import static org.junit.Assert.*;
9
​
10
public class RemoteWebDriverTest {
11
  private WebDriver driver;
12
​
13
  @Before
14
  public void setUp() throws Exception {
15
        driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4444/wd/hub”),
16
        DesiredCapabilities.firefox());
17
  }
18
  @Test
19
  public void testCheese() throws Exception {
20
        driver.get(“http://www.google.com”);
21
        WebElement element = driver.findElement(By.name(“q”));
22
        element.sendKeys(“Cheese!”);
23
        Thread.sleep(1000); //Deal with Google Instant
24
        element = driver.findElement(By.name(“btnG”));
25
        element.click();
26
        Thread.sleep(1000); //Deal with Google Instant
27
        assertEquals(“cheese! - Google Search”, driver.getTitle());
28
  }
29
​
30
  @After
31
  public void tearDown() throws Exception {
32
        driver.quit();
33
  }
34
}
35
​

The constructor for RemoteWebDriver accepts two arguments:

  1. The URL for the remote server instance.
  2. A Capabilities object, which specifies the target platform and/or browser. DesiredCapabilities provides static factory methods for the commonly used choices (e.g., DesiredCapabilities.firefox()).
Section 8

Mobile Device Support

WebDriver provides excellent support for testing Web applications on modern mobile device platforms. Support for the following platforms are provided:

  • iOS (iPhone/iPad)
  • Android
  • Blackberry (5.0+)
  • Headless W ebKit

WebDriver is capable of running tests both in the platform emulators and the devices themselves. The driver implementations employ the same JSON-based wire protocol utilized by the RemoteWebDriver.

Driving the iOS Platform

The iPhone driver works via an iPhone application that implements the JSON-based wire protocol and then drives a UIWebView, which is a WebKit browser embeddable in iPhone applications.

  1. Install the iOS SDK from http://developer.apple.com/ios.
  2. Download the WebDriver source from http://code.google.com/p/webdriver/source/checkout.
  3. Open iphone/iWebDriver.xcodeproj in XCode.
  4. Set the build configuration’s active executable to iWebDriver.
  5. Click Build and Go.

The iOS simulator will launch with the iWebDriver app installed. You can then connect to the iW ebDriver app from your language of choice:

33
1
​
2
package com.deepsouthsoftware.seworkshop;
3
import java.net.URL;
4
import org.openqa.selenium.*;
5
import org.junit.*;
6
import org.openqa.selenium.remote.*;
7
​
8
import static org.junit.Assert.*;
9
​
10
public class IphoneWebDriverTest {
11
  private WebDriver driver;
12
​
13
  @Before
14
  public void setUp() throws Exception {
15
        driver = new RemoteWebDriver(new URL(“http://localhost:3001/hub”),
16
        DesiredCapabilities.iphone());
17
  }
18
​
19
  @Test
20
  public void testCheese() throws Exception {
21
        driver.get(“http://www.google.com”);
22
        WebElement element = driver.findElement(By.name(“q”));
23
        element.sendKeys(“Cheese!”);
24
        element.submit();
25
        assertEquals(“Cheese! - Google Search”, driver.getTitle());
26
  }
27
​
28
  @After
29
  public void tearDown() throws Exception {
30
        driver.quit();
31
  }
32
}
33
​

figure6

Driving the Android Platform

The Android driver works via an Android application that implements the JSON-based wire protocol and then drives an Android WebView, which is a WebKit browser embeddable in Android applications.

  1. Install the Android SDK from http://developer.android.com/sdk/index.html.
  2. Run ./android and install a target API (e.g. 2.2).
  3. Execute ./android create avd -n my_android -t 1 -c 100M to create a new Android Virtual Device targeting Android 2.2 with a 100M SD card. Do not create a custom hardware profile.
  4. Start the emulator: ./emulator -avd my_android &
  5. Install the Android WebDriver Application: ./adb -e install -r android-server.apk. You can download this from: http://code.google.com/p/selenium/downloads/list.
  6. Set up port forwarding: ./adb forward tcp:8080 tcp:8080

You can then connect to the AndroidDriver app from your language of choice:

33
1
​
2
package com.deepsouthsoftware.seworkshop;
3
​
4
import java.net.URL;
5
import org.openqa.selenium.*;
6
import org.junit.*;
7
import org.openqa.selenium.android.AndroidDriver;
8
​
9
import static org.junit.Assert.*;
10
​
11
public class AndroidWebDriverTest {
12
  private WebDriver driver;
13
​
14
  @Before
15
  public void setUp() throws Exception {
16
        driver = new AndroidDriver();
17
  }
18
​
19
  @Test
20
  public void testCheese() throws Exception {
21
        driver.get(“http://www.google.com”);
22
        WebElement element = driver.findElement(By.name(“q”));
23
        element.sendKeys(“Cheese!”);
24
        element.submit();
25
        assertEquals(“Cheese! - Google Search”, driver.getTitle());
26
  }
27
​
28
  @After
29
  public void tearDown() throws Exception {
30
        driver.quit();
31
  }
32
}
33
​

Like This Refcard? Read More From DZone

related article thumbnail

DZone Article

How to Perform Custom Error Handling With ANTLR
related article thumbnail

DZone Article

Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
related article thumbnail

DZone Article

How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
related article thumbnail

DZone Article

Role of Cloud Architecture in Conversational AI
related refcard thumbnail

Free DZone Refcard

Platform Engineering Essentials
related refcard thumbnail

Free DZone Refcard

The Essentials of GitOps
related refcard thumbnail

Free DZone Refcard

Continuous Integration Patterns and Anti-Patterns
related refcard thumbnail

Free DZone Refcard

Getting Started With CI/CD Pipeline Security

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: