0% found this document useful (0 votes)
19 views

Complete Selenium

Uploaded by

kartikkhairnar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Complete Selenium

Uploaded by

kartikkhairnar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

❖ Disadvantages of manual testing:

1. More human efforts are required.


2. Test cycle duration is increased.
3. Compatibility testing (Executing same test cases on different browser) is difficult.
4. Regression testing is also time consuming as we run same test cases after the build update
to check effect of newly added module on old module.

❖ What is Automation testing?


Automation testing is defined as testing the application features by using automation toll and
executing test script on that tool. The different types of automation tools are Selenium, ATP,
Selendroid, Appium etc. Automation testing tool is used to perform the testing but as a tester
we have to provide some commands to this tool and that commands are called as “Scripting.”

❖ Advantages of Automation testing:


1. Less human efforts are required.
2. Test cycle duration decreases.
3. Compatibility testing becomes easier. We can perform compatibility testing by changing 2-3
lines of out script/code.
4. Reusability of code is allowed.
5. Regression testing is also become easier as we can use same test script.
6. Project cost reduced due to automation.
7. It is reliable and efficient.

❖ Advantages of Selenium:
1. It is open source automation tool.
2. It supports multiples programming languages such as java, python, C sharp etc.
3. Cross browser testing is possible
4. Cross platform testing is also possible

❖ Disadvantages of Selenium:
1. We can’t automate desktop based application (ex. Team viewer, MS-office)
2. We can’t automate standalone applications (ex. Calculator).
3. We can’t automate captcha code using selenium.
4. We can’t read barcode using selenium tool.
5. It doesn’t support file uploading.
6. Ad-hoc testing (we know application but can’t have test data) can’t be performed.

❖ Different java concepts used in selenium:


1. Inheritance 7. Arrays
2. Polymorphism 8. Collections
3. Interface 9. Loops
4. Up-casting 10. Control statements
5. Abstraction 11. String class
6. Encapsulation
Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 1
❖ Flavors / Types of selenium:
1. Selenium IDE: IDE stands for Integrated Development Environment. In this version of
selenium we can’t perform compatibility testing. We can run our script only in Mozilla
Firefox browser. We have record and playback option in this IDE.
2. Selenium RC: This type of selenium can support compatibility testing. That means we can
run test scripts in different browser such as chrome, Firefox, internet explorer etc. but we
can use only Java programming language to write the script.
3. Selenium Webdriver: This type of selenium can support compatibility testing. That means
we can run test scripts in different browser such as chrome, Firefox, internet explorer etc. It
can support multiple programming languages such as java, python C sharp etc to write the
script. Currently we are using selenium tool having version 3.14.
4. Selenium grid
5. Selendroid

❖ Procedure to add selenium in your project:


1. Download selenium tool from website.
2. Download chrome browser driver. Unzip that file and store at download folder.
3. Create a new project in which you want to add selenium tool and give the name to it.
Example. Deca20_selenium.
4. Create a new class in that project. Example Class name Test1.
5. Then right click on project – click on build path – configure build path – libraries – add
external jar files – then select your selenium jar file (mostly present in download folder as
you download in step1) – click on open – finally click on apply and close.
6. You can check your selenium tool is successfully added to your project or not. Just click on
the project then reference libraries tab is present inside the project. It will ensure you that
you have successfully installed selenium tool in your project.

❖ Architecture of selenium:
1. Search Context: It is a super most interface in selenium. It consists of all abstract methods
and that methods are inherited to the Webdriver interface.

2. Webdriver: It is an interface present in selenium which consists of two types of abstract


methods that is abstract methods of search context and his own abstract methods.

3. Selenium Remote Webdriver: It a class which implements all the abstract methods of
both the interfaces that is search context and Webdriver. This implementation class is
extended to the different browsers such as chrome. Firefox, internet explorer etc.

4. Browser driver: For compatibility testing we need to use runtime polymorphism to


