test automation using selenium and java
DESCRIPTION
seleniumTRANSCRIPT
![Page 1: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/1.jpg)
GM QA Community | Automation Forum Series | Session 1Java Extensibility using QTP
Apr 2013
![Page 2: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/2.jpg)
Sapient Global Markets QA
Test Automation Using Selenium
Sept 2012
![Page 3: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/3.jpg)
3© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
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.
![Page 4: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/4.jpg)
4© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenium Webdriver
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 Can be run on multiple client from a single host.
![Page 5: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/5.jpg)
5© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Installing Selenium Webdriver
Software Required JDK 1.6 , selenium-Webdriver-2.x(this can be
downloaded from http://seleniumhq.org/download/)Installation ProcedureSelenium Webdriver 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 selenium-
Webdriver-2.x.zip which was downloaded from the selenium site to a directory.
![Page 6: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/6.jpg)
6© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenium Test Automation Process
• First Generate the Script using selenium IDE in the firefox IDE• Once the Scripts are recorded add assertions where ever required• Now format the Selenese test into the language of your choice. Please refer to the Image
![Page 7: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/7.jpg)
7© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenium Test Automation Process
• Once the Selenese script is converted into your preferred language you can you can run 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 an IDE like netbeans or Eclipse IDE (JAVA)
• To Integrate the script and run them as a suite we will require build integration tools like Maven or Ant.
![Page 8: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/8.jpg)
8© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
How Selenium Works
Selenium-RC
Selenium-Webdriver
Selenium Web Driver works in the same way users interacts with the web pages. In contrast to Selenium RC, it doesn't need a server to run. It directly starts a browser
instance and controls it.
![Page 9: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/9.jpg)
9© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Running a Selenese Test on Different Browsers
Following are the steps to create a selenese test suite and run a test suite using selenium
1) Record a test case using selenium IDE and save it as “Test1.html” (say).2) Record another test case using selenium IDE and save it as “Test3.html” (or in any other
scripting language).3) In this way you can record any number of test case using selenium IDE and save them as
<filename>.html4) Then Create a New Test Suite using the File Menu. See the Image Below.
![Page 10: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/10.jpg)
10© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenese Suite cont..
Add the created test cases in to the suite as shown in Image1
Once the test cases are added to the suite save them as a test suite as in Image2
Then Open the command prompt and navigate to the directory where Selenium server is placed.
In the command prompt Enter the command as shown below.
![Page 11: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/11.jpg)
11© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenese Suite cont..
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 in the results.html file as shown below.
![Page 12: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/12.jpg)
12© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenium Test Case Development Using Java
Required Software:
selenium-java.2.32.0.zip , Selenium Java Client Driver jar , JDK 1.6 +, Eclipse (or any other IDE), Junit jar and testing jar
For building the frame work we require continuous build integration tools like Ant, Maven or cruise control.
![Page 13: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/13.jpg)
13© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Client Drivers
Enables communication of browsers with Selenium.
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?
Depends on your application under test. Most Popular is java, which will be used here for the context of the training.
![Page 14: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/14.jpg)
14© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Asserts and Verify commands
• Assert and verify are verification commands in Selenese (assertTitle, assertText, verifyTitle, verifyText).
Assert Verify
When an "assert" fails, the test is aborted.When an "assert" fails, the test is aborted. When a "verify"(Soft Assertions) fails, the When a "verify"(Soft Assertions) fails, the test will continue execution, logging the test will continue execution, logging the failure. failure.
Assert pass/fail is logged in the result logs Assert pass/fail is logged in the result logs automatically.automatically.
With Verify failures user has to check the With Verify failures user has to check the logs or the console to get details or to logs or the console to get details or to modify the code for all individual modify the code for all individual verification check points. verification check points.
![Page 15: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/15.jpg)
15© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
waitFor commands
• "waitFor" commands wait for some condition to become true (which can be useful for testing Ajax applications). They will succeed immediately if the condition is already true.
• waitFor commands will fail and halt the test if the condition does not become true within the current timeout setting (setTimeout).
• Example: waitForElementPresent or waitForVisible, which wait dynamically, checking for the desired condition every second and continuing to the next command in the script as soon as the condition is met.
![Page 16: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/16.jpg)
16© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by ID
This is the most common way of locating elements since ID’s are supposed to be unique for each element.Target Format: id=id of the element
![Page 17: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/17.jpg)
17© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
• Locating by Name
o Locating elements by name are very similar to locating by ID, except that we use the “name=” prefix instead.
o Target Format: name=name of the element
![Page 18: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/18.jpg)
18© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by Link TextThis type of locator applies only to hyperlink texts. We access the link by prefixing our target with “link=” and then followed by the hyperlink text.Target Format: link=link_text
![Page 19: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/19.jpg)
19© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by CSS Selector
CSS Selectors are string patterns used to identify an element based on a combination of HTML tag, id, class, and attributes.Locating by CSS Selector is more complicated than the previous methods, but it is the most common locating strategy of advanced Selenium users because it can access even those elements that have no ID or name.
CSS Selectors have many formats, but we will only focus on the most common ones.
Tag and IDTag and classTag and attributeTag, class, and attributeInner text
![Page 20: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/20.jpg)
20© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
• Locating by CSS Selector – Tag and ID
Syntax Description
css=tag#id
•tag = the HTML tag of the element being accessed•# = the hash sign. This should always be present when using a CSS
Selector with ID•id = the ID of the element being accessed
![Page 21: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/21.jpg)
21© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by CSS Selector – Tag and ClassLocating by CSS Selector using an HTML tag and a class name is similar to using a tag and ID, but in this case, a dot (.) is used instead of a hash sign.
Syntax Description
css=tag.class
•tag = the HTML tag of the element being accessed•. = the dot sign. This should always be present when using a CSS Selector
with class•class = the class of the element being accessed
![Page 22: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/22.jpg)
22© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by CSS Selector – Tag and AttributeThis strategy uses the HTML tag and a specific attribute of the element to be accessed.
Syntax Description
css=tag[attribute=value]
•tag = the HTML tag of the element being accessed•[ and ] = square brackets within which a specific attribute and its
corresponding value will be placed•attribute = the attribute to be used. It is advisable to use an attribute that is
unique to the element such as a name or ID.•value = the corresponding value of the chosen attribute.
![Page 23: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/23.jpg)
23© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
• Locating by CSS Selector - tag, class, and attribute
Syntax Description
css=tag.class[attribute=value]
•tag = the HTML tag of the element being accessed•. = the dot sign. This should always be present when using a CSS Selector with class•class = the class of the element being accessed•[ and ] = square brackets within which a specific attribute and its corresponding value will be placed•attribute = the attribute to be used. It is advisable to use an attribute that is unique to the element such as a name or ID.•value = the corresponding value of the chosen attribute.
![Page 24: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/24.jpg)
24© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by CSS Selector – inner textAs you may have noticed, HTML labels are seldom given id, name, or class attributes. So, how do we access them? The answer is through the use of their inner texts. Inner texts are the actual string patterns that the HTML label shows on the page.
Syntax Description
css=tag:contains(“inner text”)•tag = the HTML tag of the element being accessed•inner text = the inner text of the element
![Page 25: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/25.jpg)
25© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by DOM (Document Object Model)The Document Object Model (DOM), in simple terms, is the way by which HTML elements are structured. Selenium IDE is able to use the DOM in accessing page elements. If we use this method, our Target box will always start with “dom=document...”; however, the “dom=” prefix is normally removed because Selenium IDE is able to automatically interpret anything that starts with the keyword “document” to be a path within the DOM anyway.There are four basic ways to locate an element through DOM:
getElementByIdgetElementsByNamedom:name (applies only to elements within a named form)dom:index
![Page 26: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/26.jpg)
26© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
• Locating by DOM (Document Object Model) Syntax:
Syntax Description
document.getElementById(“id of the element”)
id of the element = this is the value of the ID attribute of the element to be accessed. This value should always be enclosed in a pair of parentheses (“”).
document.getElementsByName(“name”)[index]
•name = name of the element as defined by its ‘name’ attribute•index = an integer that indicates which element within getElementsByName’s array will be used.
document.forms[“name of the form”].elements[“name of the element”]
•name of the form = the value of the name attribute of the form tag that contains the element you want to access•name of the element = the value of the name attribute of the element you wish to access
![Page 27: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/27.jpg)
27© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Contd.
Syntax Description
document.forms[index of the form].elements[index of the element]
•index of the form = the index number (starting at 0) of the form with respect to the whole page•index of the element = the index number (starting at 0) of the element with respect to the whole form that contains it
![Page 28: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/28.jpg)
28© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by Xpath
XPath is the language used when locating XML (Extensible Markup Language) nodes. Since HTML can be thought of as an implementation of XML, we can also use XPath in locating HTML elements.
Advantage: It can access almost any element, even those without class, name, or id attributes. Disadvantage: It is the most complicated method of identifying elements because of too many different rules and considerations.
Fortunately, Firebug can automatically generate XPath locators.
![Page 29: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/29.jpg)
29© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Locating elements
Locating by Xpath
11
22
33
44
![Page 30: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/30.jpg)
30© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Process of Developing Selenium Java Scripts.
Following are the steps for generating the test scripts in java1) Record the Script in Selenium IDE and import them into Java2) Create a Selenium Java project in Eclipse and load all the necessary jar
files 3) Copy the formatted Java code into the Eclipse and run the test.
![Page 31: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/31.jpg)
31© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Formatting the recorded script into Java
Steps of formatting the code into java
1) Open the Browser and open the base URL of the application.
2) Open the Selenium IDE and start recording the test steps.
3) Once the recording is done go to Menu Options Format Selenium Junit 4 (webdriver)
4) You can see the java code in the source tab of IDE. Save the test case with .java extension.
![Page 32: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/32.jpg)
32© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Setting Up an Eclipse Project for Selenium Automation
• Eclipse is an open source community whose projects are focused on building an extensible development platform for building Java applications and frameworks. Eclipse is one of the best Java IDE and as a matter of face Eclipse is much more than a Java IDE.
• We can configure a selenium project in eclipse and even run the scripts from eclipse.
• Using eclipse its easy to enhance the recorded script. We can add power to the recorded script by parameterizing the test inputs and even validate the back values.
• Eclipse also allows us to write reusable code for efficient test automation.
![Page 33: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/33.jpg)
33© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
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.
![Page 34: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/34.jpg)
34© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Setting up the workspace
• Create a folder say (selenium) in any one of the directory and change the workspace location to the directory created by you. Then click ok button.
• On doing so you will see the eclipse welcome screen if you are doing it for the first time. On the welcome screen click on the workbench icon to open the project explorer.
• When you to try to open the eclipse from next time it will directly show the project explorer as shown in the next slide.
![Page 35: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/35.jpg)
35© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Eclipse IDE
PackageExplorer
Scrip View
console
![Page 36: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/36.jpg)
36© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Create a Project In eclipse
• File New Java Project • Specify the project name and click finish
![Page 37: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/37.jpg)
37© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Adding the required Jars
• Open the folder contain the selenium project that you have just created. Inside that folder create another folder with name “lib”
• Inside the lib folder place the following jar files.junit-4.8.1.jarselenium-java-client-driver.jarselenium-server.jartestng-5.12.jars
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.
![Page 38: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/38.jpg)
38© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenium Libraries
![Page 39: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/39.jpg)
39© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Creating a package and adding a class file
• In the eclipse Package Explorer right click on the src (source) folder. In that select New Package and give a name to the package. See the Picture1
• Now right click on the created package and click on new class and provide a class name. You will see a java script template as seen in the next slide
![Page 40: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/40.jpg)
40© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Eclipse IDE with Java Script Template
• Your IDE should look like this
This is where you have copy theJava code that you formatted in the
Selenium IDE
![Page 41: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/41.jpg)
41© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Adding the Jar files to the class path
• Right Click on the Project• Select Build Path Configure Build path• On the next window click on the libraries tab
and click on the add jars tab
![Page 42: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/42.jpg)
42© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Adding the Jar files to the class path
• As you click on the add jars button a window showing all the jar files will be opened. Select all the jar files and click on ok button
• By doing this step we are almost ready to write the script and run it.
![Page 43: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/43.jpg)
43© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Creating the test script inside the class file• Copy the code that was formatted in the selenium IDE and paste it in the Eclipse IDE script 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
![Page 44: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/44.jpg)
44© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Running the test through Eclipse
• 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.
![Page 45: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/45.jpg)
45© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Running the test through Eclipse
• Once the server is up go to the eclipse and right click on the script.
• In that select Run As JUnit Test. See the image below. After this you will get will see that the script has run successfully in the window that’s shown in the next slide.
![Page 46: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/46.jpg)
46© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Selenium Results Strip in the Eclipse IDE
success
failure
![Page 47: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/47.jpg)
47© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Components of the Selenium test Script
setUp methodThis method prepares the selenium server to run the test. This method basically hooks to the selenium server and opens up the base URL of the application
test*** methodThe methods with the name test will actually run the tests on the application. There can be any number of test methods in the app
tearDown methodThis method will run after the end of the test. This test will disconnect with the server and makes room for the next tests to run the tests.
![Page 48: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/48.jpg)
48© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Script View
![Page 49: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/49.jpg)
49© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Enhancing the Selenium commands
The generated selenium command can be enhanced by writing java commands. Using java methods we can do parameterization and data base validation.More over java methods can be used for to perform some complex validations and testing activities.The best way to use java methods is used to is have a Java IDE like eclipse.
![Page 50: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/50.jpg)
50© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Example 1 – Get the values from the list box and check if the value “HTML” is present in it
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.
![Page 51: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/51.jpg)
51© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Screen Shot of the Example Script
Xpath Locator
![Page 52: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/52.jpg)
52© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Parameterization
• Parameterization is to run the same test 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 packagesimport java.io.File;import java.io.BufferedReader;import java.io.FileReader;import java.util.StringTokenizer;
![Page 53: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/53.jpg)
53© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Parameterization Example
![Page 54: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/54.jpg)
54© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
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
![Page 55: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/55.jpg)
55© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
To Integrate with ANT
Download ANT and copy it into any of the directory
Create a JAVA_HOME environment Variable
Create a ANT_HOME environment variables
Add the ANT bin path to windows path environment variable
Create a build file to load all the jar files and create targets to run the selenium tests using the build file
![Page 56: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/56.jpg)
56© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Defining a folder Structure for Selenium ANT task
![Page 57: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/57.jpg)
57© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Running the ANT selenium task
• Start the selenium server through the command prompt
• Then go the folder where the selenium tests are stored
• Make sure that the build.xml file is present in that location
• Now run the command “ant” from the command prompt
• You can see the test running and the results will be saved in the results.txt file.
![Page 58: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/58.jpg)
58© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
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.
![Page 59: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/59.jpg)
59© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
How to use 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
• Go to the selenium distribution directory and launch in a new terminal:
ant launch-hubCheck 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)
![Page 60: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/60.jpg)
60© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
How to use Grid (Contd.)
![Page 61: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/61.jpg)
61© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
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
![Page 62: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/62.jpg)
62© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
What's good?
• Relatively easy to automate web UI tests• Record/Replay for regression tests• RC allows integration with CI and JUnit/FitNesse tests
![Page 63: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/63.jpg)
63© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
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)
![Page 64: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/64.jpg)
64© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
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!!!
![Page 65: Test Automation Using Selenium and JAVA](https://reader035.vdocuments.us/reader035/viewer/2022081506/55cf98e7550346d0339a5976/html5/thumbnails/65.jpg)
65© COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
Useful links
• http://gojko.net
• http://www.openqa.org
• http://www.solutionsiq.com/agile2008/agile-2008-domain.php
• http://storytestiq.solutionsiq.com
• http://www.cubictest.com
• http://fitnesse.info/webtest