Selenium Load Testing & Performance Monitoring Solution 1. 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

This DZone Refcard is brought to you by...

Selenium Load Testing & Performance


Monitoring Solution
TestMakerTM is the Open Source Test (OST) solution to:

1. Repurpose Your Selenium Scripts


To Be Functional Tests, Load and
Performance Tests, and Business
Service Production Monitors

2. Advanced Selenium Functions


Not Available Anywhere Else. For
Example, Data-enable Your
Selenium Scripts With No Coding.
And advanced reporting.

3. Run Your Selenium Scripts in a


PushToTest is the trusted Selenium partner to Ford, Grid or Cloud, or Both! Inside
PepsiCo, Intuit, Cisco, and Deutsche Bank. or Outside Your Firewall. Up to
Millions of Virtual Users. No
PushToTest Global Services delivers Selenium
Coding. And advanced reporting.
training, consulting, integration, and professional
technical support. We provide on-site, near-shore,
and off-shore experts. PushToTest training moves The free live webinar will be held on
your team from manual to automated testing and off the following dates:
proprietary test tools to Open Source Testing (OST.)
January 18, 2011, Tuesday, 8 am to
9 am (Pacific, California Time)
REGISTER FOR FREE
http://seleniumworkshop.com February 1, 2011, Tuesday, 8 am to
9 am (Pacific, California Time)
February 15, 2011, Tuesday, 8 am
to 9 am (Pacific, California Time)

For more information visit http://www.pushtotest.com


about PushToTest products and services or contact PushToTest sales at sales@pushtotest.com
or call +1 408 871 0122 (USA, California Pacific Time)

DZon
brought to you by...
#125

Get More Refcardz! Visit refcardz.c om Selenium 2.0:


CONTENTS INCLUDE:

What is Selenium 2.0?

Architecture

Installation

Driver Implementations

Page Interaction Model
Using the Webdriver API to Create Robust User Acceptance Tests

Mobile Device Support and more...
By Matt Stine

Therefore, the developer API exists as a thin wrapper


W H AT I S S E L E N I U M 2 . 0 ? around the core of each driver.

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 4. Reduction in Cost of Change: share as much code as
JavaScript application inside the targeted browser, WebDriver possible between the driver projects. This is
drives the browser directly through the most sensible means accomplished via the use of “Automation Atoms,”
available. For Firefox, this meant using JavaScript wrapped which are JavaScript libraries implementing various
in an XPCOM component; however, for Internet Explorer, this read-only DOM query tasks. These Atoms are used for
meant using C++ to drive IE’s Automation APIs. two purposes. The first is to compose a monolithic driver
(such as the FireFox driver) written primarily in JavaScript.
The merger of these two projects allows each technology’s
The second is to augment existing drivers written in other
strengths to mitigate the other’s weaknesses. For example,
Selenium has good support for most common use cases, but languages with tiny fragments of highly compressed
w w w.d z o n e.co m

JavaScript. This augmentation will reduce execution and


WebDriver opens up many additional possibilities via its ability
parsing time.
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 I N S TA L L AT I O N
tool for the job.
Java
Selenium 2.0 currently supports writing cross-browser tests in
From http://code.google.com/p/selenium/downloads/list
Java (and other languages on the JVM including Groovy), download the following two files and add them to your
Python, Ruby, and C#. It currently allows developers to target
classpath (version numbers are the latest as of this writing):
Firefox, Internet Explorer, and Chrome with automated tests.
• selenium-server-standalone-2.0a7.jar contains the
It also can be used with HtmlUnit for “headless” application
testing. Furthermore, driver implementations exist for the iOS Selenium server (required to use Selenium 1.x classes)
and the RemoteWebDriver server, which can be
(iPhone/iPad), Android, and BlackBerry platforms.
executed via java -jar.
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
Selenium 2 . 0

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.

DZone, Inc. | w ww. dz o ne . c o m


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

• selenium-java-2.0a7.zip contains the Java language From C#:


bindings for Selenium 2.0. All of the necessary I Web D river dr iver = n ew I n ter n et Expl orer D river ( ) ;

dependencies are bundled.


Java (Maven) Chrome
Add the following to your pom.xml: The Chrome driver is comparatively new. Because Chrome
is based on Webkit, you may be able to verify that your
<dependency>
<groupId>org.seleni umhq.selenium</ groupId > application works in other Webkit-based browsers such as
< a r t i f a c t I d > s e l e n i u m < / a r t i fa c t I d >
<versi on >2 .0 a7< / versi on >
Safari. However, because Chrome uses V8 JavaScript engine
</dependency> rather than the Safari Nitro engine, you may experience some
differences in behavior.
<dependency>
<groupId>org.seleni umhq.selenium</ groupId > From Java:
<artifact Id>selenium-server</ artifactI d>
<versi on >2 .0 a7< / versi on > WebDriver driver = new ChromeDriver();
</dependency>