perform up casting in selenium. For example, to open a browser using script, we create an
object of chrome driver with reference of Webdriver interface.
WebDriver driver = new ChromeDriver ();

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 2
❖ Webdriver and its methods:
Webdriver is an interface which is used to perform different actions on browser such as open
browser, close browser, navigation on browser etc. to perform these actions Webdriver have
inbuilt methods and that methods are called as Webdriver Methods.
1. Get method: Get method is used to open an application or to provide the URL to browser. It
doesn’t return any value. It will just open the browser and connect to given URL. If we
provide wrong URL in get method then we will get Webdriver exception.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 3
2. Close Method: This method is used to close current tab of browser. Immediate opening
and closing of browser does not provide proper simulation then we have to add wait timer.
So that after opening of browser it will wait for some time and then it will close it. This wait
is given in milliseconds. To provide this wait time we have to use Thread class of java. In
thread class sleep method is present which provides the wait time. But if we enter wait
time then thread class generates the exception then to handle this we have to use throws
Interrupted Exception before closing the method.

3. Quit Method: This method is used to close all tab of the browser. This is an alternative to
close method. If we interrupt current running script manually then we get exception saying
that unreachable browser exception. So avoid manual interruption while script is running.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 4
4. Get title method: Each website has its own title. This method is used to get the title of
particular website. This method will returns string type of information. We can display this
string in our console. We can also compare the expected title with title received by using get
title. For this purpose we have to use string equals or equal ignore case function.

5. Get current URL: This function is used to get URL of current tab of browser. The return
type of this method is String information.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 5
6. Maximize: This method is used to maximize the browser. We can’t minimize the browser
using selenium tool but we can change the size of browser using set function.

7. Navigate: This method is used to perform various operations on browser such as move
forward, move backward and refresh the webpage. This method can be used as alternate
method to get method.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 6
8. Set Size: This function is used to set the size of browser as per given dimensions. We have
to pass dimension argument to set size function. But before that we have to create the
object of dimension class of selenium tool and we have to pass expected height and width of
browser as a parameter to that dimension class.

9. Set Position: This function is used to set the position of browser as per given parameter.
We have to pass point argument to set position function. But before that we have to create
the object of point class of selenium tool and we have to pass expected x and y coordinates
of new position of browser as a parameter to that point class.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 7
10. Get Size and Position: this method is used to get current size and position of browser
respectively.

❖ HTML Coding:
To perform action on browser element then we have to use Webdriver element, before that
some basic knowledge of HTML coding because generally all the webpage are written in HTML.
HTML stands for Hyper Text Markup Language. It is written in notepad or notepad++. HTML is
not case sensitive. While saving the file use extension as html. For Example, demo.html

Q1. Write a HTML code to create a dummy webpage.

Q2. Write a HTML code to create a dummy webpage and send message.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 8
Q3. Write a HTML code to create a dummy username field in webpage.

Q4. Write a HTML code to create a dummy signup module in webpage.

Q5. Write a HTML code to create a dummy list box in webpage.

To create a multi selectable list box we have to use following syntax.


<select multiple = ‘true’>

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 9
Q6. Write a HTML code to create a dummy web table in webpage.

Summary:
1. To create a new page we have to use “html” keyword.
2. Each keyword must be written in angular curly braces that is < Keyword >.
3. Every written keyword must be closed by following given syntax. < / Keyword>
4. To create an element or web component on webpage we have to use keyword (tag name)
as “input”.
5. To create a list box we have to use “select” keyword.
6. To create a link we have to use “a” as keyword with hyper reference to link.
7. To create a web table we have to use table keyword. Then we have to enter first row then
second and so on.
8. Tag name: Any keyword which present immediately after the angular brace (<) is called as
Tag name. For example, input, body, title, html etc.
9. Attribute: Any keyword present after the tag name with equal to symbol until the end of
angular bracket. Simply we can say that attribute name is present before the equal to
symbol and attribute value present after the equal to symbol. Syntax for attribute is :
Attribute Name = ‘Attribute Value’
10. Text: Any keyword which is present after the > angular bracket or within two angular
brackets. For example, > text <

