Selenium Simplified Junit Preview

Embed Size (px)

DESCRIPTION

A free preview of the forthcoming "Selenium Simplified" book. A tutorial guide to Automated Testing in Selenium using Java and JUnit. Get more information at http:\\www.compendiumdev.co.uk\selenium

Citation preview

Selenium SimplifiedA tutorial guide to using the Selenium API in Java with JUnit Alan Richardson

Published by Compendium Developments

Copyright

!"#" by Alan Richardson

$he right of Alan Richardson to be identified as the author of this wor% has been asserted by him in accordance with the Copyright& Design and Patents Act #'(() $he views e*pressed in this boo% are those of the author) +irst published in ,reat -ritain in !"#" by. Compendium Developments http.//www)compendiumdev)co)u%/selenium contact details. alan0compendiumdev)co)u% Author1s Software $esting -log. www.eviltester.com Compendium Developments. www.compendiumdev.co.uk 2very effort has been made to ensure that the information contained in this boo% is accurate at the time of going to press& and the publishers and author cannot accept any responsibility for any errors or omissions& however caused) 3o responsibility for loss or damage occasioned by any person acting& or refraining from action& as a result of the material in this publication can be accepted by the editor& the publisher or the author) Apart from any fair dealing for the purposes of research or private study& or criticism or review& as permitted under the Copyright& Design and Patents Act #'((4 this publication may only be reproduced& stored or transmitted& in any form or by any means& with the prior permission of the publishers& or in the case of reprographic reproduction in accordance with the terms and licenses issued by the Copyright 5icensing Agency& '" $ottenham Court Road& 5ondon& 6#$ 75P) 2n8uiries concerning reproduction outside these terms should be sent to the publishers) 9ersion 0 #'/"#/!"## !nd Printing

e:boo% IS-3 . ';(:":'>!:":; paperbac% boo% IS-3 . ';(:":'>!:#:7

Table of ContentsIntroduction..........................................................................................................................................9 Getting the Source-code...................................................................................................................9 Windows, Linux or Mac................................................................................................................10 Acknowledg ents.........................................................................................................................10 !ha"ter 1#Getting Started With $he $ools..........................................................................................11 What $ools%...................................................................................................................................11 Seleniu I&'............................................................................................................................11 (a)a............................................................................................................................................11 'cli"se.......................................................................................................................................1* Seleniu -+!.............................................................................................................................1* !ha"ter *#Seleniu I&' ,asics.........................................................................................................1Install .ire/ox.................................................................................................................................1Install Seleniu -I&'.....................................................................................................................1!a"ture 0la1 ,ack 2 +ecording a Scri"t with the I&'..................................................................13 !o and, $arget and 4alue..........................................................................................................15 Sa)e $he $est.................................................................................................................................15 .urther reading...............................................................................................................................15 !ha"ter -#Install (a)a.........................................................................................................................19 !ha"ter 3#Install and +un Seleniu -+!............................................................................................*0 Ste" 6ne - &ownload 7 Install Seleniu -+!...............................................................................*0 6)er)iew o/ the !ontents o/ the Seleniu Archi)e.................................................................*1 Ste" $wo - +un Seleniu -+!........................................................................................................** Ste" $hree 2 Sto" the Seleniu Ser)er.........................................................................................*.ro the co and line............................................................................................................*.ro a 8rowser 9+L................................................................................................................*3 +unning an I&' Generated $est in &i//erent ,rowsers................................................................*3 'nd :otes..................................................................................................................................*; !ha"ter ect...................................................................................................-1 !ha"ter ;#!reate a (9nit test 9sing the (9nit ex"ort /ro Seleniu -I&'........................................-Introduction....................................................................................................................................-'x"ort an I&' scri"t as a (9nit test...............................................................................................-Load an existing scri"t into the I&'.........................................................................................-!hange the /or at o/ the scri"t to (a)a.....................................................................................-3 !reate a :ew !lass in 'cli"se..................................................................................................-< !o"1 7 0aste the code /ro Seleniu I&' into 'cli"se.........................................................-= +esol)e I "ort 'rrors...............................................................................................................30 Add (9nit to the 8uild "ath.......................................................................................................3* +e o)e the unused i "ort.......................................................................................................33 +un the (9nit test...........................................................................................................................3< Allow it through the .irewall....................................................................................................3< Seeing the test running..............................................................................................................3; !heck the results.......................................................................................................................35 It went too ?uickl1@........................................................................................................................3= 1

+un the test in de8ug ode.......................................................................................................39 !reate and I "ort so e ore I&' !on)erted tests......................................................................ect !reation...........................................................................................*HseleniumHide)html Adam ,oucher1s posts on Selenium ID2 : http.//adam)goucher)ca/MsNSeleniumOID2 particularly the plugin posts : http.//adam)goucher)ca/MsN$heOSelenium: ID2O#)*OpluginOAPI

Selenium ID2 2*tensions : http.//wi%i)open8a)org/display/SID2/ContributedO2*tensionsOandO+ormats $he +irefo* plugins search may reveal some plugins for Selenium ID2 https.//addons)moIilla)org/en:US/firefo*/search/M8Nselenium A lot of video tutorials for Selenium ID2 get added to Gou $ube : http.//www)youtube)com/resultsMsearchH8ueryNseleniumOide

1=

Chapter $:

Install Java

Gou can chec% if you have Java installed by. visiting http.//Kava)com and using the CDo I have JavaMD chec% http.//Kava)com/en/download/installed)Ksp running the command CKava :versionD from the command line