From Ruby:
Ruby
d r ive r = S elen i u m:: Web D river.for :c hro me
Run the following command:
g e m i n s t all s ele n i u m - web d r iver From Python:
from seleni um.chrome.webd river import WebDriver
Python d river = Web Driver( )
Run one of the following commands:
From C#:
easy_inst all selenium
IWebDriver driver = new ChromeDriver( );
or
pip in stall s elen ium
HtmlUnit
The HtmlUnit driver is the fastest and most lightweight of
C# the
Download selenium-dotnet-2.0a6.zip and add references to group. It is the only pure Java implementation, so it is the
WebDriver.Common.dll and WebDriver.Firefox.dll (or your only solution that will run anywhere the JVM is available.
browser of choice) to your Visual Studio project. Because it utilizes HtmlUnit to interact with your application
instead of driving an actual browser, it carries along the
associated baggage:
D R I V E R I M P L E M E N TAT I O N S
• You cannot visually inspect what’s going on during
There are currently four official driver implementations for your test.
desktop browsers: • HtmlUnit uses Rhino as its JavaScript+DOM
implementation. No major browser does this, and it is
Firefox
disabled by default.
The Firefox driver is the most mature of the browser-based
drivers. • If you do enable JavaScript, the HtmlUnit Driver will
emulate the behavior of Internet Explorer.
From Java:
WebDr iver d river = new Fi refoxDriver ( ) ;
From Java:
WebDriver driver = new HtmlUni tDriver();
From Ruby:
d r i v e r = S e l e n i u m : : We b D r i v e r. f o r : f i r e f o x
Although it isn’t recommended, it is possible to configure
the
From Python: HtmlUnitDriver to emulate a specific browser:
f r o m s e l e n i u m . f i r e f o x . w e b dr i v e r i m p o r t We b D r i v e r HtmlUnit Driver driver = new HtmlUnitDriver(BrowserVersion.FIR EFOX_3);
d river = Web Dr iver( )
S E L E N I U M R C E M U L AT I O N
From C#:
IWeb Dr iver d r iver = n ew Fi refo xDr iver( ) ;
It is possible to emulate Selenium Remote Control (RC)
Internet Explorer using the WebDriver Java implementation on any of its
The Internet Explorer driver has been tested and is known to supported browsers. This allows you to maintain both
work on IE 6, 7, and 8 on Windows XP and Vista. Compared to WebDriver and Selenium test assets side by side and to
the other drivers, it is relatively slow. engage in an incremental migration from Selenium to
WebDriver. In addition, running the normal Selenium RC
From Java: server is not required.
Web D river d rive r = n ew I n ter n et Exp lo rer Dr iver( ) ;
Web Driver driver = n ew F irefoxD river( ) ;
From Ruby:
S t r i n g b as eUr l = “h t tp :/ / d ow n fo rever yon eo rju st m e.c om / ”;
d r i ve r = S e l e n i u m : : We b D r i ve r. fo r : i n te r n e t _ e x p l o re r Seleni um selenium = new WebDriverBackedSeleni um( driver, baseUrl);

s eleni u m.op en ( “ht tp :/ / dow n foreveryon eorjus tme.c om/ ”);


From Python: s e l e n i u m . t y p e ( “ d o m a i n _ i n p u t ”, “ g o o gl e . c o m” ) ;
s e l e n i u m . c l i c k ( “ / / d i v [ @ i d = ’ c o n t a i n e r ’ ] / fo r m / a” ) ;
from s elen i um.i e.web driver i mpo rt WebDr iver
d rive r = Web Driver( ) a s s e r t T r u e ( s e l e n i u m . i s Te x t P r e s e n t ( “ I t ’s j u s t y o u ” ) ) ;

DZone, Inc. | w ww. d z o ne . c o m


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

On the downside, you may notice some inconsistencies in greatly simplify interaction with select elements and their
behavior since the entire Selenium API is not implemented (eg., association options:
keyPressNative is unsupported) and Selenium Core is emulated.
Method Purpose
You may also experience slower performance in some cases.
selectByIndex(int index)/ Selects/deselects the option at the given index.
deselectByIndex(int index)
PA G E I N T E R A C T I O N
selectByValue(String value)/ Selects/deselects the option(s) that has a value
MODEL matching
deselectByValue(String value) the argument.

After navigating to a page… selectByVisibleText(String text)/ Selects/deselects the option(s) that displays text
matching
d r iver. g et ( “h t t p :/ / w w w. g o o g l e. c o m” ) deselectByVisibleTest(String text) the argument.

deselectAll() Deselects all options.


…WebDriver offers a plethora of means for interacting with
getAllSelectedOptions() Returns a List<WebElement> of all selected
the elements on that page via its findElement methods. options.
These methods accept an argument of type By, which defines getFirstSelectedOption() Returns a WebElement representing the first selected
several static methods implementing different means of option.

element location: getOptions() Returns a List<WebElement> of all options.

isMultiple() Returns true if this is a multi-select list; false otherwise.


Locators
Locator Example (Java) You can create an instance of Select from a WebElement that
id attribute By.id(“myElementId”)
is
Select select = new Select( elemen t) ;
known to represent a select element:
name attribute By.name(“myElementName”)

