You are on page 1of 65

GM QA Community | Automation Forum Series | Session 1 Java Extensibility using QTP

Apr 2013

Sapient Global Markets QA Test Automation Using Selenium


Sept 2012

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.

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.

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 Procedure Selenium 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 seleniumWebdriver-2.x.zip which was downloaded from the selenium site to a directory.

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

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

How Selenium Works

SeleniumRC

SeleniumWebdriver

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.
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) 2) 3) 4)
Record a test case using selenium IDE and save it as Test1.html (say). Record another test case using selenium IDE and save it as Test3.html (or in any other scripting language). 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.

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

10

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

11

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

12

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?
used here for the context of the training.

Depends on your application under test. Most Popular is java, which will be

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

13

Asserts and Verify commands

Assert and verify are verification commands in Selenese


(assertTitle, assertText, verifyTitle, verifyText). Assert
When an "assert" fails, the test is aborted.

Verify
When a "verify"(Soft Assertions) fails, the test will continue execution, logging the failure. With Verify failures user has to check the logs or the console to get details or to modify the code for all individual verification check points.

Assert pass/fail is logged in the result logs automatically.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

14

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

15

Locating elements
Locating by ID
This is the most common way of locating elements since IDs are supposed to be unique for each element. Target Format: id=id of the element

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

16

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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

17

Locating elements
Locating by Link Text This 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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

18

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 ID Tag and class Tag and attribute Tag, class, and attribute Inner text

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

19

Locating elements

Locating by CSS Selector Tag and ID

Syntax

Description 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

css=tag#id

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

20

Locating elements

Locating by CSS Selector Tag and Class


Locating 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.

Description tag = the HTML tag of the element being accessed css=tag.clas . = the dot sign. This should always be present when using a CSS Selector s with class class = the class of the element being accessed

Syntax

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

21

Locating elements
Locating by CSS Selector Tag and Attribute This strategy uses the HTML tag and a specific attribute of the element to be accessed.

Description tag = the HTML tag of the element being accessed [ and ] = square brackets within which a specific attribute and its css=tag[attri corresponding value will be placed bute=value] 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.

Syntax

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

22

Locating elements

Locating by CSS Selector - tag, class, and attribute


Syntax Description

tag = the HTML tag of the element being accessed . = the dot sign. This should always be present when using a CSS Selector with class css=tag.cl class = the class of the element being accessed ass[attribut [ and ] = square brackets within which a specific attribute and its e=value] 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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

23

Locating elements

Locating by CSS Selector inner text


As 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
css=tag:contains(inner text)

Description
tag = the HTML tag of the element being accessed inner text = the inner text of the element

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

24

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:
getElementById getElementsByName dom:name (applies only to elements within a named form) dom:index

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

25

Locating elements

Locating by DOM (Document Object Model) Syntax:


Syntax document.getElementById(id of the element) Description 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 (). name = name of the element as defined by its name attribute index = an integer that indicates which element within getElementsByNames array will be used. 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
26

document.getElementsByName(na me)[index]

document.forms[name of the form].elements[name of the element]

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

Locating elements
Contd.
Syntax Description 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

document.forms[index of the form].elements[index of the element]

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

27

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

28

Locating elements
Locating by Xpath
2 1

3
COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

29

Process of Developing Selenium Java Scripts.


Following are the steps for generating the test scripts in java 1) Record the Script in Selenium IDE and import them into Java 2) 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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

30

Formatting the recorded script into Java


Steps of formatting the code into java

1) 2) 3) 4)

Open the Browser and open the base URL of the application. Open the Selenium IDE and start recording the test steps. Once the recording is done go to Menu Options Format Selenium Junit 4 (webdriver) You can see the java code in the source tab of IDE. Save the test case with .java extension.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

31

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

32

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

33

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

34

Eclipse IDE

Package Explorer

Scrip View

console

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

35

Create a Project In eclipse

File New Java Project

Specify the project name and click finish

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

36

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.jar selenium-java-client-driver.jar selenium-server.jar testng-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.
COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

37

Selenium Libraries

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

38

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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

39

Eclipse IDE with Java Script Template

Your IDE should look like this

This is where you have copy the Java code that you formatted in the Selenium IDE

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

40

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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

41

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

42

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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

43

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

44

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 thats shown in the next slide.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

45

Selenium Results Strip in the Eclipse IDE

success

failure

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

46

Components of the Selenium test Script


setUp method This 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*** method The 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 method This 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.
COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

47

Script View

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

48

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

49

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.

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

50

Screen Shot of the Example Script

Xpath Locator

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

51

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 packages
import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.util.StringTokenizer;

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

52

Parameterization Example

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

53

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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

54

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 run Create a build file to load all the jar files and create targets to the selenium tests using the build file

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

55

Defining a folder Structure for Selenium ANT task

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

56

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.
COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

57

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.
COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

58

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-hub

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)

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

59

How to use Grid (Contd.)

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

60

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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

61

What's good?
Relatively easy to automate web UI tests Record/Replay for regression tests RC allows integration with CI and JUnit/FitNesse tests

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

62

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)

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

63

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!!!

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

64

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

COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL

65

You might also like