❖ Locators:
Locators are mainly used to find the elements present on the webpage. To find the element
present on webpage we have to use find element method of Webdriver interface. This method
uses by class. This by class consists of various static methods. All the static methods present in
the by class are called as Locator types. All the locators takes String type input and there return
type is ‘by’ where as return type of find element method is web element.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 10
Types of Locator:
A. X Path
B. Id
C. Name
D. Class name
E. Tag name
F. CSS selector
G. Link test
H. Partial link test

A. Locator using X Path: X path is again divided into following sub types
a. X path by Attributes
b. X path by Text
c. X path by Contains
d. X path by Index
e. Absolute X path
f. Relative X path

a) X path by attributes: To find web element by using this method we have to follow following
steps.
1) Open browser using selenium tool.
2) Enter valid URL.
3) Give wait time so that we can easily differentiate the changes happening.
4) Once browser is open then move cursor on one particular web element such as username field.
5) Right click on it and select inspect option. Then html code of that page will open. To select
HTML code of particular web element we have to move cursor on that element then color of
code become blue.
6) Press control + F to open find tab. Then write your x path expression in that tab. To write x
path expression use following formula.
//tag name [ @ Attribute name = ‘Attribute value’]
7) Once you complete your x path expression then color of code changes from blue to yellow and
it shows 1 of 1. Hence we can conclude that we successfully find the element and make its
expression.
8) Then go to your code and after get methods write following method to find element using code.
Driver.findelement (By.xpath (“Provide your x path here”)).sendkey (“Your Username”)
9) Repeat step 5 to step 8 for each web element. To find a proper tag name use following
hierarchy. Follow the flow id / name/ class/ placeholder.
10) To click any button, use click function after x path expression.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 11
# Java code which is used to provide credentials to dummy webpage using X path by attributes

# Java code which is used to provide credentials to facebook login using X path by attributes:

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 12
b) X path by text: X path by test locator is used when developer create an element using tagname
and text only. When we can’t find attribute then we have to go with this type. We have to use
following formula to create X path expression. Rest of the process is same as per the previous
locator.
//tag name [text () = ‘text value’]
If any element present on webpage in terms of text with spaces then we have to mention that
spaces in HTML coding also. For this purpose use following syntax in html
<span> abc </span> and use formula as //span[text()=’ abc ‘]
These spaces are created by using two methods. One is by using Keyboard and another is non
breakable space.

# Java code which is used to click on link of dummy webpage using X path by text:

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 13
# Java code which is used to click on forgot password link of facebook using X path by text:

c) X path by contains: when the space created by non breakable method then we can’t use X path
by text. Hence we are going to use X path by contains. It has two types.
1. By using attributes: //tagname [contains(@Attribute name, ‘Attribute value’)]
2. By using Text: //tagname [contains(text(), ‘Text Value’)]
All remaining procedure will be same as per above examples/types.
# Fill details on dummy webpage using X path by Contains:

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 14
# Java code which is used to click on forgot password link of facebook using X path by contains:

d) X path by Index: While finding element we get multiple x paths. If we search for text type then
you might get multiple paths such as 1 of 1 or 1 of 3 etc. in such case we have to use X path
index.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 15
e) Absolute X path: To find the X path by using absolute method we need to find HTML tree
diagram first. In that diagram we have to move forward from parent to next immediate child.
We need to use single forward slash (/) in between parent and its immediate child. We have to
follow this approach until our expected element not found.
Disadvantages:
1) Identifying the element using HTML tree is difficult.
2) It’s a time lengthy and time consuming process.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 16
HTML Tree Diagram for above code:

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 17
f) Relative X path: To find the X path by using absolute method we need to find HTML tree
diagram first. In that diagram we have to move forward from parent to any child class. We need
to use double forward slash (//) in between parent and any child. We are using same example
an html tree diagram which is used in absolute X path.
Disadvantages:
1) Identifying the element using HTML tree is difficult.
2) It’s a time lengthy and time consuming process.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 18
B. Locator using Tagname: We can use tagname to find the locator using by class. But if our
code / webpage have multiple elements with same tagname then selenium perform action
on only first element. This is disadvantage of tagname. To avoid we have to go to next
locator that is ID. Following are the example of tagname as a locator for dummy webpage.

