Professional Documents
Culture Documents
Apr 2013
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.
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.
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.
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
10
Command :
11
For building the frame work we require continuous build integration tools
like Ant, Maven or cruise control.
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
13
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.
14
waitFor commands
waitFor commands will fail and halt the test if the condition
does not become true within the current timeout setting (setTimeout).
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
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
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
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
19
Locating elements
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
20
Locating elements
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
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
22
Locating elements
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.
23
Locating elements
Syntax
css=tag:contains(inner text)
Description
tag = the HTML tag of the element being accessed inner text = the inner text of the element
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
25
Locating elements
document.getElementsByName(na me)[index]
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
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.
28
Locating elements
Locating by Xpath
2 1
3
COPYRIGHT 2011 SAPIENT CORPORATION | CONFIDENTIAL
29
30
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.
31
32
Installing Eclipse
33
34
Eclipse IDE
Package Explorer
Scrip View
console
35
36
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
38
39
This is where you have copy the Java code that you formatted in the Selenium IDE
40
41
42
43
44
45
success
failure
46
47
Script View
48
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.
50
Xpath Locator
51
Parameterization
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;
52
Parameterization Example
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
54
55
56
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
59
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
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
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)
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!!!
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
65