XPATH By.xpath(“//input[@id=’myElementId’]”) We b E l e m e n t e l e m e n t = d r i v e r. f i n d E l e m e n t ( B y. i d ( “ m y S e l e c t ” ) ) ;

Class name By.className(“even-table-row”)

CSS Selector By.cssSelector(“h1[title]”) If the element does not represent a select element, this
Link Text By.linkText(“Click Me!”) constructor will throw an UnexpectedTagNameException.
By.partialLinkText(“ck
M”)
Interacting with Rendered Elements
Tag Name By.tagName(“td”)
If you’re driving an actual browser such as Firefox, you also can
also how you can simulate mouse-hover events and perform
In each case, findElement will return an instance of access a fair amount of information about the rendered state
WebElement, which encapsulates all of the metadata for that of an element by casting it to RenderedWebElement. This is
DOM element and allows you to interact with it. RenderedWebElement renderedElement = (RenderedWebElement) element;
drag-and-drop operations.
WebElement Methods
We b E l e m e n t e l e m e n t = d r i v e r. f i n d E l e m e n t ( B y. i d ( “ h e a d e r ” ) ) ;
Method Purpose

clear() Clears all of the contents if the element is a text entity.


RenderedWebElement
int moveDownBy)
Methods
the right and moveDownBy pixels down. Pass negative
click() Simulates a mouse click on the element.
Method Purpose
getAttribute(String name) Returns the value associated with the provided attribute name
(if present) or null (if not present). dragAndDropBy(int moveRightBy, Drags and drops the element moveRightBy pixels to

getTagName() Returns the tag name for this arguments to move left and up.
element.
dragAndDropOn(RenderedWeb Drags and drops the element on the supplied element.
getText() Returns the visible text contained within this element
(including subelements) if not hidden via CSS. Element element)
getLocation() height ofa the
Returns element. representing the top left-hand
java.awt.Point
getValue() Gets the value of the element’s “value” attribute.
corner of the element.
isEnabled() Returns true for input elements that are currently enabled;
getSize()
propertyName) Returns a java.awt.Dimension representing the width and
otherwise false.

