Selenium
Selenium
Automation Testing
Automation Testing is an automatic technique where tester write the script using the
automation tool to execute test cases and test the software.
We can also say that automation testing is used to automate the respective tasks which are
difficult to perform manually
The main goal of automation testing is to increase the test efficiency and develop a quality of
software.
Converting manually test cases into automation test script is called automation testing.
Advantages
It increases the test execution speed
It helps in the maximum coverage of test cases.
It saves the time and cost while compare to manually testing.
We can re-use the test scripts
It maintains good consistency
It improves quality of the software.
Selenium Tool
Selenium is free & open-source web application automation tool (web application functional testing
automation testing tool)
It is a file based on zip file format where many files are converted into one single file known
as jar files.
We need jar files to import the properties based on the requirement
1. Internally
a. Right click on the project and select properties option
b. Select java build path & click on libraries
c. Select classpath and click on add jars and go to the project and select the jar files
d. Next click on apply and close
2. Externally
a. Right click on the project and select properties option
b. Select java build path & click on libraries
c. Select classpath and click on add external jars and go to the appropriate location and
click on open
d. Next click on apply and close
Selenium Architecture
NOTE
Don’t add driver.exe file to build path because eclipse will not allow us to add .exe file to the build
path.
1. To specify the path either we add it manually to environment variable or we should add it
programmatically using System.setProperty(key, value) statement.
2. While specifying the path we use relative path by specifying the “.” operator at the
beginning which represent current java project
3. We should set the path before opening the browser only, else we will get illegal state
exception.
4. For easy maintenance we set the path of all driver executables in static block as shown
below:
Ex:
Architecture for WebDriver/Selenium Java Language
Binding
The super most interface is search context which is extended by Web Driver interface.
Web Driver interface is implemented in Remote Web Driver class.
Remote Web Driver class implements other interfaces also, such as JavaScriptExecutor,
TakeScreenShot, etc.
All the browser specific classes such as Chrome driver, Firefox driver & Internet explorer
driver, etc extends Remote Web Driver class.
Ex:
Ex:
getPageSource()
Write a script to print HTML source code of a webpage
Web Element
Anything present on the web page is called as web element, such as button, link, text box…
etc.
Web element are created using HTML language (Hyper Text Markup Language)
Each HTML contains three things:
o Tag (tag name)
o Attribute
Attribute name
Attribute value)
o Text (visible text)
Ex: HTML code for login button
<div id=”d1”> Login </div>
In the above HTML code div is the tag name, id is the Attribute name, d1 is
Attribute value & login is the visible text
In order to see the HTML code of the required element; right click on the element
and select the option inspect, if right click is disabled on the text then press
Ctrl+Shift+i or F12 key. It will display developer’s tool bar
In order to inspect another element click on inspect button and then click on
required element.
NOTE
In selenium before performing any action such as clicking, typing & selecting, etc. we should find the
element using locators
Locators
Locators are used to find the elements, in selenium there are 8 type of locators & all are static
method present in By class.
1. tagName()
2. id()
3. name()
4. className()
5. linkText()
6. partialLinkText()
7. cssSelector()
8. xpath()
By is an abstract class and all the locators will take string as an argument
Working procedure
HTML code for static webpage is:
<html>
<body>
<a id=”d1” name=”n1” class=”c1” href=https://www.qspiders.com/>Google</a><br>
<a id=”d1” name=”n1” class=”c1” href=https://www.qspiders.com/>Google</a><br>
</body>
</html>
NOTE
Type the above HTML code in the notepad and save it as demo.html on the desktop, inorder to
create a static webpage.
Write a script to click on Google link by using tag name locator
Interview Questions
Write a script to click on google link by using tagName(), id(), name(), className()
Syntax: driver.findElement(By.LinkText(“Google”)).click;
NOTE
If the text of the link is changing partially we can use Partial LinkText.
Ex:
HTML Code
<a…>Inbox(7)</a>
<a…>Inbox(7)</a>
Syntax: driver.findElement(By.PartialLinkText(“Inbox”)).click();
CSS Selector
CSS stands for Cascading Style Sheet
CSS selector is one of the locator in selenium
Syntax: tag[Attribute_Name=’Attribute_Value’]
Ex:
a[id=’d1’]
a[name=’n1’]
a[class=’c1’]
a[href=’https://www.google.com’]
These are called as CSS Expression
NOTE
We can check the CSS expression in the browser by using following steps:
If we get;
1 of 1 -> One matching Elements
1 of 3 -> Multiple matching elements
0 of 0 -> No matching elements.
X-Path
It is path of the element in a HTML tree
While writing X-Path it starts with “.” It also represents current webpage or HTML
document, using “.” Is not mandatory
To navigate from parent to child element (tag) we use “/” (Single Backward Slash)
Ex: ./HTML/body/a
Type:
Absolute X-Path
Relative X-Path
1. X-Path by Attributes
2. X-Path by Text Function
3. X-Path by Contains Function
4. Traversing X-Path
5. Independent-Dependent X-path
6. X-Path by group Index
Absolute X-Path
Starting from HTML to desired element/required element is called absolute X-path.
X-Path Index
In X-Path we can use index which starts from 1, if there is another element under the same parent
with the same tag name then the index becomes 2 and so on....
Ex:
Ex:
HTML
Body
1. div
1. a link 1
2. a link 2
2. div
1. a link 3
1. img img
2. a link 4
Questions
/HTML/body/div[1]/a[1]
/HTML/body/div[1]/a[2]
/HTML/body/div[2]/a[1]
/HTML/body/div[2]/a[2]
Relative X-Path
Relative X-Path starts with “//” which represents descendent (Child, grand child, great grand child,...,
etc)
Ex:
HTML
Body
3. div
1. a link 1
2. a link 2
4. div
1. a link 3
3. img img
4. a link 4
Questions
//div[1]/a[1]
//div[1]/a[2]
//div[2]/a[1]
//div[2]/a[2]
//div/a[1]
//div/a[2]
7. Write a X-Path for link 1 & 2
//div[1]/a
//div[2]/a
//a or //div/a
//img
Or
//div/img
/ //
It represents child It represents descendent (Child, grand child, great grandchild)
//a //table//a
It matches with all the links which are present It matches with all the links which are present
in anywhere of the webpage inside the table
Questions
Write an X-Path to identify all the links & all the images
//a|//img
//div[1]//a[1]|//div[2]//a[2]
//div[1]//a[2]|//div[2]//a[1]
X-Path by Attribute
We can also use attribute of the element while writing X-Path.
Syntax:
//tag_name[@Attribute_name = ‘Attribute_value’]
And Condition
Syntax:
OR Condition
Syntax:
Not Condition
Syntax:
//tag_name[not(@Attribute_name1 = ‘Attribute_value1’ )]
Questions
HTML Code
Syntax: //tag_name[text()=’value’]
Ex1:
HTML Code
<div>Login</div>
X-Path: //div[text()=’Login’]
Ex2:
HTML
Or
X-Path by Contains
If the text value is partially changing then we can use contains function in the X-Path.
Syntax: //tag_name[contains(text(),’value’]
Ex:
HTML Code
X-Path:
//nobr[contains(text(),’actiTime’)]
HTML Code
<a….>Inbox(7)</a>
X-Path
//a[contains(text(),’Inbox’)]
Or
//a[contains(.,’Inbox’)]
NOTE
Syntax:
//tag[contains(@Attribute_name, ‘Attribute_value’)]
Ex:
HTML Code
X-Path
//img[contains(@class,’logo’)]
Or
//img[contains(@src,’fbcdn’)]
Traversing X-Path
Navigating from one element to another element is called as traversing
Type:
1. Forward Traversing
Navigating from parent element to child is called as Forward traversing
2. Backward Traversing
Navigating from child element to parent element is called backward traversing.
Ex:
<table border=”1”>
<tr>
<td>Java</td>
<td>100</td>
</tr>
<tr>
<td>Selenium</td>
<td>300</td>
</tr>
</table>
HTML Tree tr
HTML
body td td
table
IDE java 100 DE
tbody
1. tr
1. td Java
2. td 100 Java 100
2. tr Selenium 300
1. td Selenium
2. td 300
Ex:
div
span div
div div
a a
samsung 13,999
IDE DE
X-Path:
//a[text()=’samsung’]/../../../div/div/a
Assignment
1. Write an X-Path to identify the status of testing present in type of work web table under
settings of actiTime application
X-Path:
2. Write an X-Path to identify set by default link for support type of work
X-Path: //a[text()=’support’]/../../td[5]/a
3. Write an X-Path to identify the price (rate) of engineering type of work
X-Path: //a[text()=’engineering’]/../../td[4]/span
4. Write an X-Path to identify to identify the stable version of ruby language present in client
bindings of selenium download page.
X-Path: //p[text()=’Ruby’]/../p[2]/a
OR
//p[text()=’Ruby’]/../p[contains(text(),’Stable:’]/a
5. Write an X-Path to identify API docx link for phyton language present in client binding of
selenium download page.
X-Path: //p[text()='Python']/../
p[4]/a[@href="https://seleniumhq.github.io/selenium/docs/api/py/index.html"]
Ex:
HTML
A
body
B
div C
a A D
a C
a D
When we use group index 1st it will execute the expression present inside the braces, in this
example 1st it will execute ‘//a’ & it will store matching element (A, B, C, D) links into X-Path
array where index starts from 1.
Then it will identify the matching element based on the index which is specified outside the
bracket. In this example it matches with 1st link “A”.
//a -> A,B,C,D
(//a)[1] -> A Here it matches with only 1st link
(//a) [2] -> B
(//a) [3] -> C
(//a) [4] -> D
(//a) [last()] -> D
//a[1] -> AC Here it matches with all the links having index 1
(//a[1]) [1] -> A
(//a[1]) [2] -> B
//a[2] -> BD
(//a[2]) [1] -> B
(//a[2]) [2] -> D
(//a)[1]|(//a)[last()] -> AD Here it matches with 1st & last link
//a[3]
No matches
(//a)[5]
Ex2:
HTML Code
<table border=”1”>
<tr>
<td> Selenium </td>
<td>100</td>
</tr>
<tr>
<td>Selenium</td>
<td>300</td>
</tr>
</table>
HTML Tree
HTML
body
Selenium 100
table
Selenium 300
tbody
1. tr
3. td Selenium
4. td 100
2. tr
5. td Selenium
6. td 300
Assignment
1. Write an X-Path to identify the price of “Roadster T-Shirt” (1st T-Shirt) present under men’s
section of myntra.com
(//h3[text()='Roadster'])[1]/../div/span[1]/span[1]
2. Write an X-Path to identify the price of “Libas Kurtas” present under women’s section of
myntra.com
(//h3[text()='Libas'])[1]/../div/span[1]/span[1]
3. Write an X-Path to identify the price of “H&M Jeans” present under kid’s section of
myntra.com
(//h3[text()='H&M'])[1]/../div/span[1]/span[1]
4. Write an X-Path to identify the price of “OnePlus 9r” (1st mobile) present in Amazon.in
(//span[contains(text(),'OnePlus 9R
5G')])[1]/../../../../div[3]/div[1]/div/div[1]/div[2]/a/span/span[1]
or
(//span[contains(text(),'OnePlus 9R 5G')])[1]/../../../..//span[@class=’a-offscreen’]
5. Write an X-Path to identify the price of “Mi Band 5” present in Flipkart.com
//div[contains(text(),'Mi')]/../../div[2]/div[1]/div[1]/div[1]
or
//div[contains(text(),'Mi')]/../..//div[@class=’_30jeq3_1_WHN1’]
1. id
2. name
3. link text
4. x-path
Clear()
Write a script to remove the text present in email text box of open source billing app?
Assignment
URL: https://demo.actitime.com
UN: admin
PW: manager
Hint: use SendKeys(“admin”) & SendKeys(“manager”) to enter the data into the field
isDisplayed()
Write a script to check whether Facebook logo is displayed or not?
is.Enabled()
Write a script to check login button of Facebook is enabled or not?
is.Selected()
Write a script to print the status of checkbox present in actiTime application
getSize()
Write a script to find the height & width of email text box present in Facebook
Output
Height= 52
Width= 364
NOTE
Assignment
Write a script to check whether height & width of user name & password text box are equal or not
of actiTime application
getLocation()
Write a script to check whether username & password text box are properly aligned or not.
Assignment
1. Write a script to check whether gender radio buttons are properly aligned or not present in
facebook.com after clicking create new account button [Hint: Use Thread.sleep() after clicking
the button]
NOTE:
By using submit() method. We can use submit() only if type=’submit’ attribute is present.
Keys()
Write a script to copy the text present in email text box and paste it into the password textbox
present in open-source billing app.
getText()
Write a script to print the text of forgot your password link present in actiTime application.
getTagName()
Write a script to check whether the tag name of the actiTime link is anchor tag(a) or not
getAttribute()
Write a script to print the URL of actiTime link
switchTo()
Syntax: obj_ref_switchTo().activeElement();
Write a script to find active element present in www.google.com and type java where the active
element is present.
driver.findElement(By.id(“id”)).sendKeys(“java”);
getCurrentURL()
Write a script to check whether www.seleniumhq.org link is navigating to www.selenium.dev or
not
getCSSValue()
Write a script to print the colour of forgotten password link present in Facebook
Assignment
Write a script to print font type & font size of forgotten password link present in Facebook
X-Path axes
X-Path axes are the special keywords of the x-path which has following syntax;
Syntax: /Axes_Name::tag_name
Some of the axes name used are:
1. ancestor
2. descendant
3. parent
4. child
5. following-sibling
6. preceding-sibling
Ex: /descendant::a -> //a
tr
1. td 1 java
Preceding-Sibling
2. td 2
3. td
4. td 1 Following-Sibling
selenium
5. td 2
1. a ide
2. a webdriver
Ex:
Ex:
HTML Tree
HTML
body
Java 100
table
Selenium 300
tbody
1. tr
1. td Java
2. td 100
2. tr
1. td Selenium
2. td 300
or
(//h3[text()=’Roadster’])[1]/parent::div//descendant::span[@class=’product-discountedPrice’]
Assignment
Syntax:
//tag_name[starts-with(text(),’value’)]
//tag_name[starts-with(@Attribute_Name,’Attribute_Value’)]
Ex:
<a..>Inbox(7)<a>
X-Path: //a[starts-with(text(),’Inbox’)]
By giving only “box” will not work here
Assignment
Write a script to select all the checkbox for any web application (URL should be taken from the
user) [Hint: //input[@type=’checkbox’]
Write a script to print all the content of the web table & take URL from the user. [Hint: use
//td]
Synchronization
The process of matching the selenium speed with the application is called as synchronization
Most of the time selenium is faster than the application, because of this reason we may not
expected result or we may get exceptions such as NoSuchElement Exception.
Implicit Wait
In selenium there are different ways to synchronize the script, on the of the frequently used option
used is implicit wait.
Flow Diagram
When the control comes to any findElement or findElements statement, it will check
whether any the element is present or not
If the element is present the findElement returns 1st element matching element, whereas
findElements returns all the matching elements.
If the specified element is not present then it will check for the time out
If the time is over findElement will throw NoSuchElement Exception, whereas findElements
returns empty list
If the time is not over it will wait for 500ms which is called as Pooling Period.
Then it will continue to check whether element is present or not
Ex:
Yes.
Explicit Wait
In order to handle the synchronization of any method we can use explicit wait.
WebDriver wait itself is called as explicit wait, because we have to specify the condition
explicitly
Syntax:
WebDriver driver=new WebDriver(driver,10);
wait.until(ExpectedConditions.titleIs(“Enter the title”);
Flow Diagram
When the control comes to wait.until statement it will check the specific condition
If the condition is true it will go to the next statement, if the condition is false it will check
for the time out
If time out is over, it will TimeOut Exception, else it will wait for 500ms (5s) and it will
continuous to check the condition
NOTE
NOTE
Can we handle the synchronization of the findElement method using explicit wait
Yes
TimeOut Exception