Figure ".1 : %unning 23a4a !4ersion5 from the command lineIf you don1t have Java installed then visit http.//Kava)com and follow the Download lin%s to install it on your machine)

19

Chapter %:

Install and !un Selenium !C

-efore we can actually write any tests& we install Selenium:RC) PInstallP is really the wrong word when all we will do is download and e*tract the Selenium:RC archive to a directory)

Ste$ ,ne - !ownload - Install Selenium-)#Download Selenium RC from the Selenium download page) Ehttp.//seleniumh8)org/download/F

Figure .1 : Download Selenium %0 from Selenium67I saved it to c.Lselenium

Figure .2 : 8narchi4e the sa4ed distri.ution into a director,$hen I unarchived this into a folder of the same name as the the archive using an unarchiver EI use IQArc http.//www)iIarc)org/F)

Figure ." : Extracting to a folder with same name as archi4e *0

So now I have a folder with the Selenium files. C.LseleniumLselenium:remote:control:#)")>

&vervie' of the Contents of the Selenium (rchive

Figure . : 0ontents of Selenium %0$here are two main folder types here. specific language client drivers selenium server

2ach of the supported languages has a client driver) Gou can see from the list of folders that the Selenium distribution supports. Aicrosoft )3et Java Perl P@P Python Ruby

$he client drivers are what we use in our tests to communicate with Selenium:RC) 6e use the client drivers because our tests act as clients& which communicate with the server& in order to e*ecute commands through the browser) $he client drivers contain the source code& tests& documentation and libraries that we need to wor% with the Selenium tests) $he selenium server directory has the documentation& source code& tests and e*ecutables for wor%ing with the Selenium server) 6e will e*amine the contents of the archive in more detail in future sections but this high level overview should give you enough of a basic understanding to let us continue with the tutorial)

*1

Ste$ Two - )un Selenium-)#I have the P?pen Command 6indow @ereP Aicrosoft Powertoy for RP installed) I recommend you install this very helpful tool& if you use RP) In 9ista this functionality is built in and you can browse to the folder in 6indows 2*plorer& hold down the Shift %ey& right:clic% on the folder and choose C?pen Command 6indow @ereD) http.//download)microsoft)com/download/whistler/Install/!/6RP/23: US/Cmd@erePowertoySetup)e*e http.//www)microsoft)com/windows*p/Downloads/powertoys/Rppowertoys)msp*

Note: If you don't have open command window here installed then you need to get a command window open and type: cd c:\selenium\selenium-remote-control- .!."\selenium-server- .!." #int: $ry pressing 'ta%' as you do this to automatically complete folder details. So I right clic% on the PSelenium:server:#)")>P folder and choose P?pen Command 6indow @ereP from the conte*t menu) Note: I also use the open-source &onsole 'http:((sourceforge.net(pro)ects(console(* to help me as I can have multiple console windows open in a single application. I give the command window a new title so I can find it easily by issuing the command P$itle S2523IUA S2R92RP)

Figure .& : )n eas, to identif, Selenium Ser4er command window$his ma%es it much easier to find when I Alt:$ab or loo% for it on the tas%bar) 3ow we run the selenium server by typing the command PKava :Kar selenium:server)KarP

**

Figure .# : Start up messages from the Selenium Ser4erIf you didn1t see a screen li%e the above then you may need to install Kava) Done : you have Kust installed and started the Selenium:RC server)

Ste$ Three ( Sto$ the Selenium Ser+er )rom the command lineSince you have started the Selenium server in a D?S window you can simply use CCtrlOcD to stop it)

Figure .( : )fter 0trl9c the ser4er shuts down *-

)rom a bro'ser *!+Since seleniu ser)er o"erates a we8 ser)er under (ett1 it can recei)e co 8rowsers. Issue the /ollowing to shutdown the ser)er# ands )ia htt" through

htt"#LLlocalhost#3333Lseleniu -ser)erLdri)erL%c dMshut&ownSeleniu Ser)er

If you wanted to leave the server running and Kust close down the current Selenium session& you could issue the following command in a browser) http.//localhost.7777/selenium:server/driver/McmdNshutDown

)unning an I!" Generated Test in !i erent 'rowsersAlthough the ID2 script we recorded in Chapter ! was recorded in +irefo*& and only +irefo* has the ID2& we can run ID2 recorded scripts in other browsers) $his re8uires the use of Suites& and Selenium:RC) Note: I do not recommend this approach to using +elenium. I only include it for completeness. I have had issues getting this to work with Internet ,-plorer on +elenium .!." using .ie-plore/ .iehta/ or .piie-plore. 0ther %rowser codes seem to work/ %ut when using I,1 the +elenium server seems to throw an #$23 suite e-ception. If this happens to you/ and you consider it important/ then you might need to drop down to an earlier version of +elenium-4&. +tick with the %ook though and it shouldn't %e an issue as you will never use this method to run the tests. http:((groups.google.com(group(seleniumusers(%rowse5thread(thread(167!ee686 91!:d!("f"8cadf:7 e79f1 Gou create a suite by creating an @$A5 file with lin%s to the tests you want to create) e)g suite)html %y Test &uite

or the cell padding cellpadding="1" cellspacing="1" border="1" ) &uite o* &elenese Tests

-ut since will typically create a test by copy and pasting& ma%ing it well typed and formatted may stand you in good stead for future versions of Selenium)F Since you have Selenium:RC installed you can use the following command line synta* to run a suite of Selenese scripts in other browsers)/ava -/ar selenium-server./ar -html&uite ""6! selenium.clic25"'tn,"6! selenium.wait#or3ageTo