isSelected() Returns true if the element (radio buttons, options within a


select, and checkboxes) is currently selected; otherwise false. getValueOfCssProperty(String Returns the value of the provided property.

sendKeys(CharSequence… Simulates typing into an element.


keysToSend) hover() Simulates a mouse hover event over the element.

setSelected() Select an element (radio buttons, options within a select, and isDisplayed() Returns true if the element is currently displayed;
checkboxes). PA G E O Botherwise
J E C T false.
PAT T E R N
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. The Page Object pattern is a useful tool for separating the
toggle() Toggles the state of a checkbox element. orthogonal concerns of testing the logical functionality of an
application from the mechanics of interacting with that
In addition to this set of methods designed for interacting with functionality. The Page Object pattern acts as an API to an HTML
the element in hand, WebElement also provides two methods page and describes to the test writer the “services” provided by
allowing you to search for elements within the current element’s that page, while at the same time not exposing the underlying
scope: implementation. The Page Object itself is the only entity that
Method Purpose
possesses deep knowledge of the HTML’s structure.

findElement(By by) Finds the first element located by the provided method (see Consider the Google home page. The user is able to
Locators table).
enter search terms and perform two possible actions: to
findElements(By by) Finds all elements
located by the provided method.
conduct the search or to automatically navigate to the

WebDriver provides a support DZone, Inc. | w ww. d z o ne . c o m


class named Select to
4 Selenium 2.0: Using the WebDriver API to Create Robust User Acceptance Tests

top hit (“I’m Feeling Lucky). We could model this with the a WebDriver instance or when the default no-argument
following Page Object (Java): constructor is present.
pub li c class GoogleH omePage { AnotherPageObject page = A n o t h e r P a g e O b j e c t ( “ t e s t i n g ” , 1 , 2 , 3 , d r i v e r ) ;
p r i v a t e f i n a l We b D r i v e r d r i v e r ; P a g e Fa c t o r y. i n i t E l e m e n t s ( d r i v e r , p a g e ) ;

p ublic GoogleH omePage( WebDriver d river) {


this.d river = d river; We can further clean up the code by providing meaningful
/ /C heck t hat we’re act ually on t h e G oogle H ome names for the form elements and then using annotations to
P a g e . i f ( ! ” G o o g l e ”. e q u a l s ( d r i v e r. g e t T i t l e ( ) ) ) {
t h r o w n e w I l l e g a l S t a t e E x c e p t i o n ( “ T h i s i s n’ t G o o g l e ’ s H o m e P a g e ! ” ) ;
declaratively specify the location strategy:
}
} pub li c class GoogleHomePage {
p r i v a t e f i n a l We b D r i v e r d r i v e r ;
p u b li c G o o gl e Re s u lt s Pa g e s ea rc h ( S t r i n g s e arc h Ter m s )
{ d r i v e r. f i n d E l e m e n t ( B y. n a m e ( “ q ” ) ) . s e n d K e y s ( s e a r c h Te r m s @FindBy(name = “q”)
) ; d r i v e r. f i n d E l e m e n t ( B y. n a m e ( “ b t n G ” ) ) . s u b m i t ( ) ; private WebElement searchBox;

ret u r n n e w G oo gl eR e s u l t s Pa g e( d r i ver ) ; @FindBy(name = “btnG”)


} private WebElement searchButton ;

public UnknownTopHi tPage imFeeling Lucky( String searchTerms) @FindBy(name = “btnI”)


{ d r i v e r . f i n d E l e m e n t ( B y. n a m e ( “ q ” ) ) . s e n d K e y s ( s e a r c h Te r m s ) ; private WebElement
d r i v e r. f i n d E l e m e n t ( B y. n a m e ( “ b t n I ” ) ) . s u b m i t ( ) ; imFeeling LuckyButton;
public GoogleH omePage( WebDriver driver) {
return new UnknownTopHitPage(d river) ; this.d river = d river;
}
} / / C heck t hat we’ re act ually on th e Goo gle H ome Pag e.

i f t h r o w n e w I l l e g a l S t a t e E xc e p t i o n ( “ T h i s i s n’ t G o o g l e ’ s H o m e P a g e ! ” ) ;
}( ! ” G o o g l e ”. e q u a l s ( d r i v e r. g e t T
These examples assume the behavior of the Google itle())) {

home page prior to the introduction of Google p u b l i c Go o gl eR e s u l t s Pag e s ea rc h ( S t r i n g s ea rc h Te r m s ) {


} s e a r c h B o x . s e n d K e y s ( s e a r c h Te r m s ) ;
Instant. For them to work properly, create a custom
Hot
FireFox profile turn instant off in that profile, and
Tip ret u r n n e w G o o gl e Re s u lt s Pa g e( d r iver ) ;
then run your tests using that profile by setting s e a rc h B u t t o n . s u b m i t ( ) ;

the system property “firefox.browser.profile” to public UnknownTopHit Page imFeeling Lucky(S tring
searchTerms)
} {
the name of your custom profile. i m Feeli n g Lu ck yB ut ton .s u b mi t ( ) ;
s e a r c h B o x . s e n d K e y s ( s e a r c h Te r m s
To learn more about the Page Object pattern, visit: );
return new UnknownTopHitPag e( driver);

http://code.google.com/p/selenium/wiki/PageObjects }

WebDriver provides excellent support for implementing Page The} @FindBy annotation supports the same
Object’s via its PageFactory. The PageFactory supports a supported by the programmatic API (except for CSS selectors):
location methods
“convention over configuration” approach to the Page Object
pattern. By utilizing its initElements method, the driver element
@FindBy Annotation Location Methods
location code can be removed from the previous Page Object:
Locator Examples
publi c class GoogleH omePag e {
p r i v a t e f i n a l We b D r i v e r d r i v e r ; id attribute @FindBy(how = How.ID, using =
“myElementId”)
p rivate WebElement q; @FindBy(id = “myElementId)
p rivate WebElement btn G;
p rivate WebElement btn I; name attribute @FindBy(how = How.NAME,
attribute
using = “myElementName”)
p ubli c @FindBy(name = “myElementName”)
GoogleHomePage( WebDriv
er driver) { id or name @FindBy(how = How.ID_OR_NAME,
this.driver = d river;
using = “myElement”)
// Ch eck that we’re actually on the Google H ome Pag e.
i f ( ! ” G o o g l e ”. e q u a l s ( d r i ve r. g e t T i t l e ( ) ) ) {
t h r o w n e w I l l e g a l S t a t e E xc e p t i o n ( “ T h i s i s n’ t G o o g l e ’ s H o m e P a g e ! ” ) ; XPATH @FindBy(how = How.XPATH, using
} = “//input[@id=’myElementId’]”)
} @FindBy(xpath =
“//input[@id=’myElementId’]”)
p u b l i c G o o g l eR e s u l t s P ag e s e a rc h ( S t r i n g s ea rc h Te r m s ) {
q . s e n d K e y s ( s e a r c h Te r m s ) ; Class name @FindBy(partialLinkText = “ck M”)
@FindBy(how = How.CLASS_NAME,
btnG.submit(); using=”even-table-row”)
@FindBy(className = “even-table-
ret u r n n e w G oo gl eR e s u l t s Pa g e( d r i ver ) ; @FindBy(tagName = “td”)
row”)
}
Link Text @FindBy(how = How.LINK_TEXT,
publi c UnknownTopHitPag e imFeeli ngLucky(S tri ng searchTerms) {
q . s e n d K e y s ( s e a r c h Te r m s ) ; using=”Click
X PAT HMe!”)
SUPPORT
btnI.submit(); @FindBy(linkText = “Click Me!”)

return new UnknownTopHitPage(d river) ; Partial Link Text @FindBy(how =


} WebDriver makes every
How.PARTIAL_LINK_TEXT, effort
using=”ck M”) to use a browser’s native XPath
} capabilities wherever possible. For those browsers that do
A fully initialized version of this object can then be created not have native @FindBy(how
Tag Name XPath support,
= WebDriver provides its own
How.TAG_NAME, using=”td”)
and implementation. If you’re not familiar with the behavior of the
used as in the following code snippet: various engines, this can lead to surprising results.

Web D ri ver d r iver = n ew H t mlU n i t D river ( ) ; / / o r you r c h oi c e of d river Driver Tag/Attribute Attribute Values Native
d rive r. g et ( “h t t p :/ / w w w.g oog le.c om”) ; XPath Names Support
Goo gleH o mePag e p ag e = Pag e Facto ry.i n i t Elem en ts ( d r iver, Goo gleH o mePag e.
c lass ) ;
p a g e. s e a rc h ( “ We b D r i ve r ” ) ; HtmlUnitDriver Lower-cased As they appear in HTML
Yes

This version of initElements will instantiate the class. It works InternetExplorerDriver Lower-cased As they appear in HTML
No
only when there is a one-argument constructor that accepts
DZone, Inc. | FirefoxDriver
w ww. d z o ne . c o m Case insensitive As they appear in HTML
Yes
5 Selenium 2.0: Using the WebDriver API to Create Robust User Acceptance Tests

Consider the following example: The constructor for RemoteWebDriver accepts two arguments:
< i n p ut t yp e= ”text ” n ame= ”p i z z a”/ > 1) The URL for the remote server instance.
<INPUT t y p e = ” t e x t ” name=”pie”/>
2) A Capabilities object, which specifies the target
The following behavior will result: platform and/or browser. DesiredCapabilities provides
XPath Number of Matches static factory methods for the commonly used choices
Expression
HtmlUnitDriver InternetExplorerDriver FirefoxDriver (e.g., DesiredCapabilities.firefox()).

//input 1 2 1
MOBILE DEVICE SUPPORT
//INPUT 0 2 0

WebDriver provides excellent support for testing Web


RE M OT E WE B D RI V E R applications on modern mobile device platforms. Support for
the following platforms are provided:
WebDriver provides excellent • iOS (iPhone/iPad)
capabilities around driving • Android
browers located on remote • Blackberry (5.0+)
machines. This allows the tests • Headless WebKit
to run in one environment
WebDriver is capable of running tests both in the platform
while simultaneously driving a
emulators and the devices themselves. The driver
browser in a completely different
implementations employ the same JSON-based wire protocol
environment. In turn, running
utilized by the RemoteWebDriver.
your tests in a continuous
integration environment deployed Driving the iOS Platform
on a Linux system while driving The iPhone driver works via an iPhone application that
Internet Explorer on various implements the JSON-based wire protocol and then drives
flavors of Microsoft Windows is a a UIWebView, which is a WebKit browser embeddable in
straightforward proposition. iPhone applications.
The RemoteWebDriver consists of 1) Install the iOS SDK from http://developer.apple.com/ios.
a client and server. The server is 2) Download the WebDriver source from
simply a Java Servlet running within the Jetty Servlet Container http://code.google.com/p/webdriver/source/checkout.
(but you can deploy it to your container of choice). This servlet
3) Open iphone/iWebDriver.xcodeproj in XCode.
interacts with the various browsers. The client is an instance of
RemoteWebDriver, which communicates with the Server via a 4) Set the build configuration’s active executable
JSON-based wire protocol. to iWebDriver.

