This will help you to understand about automation testing and when to do automation on your application using selenium. How selenium works, how to find web elements and what operations we can perform on the web elements to do automation.
2. Session Objective:
● What is Automation
● Automation Tools in The Market
● What is Selenium
● Finding Elements ( Locators / Selectors )
● Basic Operations on elements
● Best Practices / Tips
3. Automation Testing
Automation testing, which is also known as Test Automation, is when the
QA writes scripts and uses another software to test the product. This
process involves automation of a manual process. Automation Testing is
used to re-run the test scenarios that were performed manually, quickly,
and repeatedly.
Why And When To Automate?
● 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 multiple environments
4. Tools in The Market
Open Source Testing Tools
● Selenium
● Watir
● Sikuli
● FitNesse
Commercial Testing Tools
● HP Quick Test Professional
● HP Mercury WinRunner
● IBM Rational Functional Tester
● Micro Focus SilkTest
5. QTP and Selenium are the most used tools in the market for
software automation testing. Hence it makes sense to compare the
pros of Selenium over QTP.
Selenium QTP
Selenium is an open-source tool. QTP is a commercial tool and there is a cost
involved in each one of the licenses.
Supports mobile devices. Supports mobile devices with the help of third-party
tools.
Has capabilities to execute scripts across different
browsers.
Can run tests in specific versions of Firefox, IE, and
Chrome.
Can execute scripts on various operating systems. Works only with Windows.
Can execute tests in parallel with the use of
Selenium Grids.
QTP cannot execute tests in parallel, however
integrating QTP with QC allows testers to execute
in parallel. QC is also a commercial tool.
6. Introduction To Selenium
Who named Automation Testing Tool as Selenium ?
● Selenium was originally developed by Jason Huggins in 2004 as an internal
tool at ThoughtWorks.
● The name Selenium comes from a joke made by Huggins in an email,
mocking a competitor named HP Mercury, saying that you can cure mercury
poisoning by taking selenium supplements. The others that received the email
took the name and ran with it.
● Hence the Name…
7. Selenium is a set of tools for
cross-platform automated
testing of web application.
Selenium Supports:
● IE, Firefox, Chrome, Safari and
Other browsers.
● Windows, Linux, OS X and Other
OS’s.
● C#, Java, Ruby, Python, Perl, PHP
and Other languages.
● JUnit, RSpec, TestNG, unittest,
NUnit.
What is Selenium ?
9. Selenium Component - Selenium Core :
○ It is component core Javascript library that could drive interactions with the page,
automatically rerun tests against multiple browsers.
○ Selenium Core is the foundation of Selenium Remote Control (RC) and Selenium
IDE.
10. Selenium Component -Selenium IDE :
○ Selenium IDE (Integrated Development Environment) is a prototyping tool for
building test scripts.
○ It is a Firefox plugin and provides an easy-to-use interface for developing
automated tests.
○ Create & Edit Test Cases & Test Suites.
○ Selenium IDE has a recording feature, which records user actions as they are
performed.
○ Exports them as a reusable script in one of many programming languages that
can be later executed.
○ Run Selenese Commands.
Selenium Component - Selenium IDE :
11. Selenium Component - Selenium RC (Remote Control) :
○ It is a test tool that allows you to write automated web application UI tests in any
programming language against any HTTP website using any mainstream
JavaScript-enabled browser.
○ Selenium RC comes in two parts.
i. A server which automatically launches and kills browsers, and acts as a
HTTP proxy for web requests from them.
ii. Client libraries for your favorite computer language.
12. Selenium Component
● Selenium RC (Remote Control):
● User creates test scripts in a desired
programming language.
● Client library (interface b/w programming
languages & the Selenium-RC Server) deports
the test commands to the selenium server.
● The Selenium RC Server acts as a "middleman"
between your Selenium commands and your
browser.
● When you begin testing, Selenium RC Server
"injects" a Javascript program called Selenium
Core into the browser.
● Selenium RC Server deciphers and converts the
test commands into JavaScript commands and
sends them to the browser.
● Browser executes the commands using
selenium core and sends results back to the
Selenium RC Server
● Selenium RC Server delivers the test results to
the client library.
13. Selenium Component - Selenium WebDriver :
○ Selenium WebDriver is the successor to Selenium RC which sends
commands directly to the browser and retrieves results.
○ WebDriver can support the headless execution..
○ Purely object-oriented API.
○ Can test iPhone/Android applications.
15. Selenium Component - Selenium WebDriver :
1. Language Level Bindings : These languages in which are making an framework, will interact with
the Selenium Webdriver and work on various browsers and other devices. So we have a common
API that we use for Selenium that has a common set of commands and we have various bindings
for the different languages. So you can see there’s Java, Java, Python, Ruby, there’s also some
other bindings and new bindings can be added very easily.
2. Selenium Webdriver API : Now these bindings communicate with Selenium WebDriver API and this
API send the commands taken from language level bindings interpret it and sent it to Respective
driver. In short term API contains set of common library which allow sending command to
respective drivers.
3. Drivers : Internet Browser Specific Drivers such as IE, FF, Chrome etc. The basic idea is each one
of these drivers knows how to drive the browser that it corresponds to.
For E.g. Chrome Driver knows how to handle the low level details of chrome browser and drive it to do
things like clicking the target element, navigating the target page etc.
16. Selenium Component - Selenium Grid:
○ Run your tests against different browsers, operating systems, and
machines all at the same time. This will ensure that the application you
are Testing is fully compatible with a wide range of browser-O.S
combinations.
○ Save time in the execution of your test suites. If you set up Selenium
Grid to run, say, 4 tests at a time, then you would be able to finish the
whole suite around 4 times faster.
17. Selenium Component - Selenium Grid:
What is a Hub and Node?
● The Hub
○ The hub is the central point where you
load your tests into.
○ There should only be one hub in a grid.
○ The hub is launched only on a single
machine, say, a computer whose O.S is
Windows 7 and whose browser is FF.
○ The machine containing the hub is
where the tests will be run, but you will
see the browser being automated on
the node.
● The Nodes
○ Nodes are the Selenium instances
that will execute the tests that you
loaded on the hub.
○ There can be one or more nodes
in a grid.
○ Nodes can be launched on
multiple machines with different
platforms and browsers.
○ The machines running the nodes
need not be the same platform as
that of the hub.
18. Locators
Selenium uses what is called locators to find and match the elements of your page
that it needs to interact with.
The different types of locator are:
● ID
● Link Text
● CSS Selector
○ Tag and ID
○ Tag and class
○ Tag and attribute
○ Tag, class, and attribute
● Name
● XPath
● DOM (Document Object Model)
○ getElementById
○ getElementsByName
○ Dom:name
○ Dom:index
19. Locators
Locating by ID : The most efficient way and preferred way to locate an element on a web page is By
ID. ID will be the unique on web page which can be easily identified.
<form id="login">
<input id=”username” name="username" type="text"/>
<input id="password" name="password" type="password"/>
<input type="submit" name="signin" value="SignIn" />
</form>
WebElement item = driver.findElement(By.id("username"));
page.find_by_id(‘username’)
20. Locators
Locating by Name: When there is no Id to use, the next worth seeing if the desired element
has a name attribute. But make sure there the name cannot be unique all the times. If there are
multiple names, Selenium will always perform action on the first matching element.
<input id=”username” class=”uname” name="username" type="text"/>
<input id="password" name="password" type="password"/>
<input type="submit" name="signin" value="SignIn" />
WebElement locator = driver.findElement(By.name("password"));
WebElement element =driver.findElement(By.className(“uname”));
Locating by Class: The CSS class locator uses a specific class attribute to get to the first
element on a web page.
21. Locators
Locating by Link Text : Finding an element with link text is very simple. But make sure, there is only
one unique link on the web page. If there are multiple links with the same link text (such as repeated
header and footer menu links), in such cases Selenium will perform action on the first matching element
with link.
<a href="http://www.google.com/image1/downloads">Downloads</a>
WebElement download = driver.findElement(By.linkText("Downloads"));
Locating By Partial Link Text: In the same way as LinkText, PartialLinkText also works in the
same pattern. User can provide partial link text to locate the element.
<a href="http://www.google.com/image1/downloads">Downloads</a>
WebElement download = driver.findElement(By.PartialLinkText("Down"));
22. Locators
Locating by CSS Selector: When we don't have an option to choose Id or Name, we should
prefer using CSS Selectors as the best alternative. CSS Selectors are string patterns used to
identify an element based on a combination of HTML tag, id, class, and attributes.
CSS locator Examples using ID and Class attributes:
<input id=”username” class=”uname” name="username" type="text"/>
<input id="password" class=”pass” name="password" type="password"/>
<input type="submit" name="signin" value="SignIn" />
WebElement Username = driver.findElement(By.cssSelector("input#username"));
WebElement Username = driver.findElement(By.cssSelector("input.uname"));
Syntax
css=tag#id
Syntax
css=tag.class
Syntax
css=tagName[attributename=attributeValue]
23. Locators
Locating by CSS Selector: Using CSS Selectors, we can also locate elements with sub-strings.
They are really helpful when you are working with dynamically generated ids /classes in web page
<input id=”username” class=”uname” name="username" />
<input id="password" name="password" type="password"/>
<input type="submit" name="signin" value="SignIn" />
Following special characters are important in CSS Locators for Sub-string matches:
1. '^' symbol represents the starting text in a string.
css=input[id^='pass']
2. '$' symbol represents the ending text in a string.
css=input[id$='name']
3. '*' symbol represents contains text in a string.
css=input[id*='erna']
24. Locators
Locating by CSS Selector (Tag and attribute):
<input id=”username” class=”uname” name="username" type="text"/>
<input id="Passwd" class=”pass” name="pword" type="password"/>
<input type="submit" name="signin" value="SignIn" />
WebElement Username = driver.findElement(By.cssSelector("input[type=’submit’]"));
Locating by CSS Selector (Tag, class, and attribute):
WebElement Username =
driver.findElement(By.cssSelector("input#Passwd[type=’password’][name=’pword’]"));
Syntax: tagName[attributename=attributeValue]
Syntax: tagName<. Or #><value of Class or ID attribute><[attributename=attributeValue]>
25. Locators
Locating by DOM Selector (getElementById): The Document Object Model (DOM), in simple
terms, is the way by which HTML elements are structured.
<input id=”username” class=”uname” name="username" />
<input id="password" name="password" type="password"/>
<input type="submit" name="signin" value="SignIn" />
Syntax: document.getElementById("id of the element")
WebElement email = (WebElement) ((JavascriptExecutor) driver).executeScript("return
document.getElementById('username');");
26. Locators
Locating by DOM Selector (getElementsByName): The getElementById
method can access only one element at a time, as ID of an element is unique. In
case if getElementsByName method, we get an array of elements because
multiple elements can share the same name. We access the individual elements
using an index which starts at 0.
Syntax: document.getElementsByName("name")[index]
<input id=”Firstname” name="my-name" />
<input id=”Lastname” name="my-name" />
<input id="password" name="password" type="password"/>
<input type="submit" name="signin" value="SignIn" />
WebElement last = (WebElement) ((JavascriptExecutor) driver).executeScript("return
document.getElementsByName('my-name')[0];");
27. Locators
Locating by DOM Selector (Dom:name): This method will only apply to the element which
contains name attribute.
<form class=”my-form” name=”login”>
First name:<input type="text" name="firstname" value="lucifer">
Last name:<input type="text" name="lastname" value="Morningstar">
Password:<input name="pword" type="password"/>
</form>
Syntax: document.forms[“ form name"].elements["element name"]
WebElement Fname = (WebElement) ((JavascriptExecutor) driver).executeScript("return
document.forms['login'].elements['lucifer'];");
28. Locators
Locating by DOM Selector (Dom:index): This method works even when the
element (form) has no name because it uses the form's index and not its name.
Syntax: document.forms[form index].elements[element index]
<form class=”my-form” name=”login”>
First name:<input type="text" name="firstname" value="lucifer">
Last name:<input type="text" name="lastname" value="Morningstar">
Password:<input name="pword" type="password"/>
</form>
WebElement Lastname = (WebElement) ((JavascriptExecutor) driver).executeScript("return
document.forms[0].elements['1’'];");
29. Locators
Locating by XPATH: XPath is defined as XML path. It is one method which guarantees to locate any
element on the page using the XPath expression. But we should be very careful while forming an XPath
as it may not work if there are changes in the web application.
Syntax: Xpath=//tagname[@attribute='value']
● // : Select current node.
● Tagname: Tagname of the particular node.
● @: Select attribute.
● Attribute: Attribute name of the node.
● Value: Value of the attribute.
There are two types of XPath:
1. Absolute XPath.
2. Relative XPath .
● / : root node.
30. Locators
Locating by XPATH(Absolute XPath): It is the direct way to find the element, but if some
other tag added in between, then this path will no longer work. When are talking about
Absolute XPath begins with the single forward slash(/) , which means you can select the
element from the root node.
<html>
<body>
<form id=”my-form” name=”login”>
<input type="text" name="firstname" value="lucifer"/>
<input type="text" name="lastname" value="Morningstar"/>
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
XPATH will work If the Path we defined as
firstname = driver.find_element_by_xpath("/html/body/form/input[1]")
But If there is a tag that has added between body and form as belowit won’t work
firstname = driver.find_element_by_xpath("html/body/div/form/input[1]")
31. Locators
Locating by XPATH(Relative Xpath): For Relative Xpath the path starts from the middle of the
HTML DOM structure. It starts with the double forward slash (//), which means it can search the element
anywhere at the webpage.
<html>
<body>
<form id=”my-form” name=”login”>
<input type="text" name="firstname" value="lucifer"/>
<input type="text" name="lastname" value="Morningstar"/>
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
firstname = driver.find_element_by_xpath("//form/input[@name=firstname]")
firstname = driver.find_element_by_xpath("//form[@id=my-form]/input[1]")
firstname = driver.find_element_by_xpath("//input[@name=firstname]")
32. Basic Operations
Basic Operations on elements: Once we identify web elements using a suitable
selector strategy, we will need to interact with the elements. The methods we will be using
depends on the type of WebElement we interact with.
For example, we can sendKeys() to a text field, click() on a link etc. Below given commands
are commonly used commands of selenium webdriver to use in automation process of any
software web application.
● Command To Open URL In Browser:
driver.navigate().to("https://www.facebook.com/");
driver.navigate().back();
driver.navigate().forward();
33. Basic Operations
● Store text of targeted element in variable:
String dropdown = driver.findElement(By.tagName("select")).getText();
● Typing text in text box or text area.
driver.findElement(By.id("my-name")).sendKeys("My First Name");
● Clicking on any element or button of webpage:
driver.findElement(By.id("submitButton")).click();
● Selenium WebDriver Assertions.
Assert.assertEquals(actual, expected);
Assert.assertNotEquals(actual, expected);
● Submit() method to submit form
driver.findElement(By.xpath("//input[@name='Company']")).submit();
34. Basic Operations
● Handling Alert, Confirmation and Prompts Popups:
String myalert = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
driver.switchTo().alert().dismiss();
driver.switchTo().alert().sendKeys("This Is Lucifer");
● Implicit wait in webdriver
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
● Explicit wait in webdriver with WebDriver
WebDriverWait wait = new WebDriverWait(driver, 20);
These were the basic operation that we do on the webpage, operations also Handling Multiple
Windows, Handling Iframes, Fileupload, Datepicker etc.
35. Best Practise / TIPS
● Understand the application being tested:
○ Business Model
○ Application Flow
● Good Automation means good manual test case:
It is always advisable to first write the test case in manual form. One should run this test case
manually at least once to clearly decide what objects need to be identified and what will be the flow
of navigation. Ask questions with manual testers.
● Know The Selenium methods well
● Know The Selectors well
Preferred selector order: id > name > css > xpath
● More you write more you learn