Fig: HTML Code to create Dummy webpage for tagname

Fig: Java Code to find element using tagname

C. Locator using ID: This method is used to find element when element has a attribute with
ID. We can’t use locator using ID in two cases. When ID is not present and when ID is
duplicates. That means two ID have same values then selenium perform action for first ID
only. Example of find element using ID is shown in figure given below:

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 19
Fig: Java Code to find element using ID

D. Locator using Class Name: Class name is used when ID is not present or ID has duplicate
values. We can’t use locator using class name in two cases. When class name is not present
or when class name has duplicate values. Example of find element using class name is
shown in figure given below:

Fig: HTML Code to create Dummy webpage for class name

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 20
E. Locator using Name: When class name is not present or when class name has duplicate
values then we are going to use locator by name. We can’t use locator by name when name
is not present or when name has duplicate values. Example of find element using name is
shown in figure given below:

Fig: HTML Code to create Dummy webpage for name

Fig: Java Code to find element using Name

F. Locator using Link Text: If tagname, ID, Class name, Name is not present or having some
duplicate values in html code then we should use Link text or partial link text method to
access the web element. It is mainly used to find the element having link and text present
on that link. We can’t use these methods for normal text codes. In link text we identify the
element by using entire text present on the link. Example of find element using link text is
shown in figure given below:

Fig: HTML Code to create Dummy webpage for Link text & partial link text
Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 21
Fig: Java Code to find element using Link text

G. Locator using Partial Link Text: If tagname, ID, Class name, Name is not present or having
some duplicate values in html code then we should use Link text or partial link text method
to access the web element. It is mainly used to find the element having link and text present
on that link. We can’t use these methods for normal text codes. In partial link text we
identify the element by using some part of text present on the link. Example of find element
using partial link text is shown in figure given below:

Fig: Java Code to find element using Partial Link text

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 22
❖ Web elements methods: It is an interface used to perform action on element present on
browser. If we want to perform multiple actions on same element then we can find that
element at once and store it in reference variable having data type as web element. It will
remove every time finding of same element. We just use that reference variable and perform
the desired action on it. Some important web element methods are given below.
1) Send keys (): This method is used to enter the value in the text field present on the
webpage. We have to pass string argument to this method. Following java code is an
example of this method.

2) Click (): This method is used to click on button links available on webpage. We also use click
option to select the radio button and check box. We are not passing any argument to this
method. Following java code is an example of this method.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 23
3) Clear (): This method is used to clear/remove the value in the text field present on the
webpage. We are not passing any argument to this method. Following java code is an
example of this method.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 24
4) Is enabled (): This method is used to check whether the element is enabled for operation or
not. We are not passing any argument to this method. This method will return Boolean
result in terms of true or false. If element is enable for action then this method will return
true and if element is disable for action then this method will return false. Following java
code is an example of this method.

5) Is Selected (): This method is used to check whether the element such as radio button or
check box is selected or not. We are not passing any argument to this method. This method
will return Boolean result in terms of true or false. If element is selected previously then
this method will return true and if element is not selected then this method will return
false. Following java code is an example of this method.

6) Is Displayed (): This method is used to check whether the element is displayed on the
webpage or not. We are not passing any argument to this method. This method will return
Boolean result in terms of true or false. If element is displayed then this method will return
true and if element is not displayed/present on webpage then this method will give
exception. To handle this exception we have to use try and catch block to handle risky code.
Following java code is an example of this method.
Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 25
7) Get Text (): This method is used to get text present on the web element on the webpage. We
are not passing any argument to this method. This method will return string information
present on web element. Following java code is an example of this method.

❖ List Box: To handle list box by using selenium tool we have to follow following steps:
Step 1: Identify the list box element on web page and store it in reference variable. Data type of
this variable is Web Element.
Step 2: Create an object of select class and give the reference variable of web element as an
argument to the select class.
Step 3: Use different select class methods to select your desired options from list box.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 26
Q1. Write a java script to enter date of birth in facebook sign up page.