Using RemoteWebDriver 5) Click Build and Go.


First, download selenium-server-standalone-2.0a7.jar and run it The iOS simulator will launch with the iWebDriver app
on the machine you want to drive: installed. You can then connect to the iWebDriver app from
java -jar selenium-server-standalone-2.0a7.jar your language of choice:
Next, implement a client in your language of choice: package com.deepsouthsoftware.seworkshop;

i m p o r t j ava . n e t . U R L ;
package com.deepsouthsoftware.seworkshop;
i m p o r t o r g . o p e n q a . s e le n i u m. * ;
import org .junit.*;
import j ava . n e t . U R L ;
i m p o rt o r g . o p en q a. s ele n i u m .re mo te. * ;
import o r g . o p en q a . s el e n i u m . * ;
import org .junit.*;
import static org.junit.Assert.*;
i mp o r t o rg . o p en q a. s e len i u m. rem ote .* ;
p u bli c cl ass I p h on eWebD ri verTest {
import stat ic org.junit.Assert.*;
p r ivat e Web D r ive r d r ive r ;
p ubli c class RemoteWebDr iverTes t {
@Before
p r i va te We b D r i ver d r i ver ;
p u b l i c vo i d s e t U p ( ) t h ro w s E xc e p t i o n {
driver = new RemoteWebDriver( new URL(“http:// localhost:3001/hub”) ,
@Before
Des iredCap abilities.iphon e() );
p u b l i c vo i d s e t U p ( ) t h ro ws E xc e p t i o n {
}
driver = new RemoteWebDriver(new UR L(“http://127.0.0.1:4444/wd/hub”),
DesiredCapabilities.firefox());
} @ Tes t
p u b l i c v o i d t e s t C h e e s e ( ) t h ro w s
E xc e p t i o net(
d river.g { “ht t p:/ / w ww.g oogle.co m”) ;
@Test
p u b l i c vo i d t e s t C h e e s e ( ) t h r o ws E xc e p t i o n {
Weebl eElmeemnet .nt s eenldeK
meeynst ( “=C h e e s e ! ” ) ;
d r iver. g et ( “ h t t p :/ / w w w. g o o g l e. c o m” ) ;
d r i ve r. f i n d El e m e nt ( B y. n a m e ( “ q” ) ) ;
We bE l e m e nt e l em e nt = d r i v e r. f i n dE l em en t ( B y. n a m e ( “ q” ) ) ;
el emen t .s en dK ey s( “C h eese! ”); assertEquals( “Cheese! - Google
e l eSme ae rncth. s”,u bdmriitv(e) r.
; get Tit le());
Th read . sleep ( 10 00 ) ; / / D ea l w i t h G oo g l e I n s t a n t
e l em en t = d r i v e r. f i n d E l e m e n t ( B y. n a m e ( “ b t n G ” ) ) ;
}
element.click();
Thread.s leep ( 100 0) ; // Deal wi t h Goo gle I ns tan t @After
a s s e r t E q u a l s ( “ c h e e s e ! - G o o g l e S e a r c h ”, d r i ve r. g e t T i t l e ( ) ) ;
p ub li c vo i d tear Do w n ( ) t hro ws Exc ept i on {
} d r i ve r. q u i t ( ) ;

@After } }
p u b li c vo i d tearD ow n ( ) t h ro ws Exc ep t i o n {
d r i v e r. q u i t ( ) ;
}
}

DZone, Inc. | w ww. d z o ne . c o m


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

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:
package com.deepsouthsoftware.seworkshop;

import j ava . n e t . U R L ;
import o r g . o p en q a . s e le n i u m . * ;
import org.junit.*;
i mp o r t o rg . o p en q a. s e len i u m. a n d ro i d . A n d ro i d D ri ver ;

import static org.junit.Assert.*;

Driving the Android Platform p u bli c c lass A n d roi d Web D river Tes t {
p r i va te Web D r ive r d r iver ;
The Android driver works via an Android application that
@Before
implements the JSON-based wire protocol and then drives an p u b l i c vo i d s et U p ( ) t h ro ws Exc ep t i o n {
d r ive r = n ew A n d ro i d D river ( ) ;
Android WebView, which is a WebKit browser embeddable in }
Android applications. @Test
p u b l i c vo i d t e s t C h e e s e ( ) t h ro ws E xc e p t i o n {
1) Install the Android SDK from d r ive r. g e t ( “h t t p : / / w w w. g o o gl e. c o m” ) ;
We bE l e m e nt e l e m e nt = d r i v e r. f i n dE l em e nt ( B y. n a m e ( “ q” ) ) ;
http://developer.android.com/sdk/index.html. e lem en t . sen dK ey s (“C h ees e!”);
element.submit();
2) Run ./android and install a target API (e.g. 2.2). }
a s s e r t E q u a l s ( “ C h e e s e ! - G o o g l e S e a r c h ”, d r i v e r. g e t T i t l e ( ) ) ;

3) Execute ./android create avd -n my_android -t 1 @After


