Date post: | 03-Jun-2018 |
Category: |
Documents |
Upload: | quontrasolutions |
View: | 224 times |
Download: | 0 times |
of 82
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
1/82
1
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
2/82
Test automation is the use of softwareTo set test preconditions.
To control the execution of tests.
To compare the actual outcomes to predictedoutcomes.
To report the Execution Status.
Commonly, test automation involvesautomating a manual process already in placethat uses a formalized testing process.
2
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
3/82
Frequent regression testing Repeated test case Execution is required
User Acceptance Tests
Faster Feedback to the developers Reduce the Human Effort
Test same application on multipleenvironments
3
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
4/82
Quick Test Professional By HP
Rational Functional Tester By Rational (IBM Company)
Silk Test By Borland
Test Complete By Automated QA
QA Run (Compuware )Watir ( Open Source)
Selenium ( Open Source)Sahi (Open Source)
4
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
5/82
Selenium is a robust set of tools thatsupports rapid development of testautomation for web-based applications.
Selenium provides a rich set of testing
functions specifically geared to the needs oftesting of a web application.
Selenium operations are highly flexible,allowing many options for locating UI
elements and comparing expected testresults against actual application behavior.
5
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
6/82
Supports Cross Browser Testing. TheSelenium tests can be run on multiplebrowsers.
Allows scripting in several languages like
Java, C#, PHP and Python. Assertion statements provide an efficient way
of comparing expected and actual results. Inbuilt reporting mechanism.
6
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
7/82
Selenium IDE
Selenium Remote Control
Selenium Grid
7
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
8/82
Selenium IDE is an integrated developmentenvironment for Selenium tests.
It is implemented as a Firefox extension,and allows you to record, edit, and replay
the test in firefox Selenium IDE allows you to save tests asHTML, Java, Ruby scripts, or any otherformat
It allows you to automatically add assertions
to all the pages. Allows you to add selenese commands as
and when required
8
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
9/82
Using Firefox, first, download the IDE from theSeleniumHQ downloads page When downloadingfrom Firefox, youllbe presented with the WindowA. (See Next Slide)
Select Install Now. The Firefox Add-ons windowpops up, first showing a progress bar, and whenthe download is complete, displays the Window B.(See Next Slide)
Restart Firefox. After Firefox reboots you will find
the Selenium-IDE listed under the Firefox Toolsmenu.
9
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
10/8210
Window A Window B
To run the Selenium-IDE, simply select it from the Firefox Tools menu. It opens
as follows with an empty script-editing window and a menu for loading, or
creating new test cases. (See Next Slide)
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
11/8211
Start and Stop
Recording
SeleneseScript
Editor
Selenium Log
Replay
Toolbar
Accessor
Area
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
12/8212
Selenium Options allow you to
Set The Text Encoding Format
Set the Default Page Time Out
Set The Base URL recordingoption
Adding Selenium Core and IDE
Extension
Add Automatic Page Title
Assertions
There are also other options
under development
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
13/82
Open Firefox that has the IDE installed Open the base URL of the application to record. Keep the application in a common base state. Go To Tools Selenium IDE and the IDE will be
opened Now perform the operations on the applicationas you are testing the application.
Once you are done with the recording click onthe stop recording button and save the test
case through the file menu. By default it will besaved as a selenese script (HTML format)
13
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
14/82
clicking a link - clickor clickAndWaitcommands
entering values - typecommand
selecting options from a drop-down listbox -selectcommand
clicking checkboxes or radio buttons - clickcommand
14
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
15/82
Make sure the application is in the commonbase state.
Click on the run button. Here you can alsocontrol the speed of the execution using the
toolbar
Once the test is run you can view the test login the bottom of the IDE window
15
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
16/82
In the Selenium IDE youcan create any numberof test cases and savethem as test suite.
To Run the test Suiteclick on the Play entiretest suite button asshown below.
16
Test Suite with
Test1 & test2
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
17/82
Test Runner allows you to run the test case in abrowser loaded with the Selenium-Core
TestRunner.
Test runner is invoked by clicking the below Shownbutton in the IDE
17
On Clicking the Test Runner Button you will the
window as seen in the next slide
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
18/82
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
19/82
Run a Test CaseClick the Run button to run the currently displayed testcase.
Run a Test SuiteClick the Run All button to run all the test cases in thecurrently loaded test suite.
Stop and StartThe Pause button can be used to stop the test case while itis running. The icon of this button then changes to indicatethe Resume button. To continue click Resume.
Stop in the Middle
You can set a breakpoint in the test case to cause it to stopon a particular command. This is useful for debugging yourtest case. To set a breakpoint, select a command, right-click, and from the context menu select Toggle Breakpoint.
19
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
20/82
Start from the Middle
You can tell the IDE to begin running from aspecific command in the middle of the test case.This also is used for debugging. To set a startpoint, select a command, right-click, and from
the context menu select Set/Clear Start Point.Run Any Single Command
Double-click any single command to run it byitself. This is useful when writing a singlecommand. It lets you immediately test a
command you are constructing, when you are notsure if it is correct. You can double-click it to seeif it runs correctly. This is also available from thecontext menu.
20
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
21/82
Selenese allows multiple ways of checking forUI elements.
Verifications and assertions are used to checkif
an element is present somewhere on the page?
specific text is somewhere on the page?
specific text is at a specific location on the page?
Verifications and assertions are not one and
the same. If an assertion fails, the script will be aborted
but if a verification fails the script will continue.
21
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
22/82
22
verifyTextPresentThis command is used to
check if a particular text is
present in a page or not.
To add this command , While
recording the test steps rightclick on the text item that you
want verify. Once right clicked
you can find an option Show
all commands. On Clicking it
you will find an option
verifyTextPresent, select it
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
23/82
verifyTitle This command is used to
check if the page title iscorrect or not.
To add this command ,
While recording the teststeps right click anywhere on the page thatyou want verify. Onceright clicked you can findan option Show allcommands. On Clicking
it you will find an optionverifyTitle,select it
23
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
24/82
verifyElementPresent
This command is used toverify if a page element ispresent in the page or not.
To add this command ,While recording the test
steps right click anyelement on the page thatyou want verify. Once rightclicked you can find anoption Show allcommands.OnClicking it
you will find an optionverifyElementPresent,select it
24
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
25/82
verifyValue This method is used to
check if edit box hasparticular value or if thecheck box is on. Basicallythis method returns thevalue of present in the
object. To add this command ,
While recording the teststeps right click anyelement on the page thatyou want verify the value of.Once right clicked you canfind an option Show allcommands. On Clicking ityou will find an optionverifyValue,select it.
25
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
26/82
Assertions are same as Verifications. Theonly difference is, if the assertions fail thescript will abort. But the script willcontinue run in case a verification point
fails. The steps for inserting the assertions is
same as that of verification point.
While recording Right ClickShow all
commands select an assertion.
26
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
27/82
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
28/82
waitForPageToLoadThis command will make the script to wait till the page
loads.
Syntax is waitForPageToLoad(timeout); Time out is the
maximum time the script will wait for the page to load.
28
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
29/82
waitForAlertThis command will wait for the alert message toappear
waitForTableThis command will wait for the Web table tocompletely load in the page
waitForTitleThis command will for the page Title to appear onthe browser.
Other waitFor commandsSelenium has several other wait command like
waitForText, waitForPopup and so on. Thesecommands are generically called Synchronizationcommands
29
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
30/82
Store command are used to fetch the valuesfrom the application and store it in a variable.These variables can be used latter forvalidation purpose.
The Store command can be used to retrievethe page title, text from the page and otherattributes from the application.
30
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
31/82
Echo command isused to print thevalue in to theselenium IDS log.
When printing avariable use ${var}
There are somelimitations for this
methods this has tobe used withcaution
31
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
32/82
Can run the test only on FirefoxNo Programming login (like loops, conditional
statements) can be applied
Selenium IDE can execute scripts created inSelenese only.
It is difficult to use Selenium IDE for checkingcomplex test cases involving dynamic
contents
32
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
33/82
A solution to cross browser testing.A server, written in Java and so available on
all the platforms.Acts as a proxy for web requests from them.
Client libraries for many popular languages. Bundles Selenium Core and automatically
loads into the browser
33
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
34/82
Software RequiredJDK , selenium-remote-control (this can be
downloaded fromhttp://seleniumhq.org/download/)
Installation Procedure Selenium RC is simply a jar file and to run it we
need java installed. Once the Java is installed justunzip the selenium-remote-control-zip which was
downloaded from the selenium site to a directory.
34
http://seleniumhq.org/download/http://seleniumhq.org/download/8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
35/82
First Generate the Script using selenium IDE in thefirefox IDE
Once the Scripts are recorded add assertions whereever required
Now format the Selenese test into the language ofyour choice. Please refer to the Image
35
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
36/82
Once the Selenese script is converted intoyour preferred language you can you canrun them using Selenium Server.
For running the script you also need the
client driver for that particular language. To enhance the script we will require IDE
like netbeans or Eclipse IDE To Integrate the script and run them as a
suite we will require build integration toolslike Maven or Ant.
36
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
37/82
37
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
38/82
Following are the steps to create a selenese testsuite and run a test suite using selenium RC
1) Record a test case using selenium IDE and save it asTest1.html (say).
2) Record another test case using selenium IDE and save its as
Test3.html (say).3) In this way you can record any number of test case using
selenium IDE and save them as .html
4) Then Create a New Test Suite using the File Menu. See theImage Below.
38
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
39/82
39
Add the created test cases in to the suite asshown in Image1
Once the test cases are added to the suite save
them as a test suite as in Image2
The Open the command prompt and navigate to
the directory where Selenium RC (server) isplaced.
In the command prompt Enter the command as
shown below.
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
40/82
Command : java -jar selenium-server.jar
-multiwindow -htmlSuite"*iexplore"
"https://localhost/bookstore/""D:\testa.html""C:\results.html
Once the command is run
the results will be stored inthe results.html file asshown below.
40
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
41/82
Required Software Selenium RC Server jar , Selenium Java Client
Driver jar , JDK +, Eclipse (or any other IDE),Junit jar and testing jar
For building the frame work we requirecontinuous build integration tools like Ant,Maven or cruise control.
41
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
42/82
Enables communication with Selenium RemoteControl Server.
Functionality of Selenium is exposed via thesedrivers.
Available in Ruby, Python, Java, C-Sharp and so
on. Give access to the rich features and libraries of the
language, including their unit testing framework. Makes it possible to develop automated scripts in
the same language as product. Which one you should use?
Whatever make sense in your context. Most Popular is java andfor the training we will use java
42
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
43/82
Following are the steps for generating thetest scripts in java1) Record the Script in Selenium IDE and
format them into Java
2) Create a Selenium Java project in Eclipse andload all the necessary jar files
3) Copy the formatted Java code into theEclipse and run the test.
43
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
44/82
Steps of formatting the code intojava
1) Open the Browser and openthe base URL of theapplication.
2) Open the Selenium IDE andstart recoding the test steps.
3) Once the recording is done goto Menu Options FormatJava(TestNG) Selenium RC
4) You can see the java code inthe source tab of IDE. Save thetest case with .java extension.
44
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
45/82
Eclipse is an open source community whoseprojects are focused on building an extensibledevelopment platform for building Javaapplications and frameworks. Eclipse is one of thebest Java IDE and as a matter of face Eclipse ismuch more than a Java IDE.
We can configure a selenium project in eclipse andeven run the scripts from eclipse.
Using eclipse its easy to enhance the recordedscript. We can add power to the recorded script byparameterizing the test inputs and even validate
the back values. Eclipse also allows us to write reusable code for
efficient test automation.
45
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
46/82
Download the Eclipse IDE for Java Developersfrom the http://www.eclipse.org/downloads/ page.
Unzip the downloaded zip file from the above siteinto a directory.
Once the unzipping is over open the folder anddouble click on the icon and it will open a dialogbox as shown below.
46
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
47/82
Create a folder say (selenium) in any one ofthe directory and change the workspacelocation to the directory created by you.Then click ok button.
On doing so you will see the eclipsewelcome screen if you are doing it for thefirst time. On the welcome screen click onthe workbench icon to open the projectexplorer.
When you to try to open the eclipse fromnext time it will directly show the projectexplorer as shown in the next slide.
47
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
48/82
48
PackageExplorer Scrip View
console
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
49/82
File New JavaProject
49
Specify the project nameand click finish
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
50/82
Open the folder contain the selenium project thatyou have just created. Inside that folder createanother folder with name lib
Inside the lib folder place the following jar files.junit-4.8.1.jar
selenium-java-client-driver.jarselenium-server.jar
testng-5.12.jars
After placing the jar file in lib come back to theeclipse and click on the project explorer strip and
press F5. You should see the all the jar files underthe lib folder in the project explorer as shown inthe Image in next slide.
50
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
51/82
51
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
52/82
In the eclipse Package Explorer right click on the src(source) folder. In thatselect New Packageand give a name to the
package. See thePicture1 Now right click on the
created package andclick on new classand provide a class
name. You will see ajava script template asseen in the next slide
52
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
53/82
Your IDE should look like this
53
This is where you have copy the
Java code that you formatted in the
Selenium IDE
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
54/82
Right Click on theProject
Select Build Path Configure Build path
On the next windowclick on the librariestab and click on theadd jars tab
54
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
55/82
As you click on theadd jars button awindow showing allthe jar files will be
opened. Select allthe jar files and clickon ok button
By doing this step
we are almost readyto write the scriptand run it.
55
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
56/82
Copy the code that was formatted in theselenium IDE and paste it in the Eclipse IDEscript template.
Make sure the class name that you created
and the class name in the script are same Make sure that you have add the methods
setUp and tearDown
56
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
57/82
Start the Selenium Server For this open the command prompt and go to the
folder where the selenium server is present. Run the command java -jar selenium-server.jar The Selenium server will start running and you can
see the message in the command prompt.
57
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
58/82
Once the server is up go to the eclipse and rightclick on the script.
In that select Run As JUnit Test. See the imagebelow. After this you will get will see that the scripthas run successfully in the window thats shown in
the next slide.
58
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
59/82
59
success
failure
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
60/82
setUp methodThis method prepares the selenium server to runthe test. This method basically hooks to theselenium server and opens up the base URL of theapplication
test*** methodThe methods with the name test will actually runthe tests on the application. There can be anynumber of test methods in the app
tearDown method
This method will run after the end of the test. Thistest will disconnect with the server and makesroom for the next tests to run the tests.
60
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
61/82
61
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
62/82
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
63/82
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
64/82
== Equal (careful)!= Not equal
>= Greater than or equal
Greater than
< Less than
64
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
65/82
If Condition
if (name != selenium)
{
System.out.print(Tool Changed);
}
Else
{System.out.print(Tool is ok);
}
65
Loop n timesfor ( i = 0; i < n; n++ )
{// this code body will execute n times
// I from 0 to n-1
}
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
66/82
66
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
67/82
Required Packages importjava.util.Date;
importjava.text.SimpleDateFormat;
Sample Code
67
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
68/82
Implementation Steps1) First get the values from the list box2) To get the values from the list box or any
other HTML element we need to know its
Xpath. (use Xpath checker)3) The values taken from the list box are
stored in a variable.4) Use the indexOf method to find if HTML is
present in the extracted variable.
68
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
69/82
69
Xpath Locator
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
70/82
Parameterization is to same run the scriptwith multiple set of data. The data will be stored in a CSV file. We will have write a program to read the
values from the excel line by line andreplace the values.
For reading the values from a CSV file wewill require the following packages import java.io.File;
import java.io.BufferedReader; import java.io.FileReader; import java.util.StringTokenizer;
70
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
71/82
71
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
72/82
Ant is a free tool from the Apache JakartaGroup Originally written by James Duncan
Davidson
on an airplane from Europe to the US... Like make for Java only better Uses an XML file to drive its action Extremely powerful and modular Easily extensible Written in Java so it is cross platform
72
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
73/82
Download ANT and copy it into any of thedirectory
Create a JAVA_HOME environment Variable
Create a ANT_HOME environment variables
Add the ANT bin path to windows pathenvironment variable
Create a build file to load all the jar files
and create targets to run the selenium testsusing the build file
73
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
74/82
74
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
75/82
Start the selenium server through thecommand prompt Then go the folder where the selenium tests
are stored
Make sure that the build.xml file is presentin that location
Now run the command ant from thecommand prompt
You can see the test running and the resultswill be saved in the results.txt file.
75
Selenium-Grid
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
76/82
76
Selenium Grid
Selenium-Grid allows the Selenium-RC solution to scale for test
suites or test suites to be run in multiple environments.
With Selenium-Grid multiple instances of Selenium-RC are
running on various operating system and browser configurations,
each of these when launching register with a hub. When tests are
sent to the hub they are then redirected to an available Selenium-
RC, which will launch the browser and run the test.
This allows for running tests in parallel, with the entire test suite
theoretically taking only as long to run as the longest individual test.
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
77/82
Download Selenium Grid latest binarydistribution and unpack it on yourcomputer.
Go at the root of selenium grid directoryand validate your installation:
cd selenium-grid-1.0 ant sanity check
Go to the selenium distribution directoryand launch in a new terminal:
ant launch-hub
Check out that the Hub is running by
looking at its console in a browser:http://localhost:4444/console(See the Browser in thenextslide)
77
http://localhost:4444/consolehttp://localhost:4444/console8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
78/82
78
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
79/82
In a new terminal enter the followingcommand
ant launch-remote-control
Based on your target file you can run either in
sequence or in parallel
79
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
80/82
Relatively easy to automate web UI tests Record/Replay for regression tests
RC allows integration with CI andJUnit/FitNesse tests
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
81/82
Speed: RC->Browser communication is aspeed bottleneck (run in grid, overnight)
UI is brittle, tests depending on the UI break alot (DSTL might fix this, page abstractions as
well)
Data-backed tests are not easily repeatable(integrate with DB test engines)
8/12/2019 Test Automation Using Selenium Presented by Quontra Solutions
82/82
Use Selenium to verify workflow and session Don't put Selenium tests in your main
development build run them overnight
Have dedicated machines that run tests