You are on page 1of 9

Selenium Tutorial : Selenium Remote

Control
7. Selenium Remote Control (RC)
7.1. Overview
While Selenium IDE may seem a productive and efficient tool for writing test-cases, it
lacks many essential features of a testing tool:
o Conditional statements
o Loops
o Logging
o Exception handling
o Reporting
o Test fxtures and data-driven tests
o Test dependencies
o Taking screenshots
Selenium RC is the answer to a more powerful test-suite for your applications
!t follows a client"server model allowing client li#raries to execute tests on a
#rowser controlled #y the server
Figure 7. Overview of Selenium RC
7.2. Selenium Server
Selenium server is the program that drives the #rowser
!t em#eds Selenium Core framework and in$ects it into the #rowser
!t communicates with the running test client and drives the #rowser
Client tests sends commands that the server interpretes in order to drive the
#rowser
The server sends #ack results to the client
Client and server communicates via %TT& 'ETs and &(STs so you can easily
plug into
Server is confgura#le at startup via command-line options use java -jar
selenium-server.jar -h to see the list of options
7.3. Client libraries
Client li#raries provides the )&! to program tests and execute them on the
server
Each implementation provides access to all Selenium commands
Supported AI implementation e!ists in:
o *ava +also accessi#le via 'roovy,
o -et
o &%&
o &ython
o &erl
o Ru#y
7.. !ab 3 : "nstallation of Selenium RC
7.4.1. Prerequisites
) *./ installed on the system
) Ru#y distri#ution with the rspec and selenium-client gems installed if
you0re working
The Selenium RC archive
7.4.2. Download The Tested Application
In the folder of your choice, e!ecute the following command:
git clone http://github.com/wolframarnold/selenium-test-app.git
"o to the selenium-test-app folder and run the following:
o cp
./vendor/plugins/active_scaffold/test/mock_app/config/database.yml
./config/
o rake db:migrate
#ow you should $e a$le to lauch the application with the script server availa$le in the
scripts folder:
./scripts/server
The application should #e availa#le at http://localhost:3000
7.4.3. Running The RC Serer
1n2ip the Selenium RC archive somewhere on your system
'o into the selenium-rc3 then the selenium-server folder
(pen a terminal in this folder and enter the command java -jar selenium-
server.jar
!f there0s no error messages3 your Selenium RC server should #e running now
7.4.4. Running A Test !"aa#
Create a new *ava pro$ect in Eclipse or your *ava !4E
)dd the selenium-java-client-driver.jar $ar to the pro$ect0s classpath
)dd a new *1nit test-case to the pro$ect 5hen asked3 select Junit 4
6ou should now have an empty $ava fle
(pen the la#7 test-case on Selenium !4E
(n Selenium !4E3 open the Options menu then Format and click Java (Junit)
Copy"paste the contents in the *1nit fle on Eclipse
Change the package name at your will
%ou may have to fi! the imports
o Click on the following icon and choose the most appropriate option to
correct the fle
o 6ou may also press Ctrl + Shift + O to let Eclipse do it automatically
)t this point you shouldn0t have any more errors and you are ready to run the
test
Selenium RC will open a couple of 8irefox windows and run the test
7.4.$. Running A Test !Ru%&#
(pen the la#7 test-case on Selenium !4E and export it as u!" (rspec)
A little workaround is needed, open the e!ported file:
o (n line 9:3 where you should have; /!re you sure you want to
delete "suggestion_name#$%s%&'(/ )* page.get_confirmation.should
be_true
o Change it to; page.get_confirmation.should match+/!re you sure you
want to delete "suggestion_name#$%s%&'(/,
Run the test with spec "filename#.rb
Selenium RC should launch a couple of new #rowser windows and run the test
7.#. $unit
*unit is the reference unit-testing framework for *ava
The framework allows you to test your applications using specifc classes
containing the logic performing actions on the tested classes and checking
the results
Eclipse provides support for *unit3 so we0ll #e using that
*1nit < uses named methods to detect set-up and test methods
&'nit ( cheat sheet:
import static org.junit.!ssert.assert-.uals/

public class !ddition0est 1 //

2eep0hought dt/

public void set3p+, 1 //


dt ) new 2eep0hought+,/
dt.initiali4e+,/
5

public void tear2own+, 1 //


dt.close6!&ession+,/
save7esult+,/
5

public void test2eep0hought+, 1 //


int answer ) dt.ask+89hat is the meaning of life: the universe and
everything;8,/
assert-.uals+<=: answer,/ //
5

5
The tests are em#edded within a classic *ava=ean
The set3p+, method will #e executed once3 #efore any other
method
tear2own+, method is executed after the last test method
-ame your tests methods following the test&omething+, pattern
They will #e executed in the order of declaration
*unit uses assert... methods to provide verifcation of values
'ote
In our tests, we)ll use verify... methods provided
$y the selenium driver instead
7.%. RS&e'
RSpec is a =ehaviour 4riven 4evelopment framework for Ru#y
!t provides a framework that tests your apps according to defned #ehaviors
!n =ehavior 4riven 4evelopment +=44,3 you frst defne the #ehaviors of your
app according to the client0s need3 then your write the tests that validate
these #ehaviors and then you start to develop your application
!t is )gile-oriented
=ehaviors are defned in a spec fle containing set-up methods as well as
methods providing tests for a given #ehavior
6ou run tests with the spec command in a terminal
*Spec cheat sheet:
re.uire >thing>