-c p ub li c vo i d tear Do w n ( ) t hrows Exc ept i on {
d r i v e r. q u i t ( ) ;
100M to create a new Android Virtual Device targeting }
}
Android 2.2 with a 100M SD card. Do not create a custom
hardware profile.

A BOU T THE AU THOR RECOMMENDED BOOK


Matt Stine is the Group Leader of Research Application Two of the industry’s most experienced agile testing
Development at St. Jude Children’s Research Hospital in practitioners and consultants, Lisa Crispin and J ane t Gregory,
Memphis, TN. For the last decade he has been developing have t ea me d up to bring you the definitive answers to these
and supporting enterprise Java applications in support of questions and many others. In Agile Testing, Crispin and
life sciences research. Matt appears frequently on the No Gregory define agile testing and illustrate the tester’s role with
Fluff Just Stuff symposium series tour, as well as at other examples from real agile teams. They teach you how to use the
conferences such as JavaOne, SpringOne/2GX, The Rich agile testing quadrants to identify what testing is n e e d e d , who

Web Experience, and The Project Automation Experience. He is an Agile Zone should do it, and what tools might help. The book chronicles an agile software
Leader for DZone, and his articles also appear in GroovyMag and NFJS the development iteration from the viewpoint of a tester and explains the seven
Magazine. When he’s not on the road, Matt also enjoys his role as President key success factors of agile testing.
of the Memphis/Mid-South Java User Group. His current areas of interest
include lean/agile software development, modularity and OSGi,
Groovy/Grails, JavaScript development, and automate d testing of modern
web applications. Find him on Twitter at http://www.twitter.com/mstine and
read his blog at http://www.mattstine.com.