Q2. Write a java script to identify the size of month field of list box of facebook sign up page.
Get option method is used to get all the options present in the list box as an output. Return type
of get option method is List <Web Elements>.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 27
Q3. Write a java script to display all the options of month field of facebook sign up page.

Q4. Write a test script to display all the options of month List box in alphabetical order on
facebook sign up page.
At first we have to identify the list box and store it in reference variable. Then create a new
object of select class and give reference variable as an argument to this select class. Use get options
method to get list of all web element present in that list box. To make it in alphabetical order we have
to collection. We can use any one collection from array list, vector, linked list, hash set, tree set. In our
first program we are going to use tree set because it store elements in ascending order. We can get the
elements from collection by using Iterator. We can also make descending order by using Iterator.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 28
Q5. Write a test script to display all the options of month List box in reverse alphabetical order
on facebook sign up page.

Q6. Write a test script to check month List box is multi selectable or not.
At first we have to identify the list box and store it in reference variable. Then create a new
object of select class and give reference variable as an argument to this select class. Use is multiple
function of select class to check whether given list box is single selectable or multi selectable. This
function will return Boolean result. If given list box is multi selectable then this function will return
true otherwise it will return false.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 29
Q7. Write a test script to get first selected option in day list box of facebook sign up page.
At first we have to identify the list box and store it in reference variable. Then create a new
object of select class and give reference variable as an argument to this select class. Then use function,
get first selected option of select class and store it in new reference variable. It will return web
element and finally print text using get text method of web element.

Q8. Write a test script to get all selected option in day list box of facebook sign up page.
At first we have to identify the list box and store it in reference variable. Then create a new
object of select class and give reference variable as an argument to this select class. Then use function,
get all selected option of select class and store it in new reference variable. It will return list of web
element and finally print text using for each loop and get text method of web element. It is mostly used
to get all selected options from multi selectable list box.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 30
Q9. Write a test script to deselect option in month list box of facebook sign up page.
In selenium list box we use following select class methods to deselect the option. But it is only
used for multi selectable list box.
1. Deselect by index
2. Deselect by value
3. Deselect by visible text
4. Deselect all
But we can’t use deselect option for single selectable list box. If we try to do this then we will
get exception Unsupported Operation Exception.

❖ Screenshot using script: To get a screenshot of our executed test case using web
driver we have to follow some steps:
1. Open browser and enter the URL in browser.
2. Type cast the driver object to the Takes Screenshot interface.
3. Use get Screenshot As function of take screenshot and pass argument as output Type .file to
this functions signature.
4. Store the result of above operation in one reference (source) having data type as file.
5. This variable consists of default path at which our screen shot is present (Source).
6. To copy the file from this location (Source) to another location (destination), we have to use
copy method of file handler class. Before that we have to store the path of destination folder
in dest variable having data type as file.

7. Screenshot name can’t change automatically. If we want to change its name randomly then
we have to use make function of Random String class. We have to pass integer value as
argument to this method. This method will return string information. We can concatenate
this string with our previously stored name. so that last two digits of screenshot name will
change randomly.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 31
❖ Parameterization: The process of fetching the data from external source and use this
data in selenium script, is called as parameterization. These external sources can be Excel
sheet, CSV file, Test NG data provider etc.
To fetch data from excel sheet we have to follow following steps:
Step 1: download Apache POI jar file and add these jar files in your current project. Follow the
same procedure which we follow while adding the selenium jar file. Right click on your project
name. Click on build path. Click on configure build path then new window will open. Click on
external jar and then add external jars. Navigate to your location and add all jars and click on
apply and close.
Step 2: Create a excel sheet and some data on that sheet. Save this sheet at any location in your
computer. Shift + Right click on that excel sheet name then you can copy it as a path.
Step 3: Create a new object of FileInputStream Class. Provide the path of excel sheet as an
argument to constructor of this class. Store it in a variable for ex, file.
Step4: Use create method of workbook factory class and provide above file variable as input.
This will open the excel sheet.
Step 5: To navigate on particular sheet use method get sheet(“Sheet Name”).
Step 6: to go to particular row use method getRow(int value).
Step 7: To go to particular column use method getCell(int value)
Step 8: To get string value present in sheet use method getStringCellValue().
Step 9: To get Numeric value present in sheet use method getNumericCellValue().
Step 10: To get Boolean value present in sheet use method getBooleanCellValue().

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 32
Q1. Write a test script to get string information from excel sheet.
To get String data present in one cell we have to crate an object of file input stram class and
provide sheet address as argument to it’s signature. Then open the sheet, goto particular sheet
number, row number and colomn number and then use get string cell value function. It will return
String type information and you can print on console.

