Professional Documents
Culture Documents
Selenium Load Testing & Performance Monitoring Solution 1. 2
Selenium Load Testing & Performance Monitoring Solution 1. 2
Selenium Load Testing & Performance Monitoring Solution 1. 2
DZon
brought to you by...
#125
ARCHITECTURE
Selenium 2 . 0
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);
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.
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 ” ) ) ;
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
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.
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
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 ) ;
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())) {
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!”)
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
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 ( ) ;
}
}
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 ;
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 ( ) ) ;
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
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
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
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
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
y
i
l
919.678.0300
e
S
m
refcardz@dzone.com
a
l
E
9 781936 502035
l
Sponsorship Opportunities
!
t
s
a
sales@dzone.com
d
m
o
nges
d
Version 1.0
r
e
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