Test Automation Using Selenium
Test Automation Using Selenium
Softsmith Infotech
Test Automation
Test automation is the use of software
To set test preconditions. To control the execution of tests. To compare the actual outcomes to predicted outcomes. To report the Execution Status.
Commonly, test automation involves automating a manual process already in place that uses a formalized testing process.
Softsmith Infotech 2
Softsmith Infotech
Selenium
Selenium is a robust set of tools that supports rapid development of test automation for webbased applications. Selenium provides a rich set of testing functions specifically geared to the needs of testing of a web application. Selenium operations are highly flexible, allowing many options for locating UI elements and comparing expected test results against actual application behavior.
Softsmith Infotech 5
Selenium Features
Supports Cross Browser Testing. The Selenium tests can be run on multiple browsers. 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.
Softsmith Infotech 6
Selenium Components
Selenium IDE Selenium Remote Control Selenium Grid
Softsmith Infotech
Selenium IDE
Selenium IDE is an integrated development environment 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 as HTML, Java, Ruby scripts, or any other format It allows you to automatically add assertions to all the pages. Allows you to add selenese commands as and when required
Softsmith Infotech 8
Softsmith Infotech
Selenium Installation
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)
Softsmith Infotech
10
Selenium IDE - UI
Replay Toolbar Start and Stop Recording
Selenium Log
Softsmith Infotech
11
Selenium Options
Selenium Options allow you to Set The Text Encoding Format Set the Default Page Time Out Set The Base URL recording option Adding Selenium Core and IDE Extension Add Automatic Page Title Assertions There are also other options under development
Softsmith Infotech
12
Softsmith Infotech
15
On Clicking the Test Runner Button you will the window as seen in the next slide
Softsmith Infotech 17
Test Runner
Running Options
Run a Test Case
Click the Run button to run the currently displayed test case.
Running Options
Start from the Middle
You can tell the IDE to begin running from a specific command in the middle of the test case. This also is used for debugging. To set a start point, select a command, right-click, and from the context menu select Set/Clear Start Point.
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.
Softsmith Infotech 21
Verification Commands
verifyTextPresent This 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 right click 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
Softsmith Infotech
22
Verification Commands
verifyTitle This command is used to check if the page title is correct or not. To add this command , While recording the test steps right click any where on the page that you want verify. Once right clicked you can find an option Show all commands. On Clicking it you will find an option verifyTitle, select it
Softsmith Infotech
23
Verification Commands
verifyElementPresent This command is used to verify if a page element is present in the page or not.
To add this command , While recording the test steps right click any element on the page that you want verify. Once right clicked you can find an option Show all commands. On Clicking it you will find an option verifyElementPresent, select it
Softsmith Infotech 24
Verification Commands
verifyValue This method is used to check if edit box has particular value or if the check box is on. Basically this method returns the value of present in the object.
To add this command , While recording the test steps right click any element on the page that you want verify the value of. Once right clicked you can find an option Show all commands. On Clicking it you will find an option verifyValue, select it.
Softsmith Infotech 25
Assertions
Assertions are same as Verifications. The only difference is, if the assertions fail the script will abort. But the script will continue run in case a verification point fails. The steps for inserting the assertions is same as that of verification point. While recording Right Click Show all commands select an assertion.
Softsmith Infotech 26
Assertion Statements
assertTextPresent This will assert if the text is present in the page. assertText This will assert if a particular element is having the particular text.
Softsmith Infotech
27
Softsmith Infotech
28
Store Commands
Store command are used to fetch the values from the application and store it in a variable. These variables can be used latter for validation purpose. The Store command can be used to retrieve the page title, text from the page and other attributes from the application.
Softsmith Infotech
30
Echo Command
Echo command is used to print the value in to the selenium IDS log. When printing a variable use ${var} There are some limitations for this methods this has to be used with caution
Softsmith Infotech 31
Selenium RC
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
Softsmith Infotech 33
Installing Selenium RC
Software Required
JDK 1.6 , selenium-remote-control-1.0.3 (this can be downloaded from http://seleniumhq.org/download/) Installation Procedure Selenium RC is simply a jar file and to run it we need java installed. (JDK 1.6 is preferred) Once the Java is installed just unzip the seleniumremote-control-1.0.3. zip which was downloaded from the selenium site to a directory.
Softsmith Infotech
34
Softsmith Infotech
35
Softsmith Infotech
37
1) 2)
Following are the steps to create a selenese test suite and run a test suite using selenium RC
Record a test case using selenium IDE and save it as Test1.html (say). Record another test case using selenium IDE and save its as Test3.html (say). In this way you can record any number of test case using selenium IDE and save them as <filename>.html Then Create a New Test Suite using the File Menu. See the Image Below.
3)
4)
Softsmith Infotech
38
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) is placed. In the command prompt Enter the command as shown below.
Softsmith Infotech
39
Softsmith Infotech
40
Softsmith Infotech
41
Client Drivers
Enables communication with Selenium Remote Control Server. Functionality of Selenium is exposed via these drivers. 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 and for the training we will use java
Softsmith Infotech
42
Softsmith Infotech
44
Installing Eclipse
Download the Eclipse IDE for Java Developers from the http://www.eclipse.org/downloads/ page. Unzip the downloaded zip file from the above site into a directory. Once the unzipping is over open the folder and double click on the icon and it will open a dialog box as shown below.
Softsmith Infotech
46
Eclipse IDE
Package Explorer
Scrip View
console
Softsmith Infotech
48
Softsmith Infotech
49
After placing the jar file in lib come back to the eclipse and click on the project explorer strip and press F5. You should see the all the jar files under the lib folder in the project explorer as shown in the Image in next slide.
Softsmith Infotech 50
Selenium Libraries
Softsmith Infotech
51
This is where you have copy the Java code that you formatted in the Selenium IDE
Softsmith Infotech
53
Softsmith Infotech
57
Softsmith Infotech
58
success
failure
Softsmith Infotech
59
Script View
Softsmith Infotech
61
Softsmith Infotech
63
Relational Operators
== != >= <= > < Equal (careful) Not equal Greater than or equal Less than or equal Greater than Less than
Softsmith Infotech
64
Programming Elements
If Condition
if (name != selenium) { System.out.print(Tool Changed); } Else { System.out.print(Tool is ok); }
Loop n times
for ( i = 0; i < n; n++ ) { // this code body will execute n times // I from 0 to n-1 }
Softsmith Infotech 65
Softsmith Infotech
66
Date Functions
Required Packages import java.util.Date; import java.text.SimpleDateFormat; Sample Code
Softsmith Infotech
67
Example 1 Get the values from the list box and check if the value HTML is present in it
Implementation Steps 1) First get the values from the list box 2) 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.
Softsmith Infotech 68
Xpath Locator
Softsmith Infotech
69
Parameterization
Parameterization is to same run the script with 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 and replace the values. For reading the values from a CSV file we will require the following packages
import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.util.StringTokenizer;
Softsmith Infotech 70
Parameterization Example
Softsmith Infotech
71
Ant Integration
Ant is a free tool from the Apache Jakarta Group 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
Softsmith Infotech 72
Softsmith Infotech
74
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 SeleniumRC, 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.
Softsmith Infotech
76
How to Grid
Download Selenium Grid latest binary distribution and unpack it on your computer. Go at the root of selenium grid directory and validate your installation:
cd selenium-grid-1.0 ant sanity-check
Check out that the Hub is running by looking at its console in a browser:
http://localhost:4444/console (See the Browser in the next slide)
Softsmith Infotech 77
Softsmith Infotech
78
How to Run
In a new terminal enter the following command
ant launch-remote-control
Based on your target file you can run either in sequence or in parallel
Softsmith Infotech
79
What's good?
Relatively easy to automate web UI tests Record/Replay for regression tests RC allows integration with CI and JUnit/FitNesse tests
What's bad?
Speed: RC->Browser communication is a speed bottleneck (run in grid, overnight) UI is brittle, tests depending on the UI break a lot (DSTL might fix this, page abstractions as well) Data-backed tests are not easily repeatable (integrate with DB test engines)
Best practices
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 DON'T THINK OF THIS AS A REPLACEMENT FOR EXPLORATORY TESTING!!!
Links
http://gojko.net http://www.openqa.org http://www.solutionsiq.com/agile2008/agile-2008domain.php http://storytestiq.solutionsiq.com http://www.cubictest.com http://fitnesse.info/webtest