Q2. Write a test script to get numeric information from excel sheet.
To get numeric data present in one cell we have to crate an object of file input stram class and
provide sheet address as argument to it’s signature. Then open the sheet, goto particular sheet
number, row number and colomn number and then use get numeric cell value function. It will return
double type information and you can print on console.

Q3. Write a test script to get boolean information from excel sheet.
To get boolean data present in one cell we have to crate an object of file input stram class and
provide sheet address as argument to it’s signature. Then open the sheet, goto particular sheet
number, row number and colomn number and then use get boolean cell value function. It will return
Boolean type information and you can print on console.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 33
Q4. Write a test script to get total no of rows having data from excel sheet.
To get total no of rows having data, follow all above procedure upto get sheet number. Then
use function get last row number function. This function will return the index which start from o.
hence to print total no of rows we need to add 1 in this result.

Q5. Write a test script to get total no of colomns having data from excel sheet.
To get no of clomns having data for one particular row, open sheet, navigate to sheet number,
then navigate to particular row number and then use gelt last cell value. This function directly return
the total no of colomns having data in that row.

Q6. Write a test script to print all elements present in row having same data type from excel.
Open sheet, goto sheet number, and save this in one variable sh having return type sheet. Then
find the size of colomn for that particular row. Use for loop and print the data. Here row number will
be fix, only colomn numbers are changing.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 34
Q7. Write a test script to print all elements present in colomn having same data type from excel
sheet.
Open sheet, goto sheet number, and save this in one variable sh having return type sheet. Then
find the size of rows for that particular colomn. Use for loop and print the data. Here row number will
be changing, colomn number is fixed.

Q8. Write a test script to print all data having same data type from excel sheet.
Open sheet, goto sheet number, and save this in one variable sh having return type sheet. Then
get size if last row number and give as condition for first for loop. In second for loop give condition as
last colomn number of that row. And just print the data.

Q9. Write a test script to print numeric data as String from excel sheet.
In excel sheet just add single inverted (‘) before the numeric value. Hence it will treated as text.
Follow same procedure as per program number 1

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 35
Q10. Write a test script to find which type of data is present in excel sheet using cell type.
Open sheet, goto sheet number, go to row number, goto colomn number and use function get
cell type. It will return cell type information. Just print it. You will get cell type.

Q11. Write a test script to print single cell data present in excel sheet using cell type.
Open sheet, goto sheet number, go to row number, goto colomn number and use function get
cell type. It will return cell type information. Compare it with string , numeric. Boolean type and print
accordingly.