describe 0hing do

before do
?thing ) 0hing.new
end

it 8should find the !nswer to the 3ltimate 6uestion of @ife: the


3niverse: and -verything8 do
?thing.answer.should )) <=
end

it 8should enforce the !nswer to the 3ltimate 6uestion of @ife: the


3niverse: and -verything8 do
?thing.answer ) =<
?thing.enforce
?thing.answer.should be <=
end

end
Load the tested fle +you don0t need to specify the r# extension,
describe AyBlass do
descri$e declares what is $eing tested
6ou can specify code to #e run #efore +before, and after +after, 6ou can
execute code #efore each example with before+:all,
Each example #egins with it followed #y a string descri#ing the #ehavior
to #e tested
object.should and object.should_not are used to compare actual and e!pected
values
e!amples:
o o.should )) "value# and o.should be "value# are e>uivalent
o o.should be_close "value#: "tolerance# tests if a value is
within a certain range
o o.should match "regeCp# tests if the value matches a regular
expression
o o.should raise_error tests if a method raises an error +you
can specify the type of error as arguments,
o o.should have +D,.things tests if a collection contains ?
items exactly have_at_least and have_at_most are also availa#le
7.7. $avaS'ri&t
Since Selenium is #ased on *avaScript3 you can use it in your tests
!t is an excellent way to extract information from the pages for later reuse
+here are , methods availa$le:
o get-val+script,: it takes the script as a string, e!ecutes it and return the value
to which the script evaluates too- In ru$y use get_eval or js_eval
'sing this method, the window o$.ect refers to the Selenium conte!t,
which is different from the tested application)s &avaScript conte!t
+o work around this, you need to get the application)s window o$.ect using
currentwindow ) this.browserbot.getBurrent9indow+, / If you
want to get an element, .ust use element )
this.browserbot.find-lement+8locator8,
o run&cript+script,; it actually adds a "script# tag in the current page
of the tested application3 making it easier to de#ug
7.(. Fi)tures
1sually3 fxtures designate the environment setup for the test-cases
!t is a very a#stract notion and in practice it could #e very di@erent from a
pro$ect to another depending on the pro$ect0s features
!n practice3 fxtures are a set of tools allowing the test of an application under
a pre-determined environment that produces previsi#le results
The main motivation is to avoid errors due to changing environments and the
side-e@ects that may occur
!n our case3 Selenium doesn0t provide any support for fxtures so you have to
do it #y hand using your test framework0s fxtures capa#ilities
RSpec and *1nit have support for fxtures through the set-up methods
7.*. !ab : "m&roving a test
7.(.1. Current Situation
Test is #rittle ; change in the structure of the ta#le displaying suggestions
#reaks the test
Locators are complex
5e can0t do 8ixtures
7.(.2. )ow are we going to change that*
6ou will frst creates a suggestion
Then you will fnd a way to delete that suggestion using only the id of the
delete link
Tip
0$serve the id of each ta$le row and the corresponding delete link- %ou should $e a$le to
e!tract the numeric part of the id and use it to target the delete $utton- 'se the following
snippet to get the id:
&ava:
&tring script ) 8var suggestion_ids ) new !rray+,/8 E
8page )
selenium.browserbot.getBurrent9indow+,.document/8 E
8table ) page.get-lementFyGd+>as_suggestions-
content>,.childHodes$I'/8 E
8var id ) table.rows$='.id/8 E
8if+id JJ id K) undefined, 18 E
8 suggestion_ids.push+id.match+/$0-L'E/g,,/8 E
858 E
8suggestion_ids/8/

selenium.get-val+script,/
*u$y:
script ) 8var suggestion_ids ) new !rray+,/8 E
8page ) selenium.browserbot.getBurrent9indow+,.document/8 E
8table ) page.get-lementFyGd+>as_suggestions-
content>,.childHodes$I'/8 E
8var id ) table.rows$='.id/8 E
8if +id JJ id K) undefined, 18 E
8suggestion_ids.push+id.match+/$0-L'E/g,,/8 E
858 E
8suggestion_ids/8
suggestion_id ) page.js_eval+script,
=onus; /ake a loop that creates more suggestions and fnd a way to iterate
over the ta#le to delete them using $avascript

You might also like