#82

C O N TEN TS
IA

N bC oL uU tDCE l:o u d C o m p u t i n g
U sa g e
n
Getting Started
Browse our collection of over 100 Free Cheat Sheets
with
Aldo
#64

S c e n ayouby...
ri o s ®
■C ost

Upcoming Refcardz
D a t a T i e r Te c h n o l o g i ee.Comply.
oughtto s

Cloud Computing

Ge t More Re fc ardz ! Visit

br
P l a t f o r m M a nChaa ngge.Collaborat

a Un ndd m
■ ero
ement
l yrien. .g. C o n c e p t s B y Dan iel
Ru bi o m
o
a l s o m i n i cm i z e sC Ot hNe n e e d t o m a k e d e s i g n
A B O UT C L O UD C O M P UT IN G c hn ae n tgi emse tzeo. v seun ptHT
o
TE N TS IN C L U
sp .o r t

re f c ard z.co m

Web a pp l i ca t i o n s h a ve a l wa ys b ee n Au tom ated g ro wth & scal abl e te c hn o l o g i e s

uou
Windows Phone 7
H a v i n g t h e ca p a b i l i t y to s u p p o r t o n e

o nt in
d e p l o y e d o n se rv er s c o n n e c te d to
t im e e v e n t s, c l o u d c o m p u t i n g
wh at is n ow de emed the ‘clo ud ’. f al ca et f o r m s a l s o f a c i l i t a t e t h e g r a d u a l

C
p

Patter Core H
s ha s c han g ed su bsta ntially in recen t d ro w t h c u r v e s
g tio n
u ou

erns
E N Tonn t in a t E v e ry
S
b y web a p pl ic ati o ns.
O bNogTuratDtCio
E :ft w a re
H
y eoawr se, vees r,p et hc iea ldl ey mwai nt hd ts h ae n edn t r a n c e o f

T
CA
te LU dg Seo n d A n t
i- t e c h n o l o g y u s e d o n s u c h se rv e rs

ML
In
I NB Cuail

CSS3
serv ic e pro vi d ers l i ke Ama zo n, Go og l e
C h ttn e rn ns a
a e r s L a rg e s c a l e g ro w t h s c e n a ri o s i n v o l v i n g

pPa t trsiro
o nl
tahn adt M
a di ac pr ot saonfdt . s c a l e t o l a r g e u s e r b a s e s , s p e c i a l i z e d efq u i p m

Valid
e n ta ( e . g . l o a d
CVeo ntd e me nte s and Pattern ■

u il a g t ic PrivatRepo
MB aunil d...P ra c

e
e
m a k in g th em k n o wl e d g e a b le i n m a n y b a l a n ce rs a n d c l u s t e rs ) a re a l l b u t
mBo re
Ma
■ sit
aT
T shhpiees scR
et esc forcem
a lrpad ta ew
ndii letl osi hnctalrvoo eud dul occenogtmo p yuot ui n tgo . TM L Th By An
ATIO N soft
Cinli dc leopuldo yc oe m d pwu et ibn ag p, pwli it ch a at in o n s a b s t r a c t e d a wa y b y r e l y i n g o n a c l o u d
M
fi l ee sr c a t t
an d tcha n b r i b u t Ha rris dy

WebDriver
D
INTEGR l din g c o m p u t i nr g p l aHt f o Lr m
Basics
’ s t e c h n oE l: o g y . opm tt hh a t i e a l te f o u e d e sc r i
UOUS s o f bui tr u n ea vi ms a dg ies p laat t r inbdu,t

w ww. dzone . c om

CON TIN pro ces version con e m od


ac TM L vs X t id
. e
en
ai l a i s y ed e
b es
wh er
is the ject ’s ap p licat i H i f d e s c ri b
ABO UT
n ble. e th
rdz!

io n (CI) to a pro ons. ik e P


HP
Ne s te
d ta g e s al e
t e rna i m ag e
Inte grat I n ai d d i t i o n , s e v e r a l c l o u d c o m p u t i n g
Ta
it ted b s
uo us com m on to
a proe m US A GE S C E N A R IO S L Tag te te
Con tin ry chan g e ies so <a > b></ c asn n s c a n b e xt
Re fca

so luti i p la tfo rm s s up p o rt d a ta t ier

nsandA
with eve .
war (i.e. , a (i.e. , inef fect b l e m p h a s i s o n t h e s e p r o v i d e r s , s o y. o u o
a > is t o v e rla
(a n d
tern s s pro) By PaulM t echn o lo g ies fi fre q u

REST
o l ository v ia pat at tern icul ar cPay
a n onl y what you consume L p, e t ly
rep ained ant i-p ” the part solu tionhsat ne. <b></a></ te d inn sid e
b> i sn o t l e a re ) n
be expl ext) and “f ix are o d u ce Wb eetbt ear p up nl idcear ts it oa nn dd ewphl aot y imt eins t au cn lt oi lu da f e w t ha t e x ce ed th e prec e de n t se t b y g e a c h o tehse r.
CI can ticu lar c ont es used to -pat te rns t h e
re