Q12. Write a test script to print all cell data present in excel sheet using cell type.
Open sheet, goto sheet number, and save this in one variable sh having return type sheet. Then
get size if last row number and give as condition for first for loop. In second for loop give condition as
last colomn number of that row. Agains go to row number, goto colomn number and use function get
cell type. It will return cell type information. Compare it with string , numeric. Boolean type and print
accordingly.
Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 36
❖ I-Frame:
Displaying webpage as a part of another webpage is called as I-frame. I-frame will be created
by using tagname ‘iframe’.
Q. How to handle i-frame using selenium webdriver?
1. To handle i-frame using selenium webdriver, we have to change the focus of selenium from
main webpage to part of that webpage (to the i-frame). For this purpose we have to use
switch to frame function.
2. We can switch to frame by using three methods. By index, by id and by name. generally we
are not using by index method. to get name or id we have to find html tree diagram for that
frame. And pass that name or id as string in signatute of switch to frame function.
3. Then our focus moves from main page to that frame. We can perform action on the element
present on that i-frame.
4. Sometimes it is possible to have frame within frame. We need to follow same procedure.
5. If we want to go back to one frame only then use function switch to parent frame.
6. If we want to go directly to main / default page then use switch to default content function.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 37
❖ POPUP Handling:
Pop ups are small separate windows which open when we performs action on webpage. If we
can’t handle the popup we are not able to move forward. If we able to perform inspect on web element
then we can handle popup by using selenium but if we are not able to inspect web element then such
element can’t handled by using selenium. We have to use some indirect approaches such as use of
interfaces, auto IT toll and robot class. Types of popup are given below
1) Hidden – Division Popup: These popup had colorful look. Colors are present on different tabs.
We can inspect the elements present on this popup. As we can inspect the elements present on
popup, no need to change the focus of selenium. When we open flipkart website then after
loading page login popup will comes. This is an example of hidden division popup. Use
following URL to check it. https;//www.flipkart.com/

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 38
2) Alert Popup: We can’t inspect the elements present on alert popup. This popup consists of
fields such as ok or cancel button and text is also present on that popup. Sometimes it also
contains (?) or (!) symbols. Use following URL https://www.seleniumeasy.com/test/javascript-
alert-box-demo.html
To handle this popup follow given steps:
I. At first we need to switch the focus of selenium from the main page to the alert popup.
For this purpose use syntax: Alert alt = driver.switchTo.alert();
II. Alert is an interface which consists of three abstract methods. Accept, dismiss and get
text. Accept method is used to click on OK button, dismiss method is used to click on
CANCEL button whereas get text method is used to get text present on the alert popup.
Get text method will return string type of information.
Q1. Write a test script to click on ok button of alert popup generated by above mentioned
webpage.