HTM L
etim ces s. Anti u t, i n b ut ca n yt oe amr so sa tg op hwo an se ssiemr vi liacre s : p l a n s w i tth VS XHT a l, b u t <a> Tag
lin a par R e la t i o n al Dat ab ase S y st e ms (R DB M S):
Ge t Mo

es som co mpu ting p latfo rm c an o ffer yo ur web s of ML


,b <b></
vape p ro ach wit h the pro n e fi c ia l not
m e nting
allo ted reso urc es, with an in curre d co st W g h il e i tH T M L h
Vis it

d e r e e n M ap Re du ce, web se rvi ce APIs, e tc . Some


as soc
iate b e b t oT ph er y ac e s ,l t s w h jo h a
ne x p a n d b aasddmo n es b e e n a
r to d u
ta p p e a h e ye ftfeenc t sd. pdr ea dc t ir e s wh e th er s u ch re so u rc es wer e c o ns u me d hLa d exed i ra bit ro u n
,t e a n n p latform s s upp ort larg e sca le RDB M S vBe ryp e cfat er dm o rel y, t h a t d fo
ea dn vd e r ss a r i ul yn ibn t em p l e m an r l . E a rl y jo r s o me t
s e i Integratio ob ra nndowt .i d t h , m e m o r y , C P U ) a r e t a l l i e d u fa oc w s e r HTM
b ha
s im e .
np er co ed u c r e d t o in u oefc ard
d e p l o y m e n t s .U w e b tu r L
pa . ont o n a p e r -u n i t b a s i s de
co m

n C u sth is R ( sCt al or tui dn gc of ro


m pmu zt ienrog ) absy i at ’l ls mk an joowron tc ol od au yd e p lat for ms . s A s a u s e r o f A m a z o n ’. s E C 2 c l o u dn c o m p u t i n g p l a t f o r m , y o u a re
v e im s r C L O U D C O M P UeT I N G P L AT F O R M S
re su l e lo p e rs rs a d d e ite d lay o th a n hana yb
s c h ath
n ge pe ad ttet h i s . T hraetiov na r i o u the
s te rm as A N D UN r D E R LY I N G C O N C E P T S co mputi ng p H tia

DZone, Inc.
reso urc es f
ti u od co n s um ed b y we b a p p lic at ion s (e. g. u
uild nasth Amazona sEC2: s i g nIndustry
e dro c eastandard
l ns o p e softwar
r a t i n egTM
andLs y s t e mfil llt h ey p las a m e w a y a s o n a l l h o s t i n g

ISBN-13: 978-1-936502-03-5
s a la c t su pp yvirtualization A m a z o nO’s c l o u d c o m p u t i n g
f h B
p l a t f o r m i s so h era v i l y bpea ss sh
e di no n i in n dteuxs t r y
s Inte g l use o p ts su c tegration orsassoo Th e k of s
s t a n d a r d s o f t w a r e a ned v i r t u a l i z a t i o n

140 Preston Executive Dr.


o rt. n
Co ntinuo u n ve n tio n a d e co n ce In
t e c h n o l o g y. P Stru c o u ld n o t
o lu late st we ta nd a r t be c b sta n

ISBN-10: 1-936502-03-8
e c nc
W h ile th i V i r t u a l i z a t i o n a l l o w s a p h ry s i c a l p i e c e o f dar
S
t i n o fC Ito

h ard ware to be u tilized by mu ltiple
o n t ho e n o o
expands o p e r a t i n g s y s t e m s . T h i as a l l o w s

Suite 100
res ou rces g

50795
u
( e . g . b a n d w i d t h , m e m oe r y , C P U ) t o b e
a l l o c a t e d e x c l u s i v e l y t o ri n d i v i d u a l
c
o p e rat in g s y ste m in s tan c es .
T

Cary, NC 27513
o
s
o
t
l
u
s
r

888.678.0399
K

DZone communities deliver over 6 million pages each month to


e

y
i
l

919.678.0300
e
S
m

more than 3.3 million software developers, architects and decision


t
r
u
e
V

Refcardz Feedback Welcome


c
n
$7.95

makers. DZone offers something for everyone, including news,


t
u
s
r

refcardz@dzone.com
a
l
E

9 781936 502035
l

tutorials, cheat sheets, blogs, feature articles, source code and


e
m
e
n

Sponsorship Opportunities
!
t
s
a

more. “DZone is a developer’s dream,” says PC Magazine.


n
z

sales@dzone.com
d
m
o
nges
d

Version 1.0
r
e

Copyright © 2010 DZone, Inc. r


All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic,
isolatecha
.
.
.

mechanical, c
a
f
Description
ftwarein
aPrivateW
orkspaceto

sitory
ntrolrepo manage HTML B
Developso version-co ingandto ASICS
e
photocopying, or otherwise, without prior written permission of the publisher.
g
allfilestoa nimiz emer
R
H
W
orkspace
Commit
etomi

ltiple

You might also like