Q2. Write a test script to get text present on popup mentioned by webpage.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 39
3) Child Browser popup / window popup: We can inspect element present on child browser
popup. But it is different from hidden division popup. These popup are generated in new tab or
new window. This is child window of our main page. This popup contains address bar,
maximize, minimize and close option as normal webpage. To handle these popup follow the
steps mentioned below: https://skpatro.github.io/demo/links/
I. At first we need to switch the focus of selenium from the main page to the alert popup.
For this purpose use syntax: Alert alt = driver.switchTo.windows(“String ID”);
II. To get ID of main webpage use get windows handle function. But we required ID of child
hence used getwindowhandles(). This will return set of ID’s.
III. As we know that index is absent in set collection hence we have to store these id in
arraylist and get respective id by using index of that webpage.
IV. Then focus will shift to child page and you can perform actions on element present on
that child webpage.
V. To move back to previous page we have to use ID of main page, then we can perform
action on main page.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 40
4) Authentication Popup: When we enter URL then it will ask for username and password. We
can’t handle it using selenium. We have to add different tool such as auto IT tool or use robot
class then we can handle it. https://chercher.tech/auth/
5) File Upload popup:
6) File Download popup:

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 41
❖ Multiple Links/elements using relative X path:
1. Get total no of link present on webpage: Open the browser, enter URL. To perform action
on multiple elements use findelements(). This method will return list of web elements.
When we find links present of facebook page using inspect option then we get multiple
matching x paths. Hence here we have to use relative X path. Tagname for link is a (use x
path as //a). Store this information in one variable ‘element’ & use function size with
variable name ‘element’ to get total number of links present on FB page and print it.

2. Get text present on all links present on webpage: follow all above procedure. Use for
each loop and use get text function in sop. Sometimes if text is not present then it will
display blank space or in any other language then shows question marks.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 42
3. Select all check boxes present on webpage: Identify the checkbox – use find elements
method to get list of web element. Use function size to get the size of checkbox. Use for each
loop and just click on that element.

4. Select all check boxes in reverse present on webpage: Identify the checkbox – use find
elements method to get list of web element. Use function size to get the size of checkbox.
Use for loop in decrement manner and just click on that element.

5. Select all check boxes in reverse present on webpage: Identify the checkbox – use find
elements method to get list of web element. Use function size to get the size of checkbox.
Use for loop in decrement manner and just click on that element and then use another for
each loop to click in ascending order. Click on each element.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 43
❖ WEB TABLES:
Program 1: How to get total number of rows present in given table using absolute X path.
To get total number of rows present in the table, we need to find table by using one of
its attribute. Then go to table body and then go to table rows using single slash (/). For this
purpose use absolute X path and find elements method. This method will return the list of web
elements. Store it in one variable and use size method of collection with the variable to get the
size of rows.

Program 2: How to get total number of rows present in given table using relative X path?
To get total number of rows present in the table, we need to find table by using one of
its attribute. Then go directly to table rows using double slash (//). For this purpose use
relative X path and find elements method. This method will return the list of web elements.
Store it in one variable and use size method of collection with the variable to get the size of
rows.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 44
Program 3: How to get total number of columns present in header of given table?
To get total number of columns present in header of the table, we need to find table by using
one of its attribute. Then go directly to table rows using double slash (//).Then use header row
number as 1 and then header tag name as th. For this purpose use relative X path and find elements
method. This method will return the list of web elements. Store it in one variable and use size method
of collection with the variable to get the size of rows.

Program 4: How to get total number of columns present in one row of given table?
To get total number of columns present in one of the row of the table, we need to find table by
using one of its attribute. Then go directly to table rows using double slash (//).Then use header row
number as 2 (because 1 is used for header) and then use tag name as td. For this purpose use relative
X path and find elements method. This method will return the list of web elements. Store it in one
variable and use size method of collection with the variable to get the size of rows.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 45
Program 5: How to print all data present in header/first row of given table?
To print the data of all columns present in header of the table, we need to find table by using
one of its attribute. Then go directly to table rows using double slash (//).Then use header row
number as 1 and then header tag name as th. For this purpose use relative X path and find elements
method. This method will return the list of web elements. Store it in one variable and use for each loop
and get text using web element method.

Program 6: How to print data present in first column of given table?


To print the data present in first columns of the table, we need to find table by using one of its
attribute. Then go directly to table rows using double slash (//). Then first select element using
header x path and get it’s text using web element method. Now use row as tr and tag name as td in X
path. For this purpose use relative X path and find elements method. This method will return the list
of web elements. Store it in one variable. Use for each loop and get text using web element method.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 46
Program 7: How to print all data present in given table?
First you need to find the number of rows present in the given table (refer program no.1) and
use it in outer for loop. Then declare on variable for column size. If row no is 1 then use x path for
header (refer program no.3) and get column size & store in column variable. Else use x path for data
field (refer program no.4) and get column size & store in column variable. Use this column size in
inner for loop. Declare on string variable as ‘text’. Again if row number is 1 then use x path for header
(refer program no.5) and get text using web element method & store in ‘text’ variable. Else use x path
for data field (refer program no.6) and get text using web element method & store in ‘text’ variable.
Finally print the results.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 47
❖ Auto Suggestions:
When we try to find anything on Google then it provide the list of suggestions. If we
search redmi on Google search then it will display the list of redmi mobile. But this list does
not have fix sequence. It will changes dynamically and depends on the how many times you
search for that particular item.
Program 1: How to print text from all suggestion in Google search?
At first open browser and go to Google search. Provide some wait time and inspect Google
search box using class value as a X path by attribute. Use sendkeys function with string argument as
“redmi”. Again provide some wait time. If we want to select redmi note 9 options then right click on
that suggestion and inspect it. Move to its parent and then use X path using class name then you will
get multiple matching so use find elements method. Save it in variable. Use for each loop to print the
text of all web elements.

Program 2: How to select one text from all suggestion in Google search?
First store your expected text in one string. Then get actual text using same method as we use
in above program 1. Then use for each loop, in that use if condition as actual text equal to expected
text then click on that option using click method and after clicking break that loop.

Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 48
Dipak Jagadale, Roll no. 19, DecA20 batch, VCTC, Pune. Page 49

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy