Selenium_Notes
Selenium_Notes
DRAWBACK/LIMITATION/DISADVANTAGE
-Initial investment is high.
-We require a skilled person.
The work of an automation engineer is to convert test cases to test scripts.
TESTSCRIPTS:
It is a code or program written against manual test cases.
Per day we can convert 2 to 3 test cases to test script.
----------------------------------------------------------------------------------------------------------------
Tools of automation d=difference
1) Selenium:
It is an automation tool used to test web and mobile application
ADVANTAGE:
1) Open source and free to download.
2) Supports multiple OS, Programming languages, and Browser
1|Page
Disadvantage:
1) Can’t automate standalone application
2) Can’t automate barcode, captcha, OTP, network error, audio, video,
etc.
3) We are not going to get the immediate support.
2) QTP (quick test professional):
Advantage:
Supports all types of applications d
Disadvantage:
1) It was paid software d
2) It only supports windows d
3) Only supports VB-Scripts (Visual Basics Scripts) d
1)SearchContext:
1) It is an interface
2) Used to search elements in the webpage
3) It has 2 abstract method
a) findElement() – In a web page if we have to find only a single element
we use this.
The Return type of findElement is webElement
b) findElements() – If we have to search more than one or multiple
elements we use this.
2|Page
The return type of findElements is List<webElement>
WHY?
1) Search the target webElement in the web page
2) Perform action on Targeted webElement
2)takesScreenshot:
1) It has only one abstract method
a. getScreenshotAs()
b. Ways to take screenshot
i. Webpage
ii. Web Element
WHY??
1) For better analysis of testScript failure we take SS.
2) Act as proof in a defect report.
3)javascriptExecutor:
It has 2 abstract method
1) executeScript()
2) executeAsyncScript()
WHY TO USE?
It is an interface user to perform the scrolling operation method horizontally and vertically
When the selenium method is not working that time we use the javascriptExecutor
Interface method.
4)WebDriver:
It is an interface which is going to control our browser.
It is an core interface in webDriverArchitecture
Method:
3|Page
1) get()
2) getTitle()
3) getCurrenturl()
4) getPageSource()
5) close()
6) quit()
7) manage()
8) navigate()
9) switchTo()
10) getWindowHandle()
11) getWindowHandles()
Date- 27/01/2024
regression testing:
process of testing the unchanged areas after modification of a feature to check if it iis
impacted or not
it is a repetitive process so the company switches to automated testing to reduce time and
effort
automation testing: process of testing sw with the help of tools ex: selenium, qtp:quick test
professional
4|Page
3. when you have to run same(repetitive) tasks again and again
test automation:
procedure to add tools to Java projects
>go to selenium.dev>downloads>download the the tool
> Paste the tool to the jar folder
> right click on the project and click build path to add a reference library
or
>go to svn repository
>search selenium
> Click on selenium java
> Click on the version to download
5|Page
Date: 27/Jan/2024
remoteWebDriver
>WebDriver is used to control the system
>RemoteWebDriver is used to control one system from another system
QUESTIONS
Q. Types of inheritance in selenium?
Q. Types of Java concepts read in selenium?
Q. Where are you going to use inheritance concepts in selenium?
HTML
1. paired tags
2. unpaired tags
LOCATOR/SELECTOR
https://letcode.in/test
>create account
>click on signup
6|Page
>enter name
>enter email
>enter password
>click on check box
> Click on signup
LOCATOR:
It is a search criteria that is used to search the web element in the web page.
8 types
all locators are static methods.
we have to select the locator according to the priority
TYPES OF LOCATORS/SELECTORS
================
5. linkText (best locator to find elements if the element is a link) used for anchor tag (WE
USE THIS WHEN WE HAVE LINK TAG)
------ linkText: text that is present in between the open and closing anchor tags is known as
linkText
If text is attached to your link is known as linktext.
It is case-sensitive.
------ CSS and Xpath are the best locators for syntax or expression.
Q. When do we go for the CSS locator?
ANS: When the web element does not have an id class or name and is not a link also then
we go for the CSS locator
8|Page
Q. How to identify the CSS selector
Ans: By using syntax: tagname[AttributeName = ‘AttributeValue’]
DATE- 29/01/2024
Q. How to see the src code of the webpage?
Ans: Right-click on the webpage > click on inspect.
DATE: 30/01/2024
- To slow down the speed of the loading process we use Thread.sleep(//time in ms) it will throw an
exception which we need to handle using throws
- Slow down the execution of the automation script.
Steps to perform when we have to perform more than one action at one time.
When we have to do more than 1 action we need to identify the element and store it inside a
variable.
9|Page
With the help of a variable call method
Date: 31/01/2024
XPATH
It is also known as XML path
IT provides the path or the address of the element
It is also used to exchange data between the client and the server.
It overcomes the drawback of CSS Selector.
There are 2 types of XPath
o Absolute XPath.
o Relative XPath. (It has 7 types.)
XPath by attribute
XPath by text
XPath by contains
XPath by group index
Independent and Dependent XPath.
XPath by axes
Correlated XPath
Relative XPath
We are going to write XPath from a child of the document it is known as Relative XPath
Keyword
1. . (dot) = means the current project
10 | P a g e
2. / = immediate child (Absolute XPath is represented by this)
3. // = any child (Relative XPath is represented by this)
4. [ ] = index value start form 1
Relative XPath:
1. Writing the XPath from Any Child of the document is known as Relative XPath
2. It is represented by ‘//’.
Why do we choose XPath?
1. When the element has only the text then we go for relative XPath
2. When the element is having a duplicate. To remove them we use XPath
a.
3. When the element is dynamic.(that can be changed)
a.
1. XPath by attribute
Ans: Identifying or searching the element on the web page based on the attribute
Syntax: //tagName[@AttributeName = ‘Attribute value’]
Operation to use at (findElements()): AND, OR, NOT
o When working with the concept of findElement we use these operations and can
only be used wirh xpath by attribute
We can remove duplicate using xpath by attribute with the help of xpath by group index
Example:
Code
11 | P a g e
Output
Operations
2. XPath by text
a. Search the element based on text.
Syntax: //tagName[text()=’textValue’].
Eg:
12 | P a g e
If the text has lengthy spaces (Non-breakable space present in the text)
When we have a partial dynamic text value (Eg: Men (35,673))
o Can be resolved by
Date: 02/02/2024
3. XPath by contains
a. When we have to enter partial attribute value and partial text value then we
use xpath by contains
b. When the text consist of partial text value.
Advantage
Support both attribute and text
Syntex for text: //tagName[contains(text(),’partialTextValue’)]
Partial value
Drawbacks/Disadvantage
When the element is completely dynamic. (Partially it can support).
o It can be overcome in independent and dependent xpath.
Syntax:
13 | P a g e
(xpath)[indexValue]
Q. Why?
Ans. When the web Element is completely dynamic that time we use independent and dependent text
path.
Independent means fixed element
Dependent means dynamic element.
Steps:
1. Identify which is fixed and dynamic
2. Write the xpath for the fixed element
3. Update the xpath by backward traversing (/..) to find a common parent for fixed and dynamic
element(we use /.. until we can get the same parent for both element or it cover both fixed and
dynamic element)
4. Update the xpath for dynamic web element .
14 | P a g e
EXAMPLE
1. (//div[@class='_4rR01T'])[1]
2. (//div[@class='_4rR01T'])[1]/../../
3. (//div[@class='_4rR01T'])[1]/../..//div[@class='_30jeq3 _1_WHN1']
Note: TO move from one tab to another tab we need switchTo concept
Date: 01/02/2024
- selenium.NoSuchElementException
-If internet is slow.
Solution: apply Thread.sleep(Time in ms)
-When the source code we wrote is invalid
Solution: compulsory check is it giving 1 of 1 or not
15 | P a g e
- selenium.InvalidSelectorException
-can also be named as InvalidLocatorException
-ClassName locator doesn’t allow multiple class names and spaces.
Solution: We can pass multiple spaces using cssSelector(“”).
- ElementClickInterceptedException
1. When the element is not able to perform the action
2. We get it when something is interrupted by the action in the middle or other element in
the web page
Solution:Apply the concept of wait: Pass the thread.sleep(time according to the
function)
Date: 03/02/2024
Absolute XPath
- Writing the XPath from the root of the document is known as absolute XPath.
- It is represented by ‘/’. (Immediate child)
- It is very lengthy.
- It consumes more time.
- We use it when the web element is not identified by the relative XPath then only we go for absolute
xpath
Example:
Here we need to take the path from the root element that is HTML tag/
16 | P a g e
Onlu value A
Only value B
Only value C
Only value D
17 | P a g e
- css locator=input[value='C']
- xpath by attribute=//input[@value='C']
- absolute path=html/body/div[2]/input[1]
to target the fourth txtbox>
- css locator=input[value='D']
- xpath by attribute=//input[@value='D']
- absolute path=html/body/div[2]/input[2]
target combination of AB
- absolute path=html/body/div[1]/input
target combination of CD
- absolute path=html/body/div[2]/input
target combination of AC
- absolute path= html/body/div/input[1]
target combination of AC
- absolute path= html/body/div/input[2]
target combination of ABCD
- html/body/div/input
target combination of AD
- html/body/div[1]/input[1]|html/body/div[2]/input[2]
target combination of BC
- html/body/div[1]/input[2]|html/body/div[2]/input[1]
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
18 | P a g e
ChromeDriver cd=new ChromeDriver();
//Launch the Flipkart
cd.get("https://www.flipkart.com/");
cd.manage().window().maximize();
Thread.sleep(2000);
WebElement a =
cd.findElement(By.xpath("html/body/div/div/div/div/div/div/div/div/div/
div/div/div/div/header/getdiv/div[2]/form/div/div/input"));
a.sendKeys("Shoes");
a.sendKeys(Keys.ENTER);
}
Drawback:
1. It is very lengthy
2. Consume more time
3. If any element is added or removed from the structure, it needs a maintenance effort. (Update)
19 | P a g e
Date: 05/02/2024
Type of interface
1) Search context: To search an Element in the WebPage then we use the interface called as
searchContext. It has 2 type
a. findElement();
i. returntype: WebElement()
1. To search single element in the webpage
2. Id and name are the best locator
3. Priority to ID
4. Exception: NoSuchElementException
b. findElements();
i. Return Typr: List<WebElement>
1. To search multiple element in the webpage we use findElements
2. Classand TagName are the best locator
3. Priority to TagName.
4. Exception: EmptyListWebElementException
Note: When we have text or linktext we should give the priority to text
2) WebDriver
- Also known as a core interface in selenium WebDriver architecture.
- It accesses the maximum number of methods. (Total 13)
- It is used to control the browser.
- It is also known as browser controlling methods.
- WebDriver interface have 11 abstract method.
1. get(String URL)
used to launch the application
It is compulsory to pass the main URL.
It is having infinite number of waiting. It won’t stop by itself we manually
have to stop if our internet is slow.
We have to enter complete URL.
2. getTitle()
To get title of current webpage.
Return type of this method is String.
20 | P a g e
3. getCurrentUrl()
Get the title of the current webpage.
4. getWindowHandle()
We use this method to get the current window ID. (d)
It will always give the id of the parent window (d)
It will only give single ID (d)
Return type is String. (d)
Why? We need it because if we want to transfer the control from one tab
to another tab we would require to know where our control is.
5. getWindowHandles() (Difference between windowHandle and window
handles)
To get multiple or all windows id’s
Return type is Set<String>
It will give multiple id
It give the id’s of both parent and child.
Different id every time the program runs
6. Close() (Difference between close() and quit())
Used to close the Tab/window.
It will close only one tab/window
It doesn’t stop the server
It will close the Parent Tab (By default the control will be in the parent
window.)
7. Quit()
It will close the tab/window
Will close all the tab/window
It will stop the server
It will close all parent and child Tab.
8. Manage():
Return type is ‘option’
We are going to handle:
Window related operation
maximize():
o To maximize our screen we use this method
Minimize():
o To minimize our screen we use this method
fullScreen():
o We can make our window fullScreen
getSize():
o Return type of this method is “dimension”
21 | P a g e
o We can get the height and width of window
o We need to import dimension from selenium package not
from awt package
o To get height and width we can use variable.getHeight() OR
refvariable.getWidth()
setSize():
o We can set the default height and width of window
o Return type is void.
o First we need to create object of the dimension
o
getPosition()
o To get the default x and y position our browser is going to
launch.
o Return type is Point
setPosition()
o We set the position of X and Y axis of the window
o We pass the value in pixel
o We need to import the package from selenium package.
o Return Type is void
o First we need to create an object of Point
o
Cookies related operation
Timeout related operation
9. Navigate():
Return type is navigation
We must use the Thread.sleep in navigation method
For the browser navigate method shows the direction
It provide browser history navigation(Provide direction to browser)
Used to pass Sub-URL
Methods()
Back()
o Backward direction
o Return type is void
Forward()
o Forward direction
22 | P a g e
o Return type is void
Refresh()
o Refresh the page
o Return type is void
To(String URL)
o To enter the sub url we use To(String URL)
o Return type is void
10. switchTo()
1. To do end-to-end testing we need switchTo()
2. To transfer the driver control from one window to another window
3. 1 parent and 1 child window
4. Default the driver control will be present in Parent window
5. When we have to work in child window-switchTo() is required.
5.
How to transfer driver control from child to parent window?
1. Use a forEach loop
2. Store inside a variable
3. Get a title of parent tab
23 | P a g e
4. Use if condition checking if it contains the parent tab
5. If it contains parent title put a break to end the loop
11. pageSource()
To get the source code of current page we use a method called
getPageSource()
Return type is String
WEB ELEMENTS
Action to be performed in WebElement
Why? To act on an element in the webPage we need to use webElement
What?
Ans: It is an interface, the element which is present on the viewPort area or visible
area
1. Search element in the webpage/Identify the element in the webpage
a. SearchContext (i): findElement()
b. Driver.findElement(By.locator()).click();
2. IT is having 14 abstract method
1. click()
i. to perform click operation
ii. Here we can get Exception as ElementClickInterceptedException
2. clear()
i. Clear the data in the web element (Example text fields)
3. sendKeys()
24 | P a g e
i. it can also perform clicking operation
ii. used to pass/perform two values/operation
1. To enter the data into text field
2. To perform some keyboard simulation
3. We use sendKeys to perform shortcut operation
Keys
It is an enum (enumeration)
IT is a java template.
It contains all the keyboard keys as the constant(That means it’s work can’t be
changed)
4. getText()
i. Return type of text is a string
ii. This method is used to get the text of an element
iii. IT can get both normal and link text
iv. It is used to verify the text of the targeted element
5. getAttribute()
i. It will return the value of the given attribute name.
ii. return type is string
iii. if the given attribute is doesn’t contain then it will return null.
6. getCssValue()
i. This method also return the attribute value for the given attribute name.
7. getTagName()
i. It gives you the tag name of any element.
8. isEnable()
i. Reurn type is boolean
ii. We use it to check whether our element is enabled or not
iii. If element is enable it gives True if not it gives the False value
iv. IF the element is disabled we can’t perform any action
1. It returns false in an disabled state.
v. IF the element is Enabled we can perform any action.
1. It return true in enabled state
Condition
2. It should have a tag-name as ‘Input’ or ‘Button’
3. If the tag name is something else we’ll get the false as output.
9. isSelected
25 | P a g e
i. Return type is boolean
ii. Used to check if the element is selected or not
iii. If the element is selected it returns true.
iv. If the element is not selected it return false.
v. Condition:
1. It is used to verify the radio button or check box
Note: If it’s used on some other element rather than check box or radio button the
default value or output will be false.
10.isDisplay()
i. Return type is boolean
ii. Used to check the targeted web element is visible on the webpage or
not. If yes it will return true if false it will return false.
11.submit()
i. it can also perform clicking operation
ii. It has some condition
1. It should have a tag name: button
2. It should have an attribute name: type=”submit”
3. It should present inside a form tag
12.getSize()
i. It return the height and width of the element
ii. Return type is dimension
13.getRect()
i. It is a combination of getSize() and getLocation()
ii. Its return type is rectangle
iii. It return heigt and width and X&Y value of the element
14.getLocation()
i. It gives the X and Y value of the element.
ii. Its return type is Point
//If we get captcha in the webpage to enter we must apply the Thread.sleep of 6-8 seconds and enter the captcha
manually
15.getScreenshotAs
SCREENSHOT
takesScreenshot
It is an interface
26 | P a g e
Used to take screenshot in selenium
It has only 1 abstract method
o getScreenshotAs()
It can be taken in two ways
o WebPage Screenshot
o WebElement Screenshot
Q. Why we take screenshot?
Ans: For the analysis of test script failure.
For adding it as a proof in test report.
ITestListener is the advanced testing tool used to take screenshot of failed testScript only it
ignore the pass test Script.
OutputType
It is an interface that contain 3 static variable which is used to specify the target ss for
getScreenshotAs(). The final variable are.
Screenshot can be taken in 3 ways
- FILE - file
- BYTES- Byte
- BASE64- String
By default screenshot extension will be png.
It will take screeshot of the visible page
Return type depends on the argument.
- If it’s FILE
STEPS TO TAKE SS
1. We need to downcast
2. We need to call a method takesScreenshotAs() and store it in the form of FILE.
3. Store the file in the variable as tempFile call absolutePath() to know the location of SS
4. Create the destination file.(When we have to store the screenshot in project)
a. Create a folder name images or anything
b. Create an object of FILE
c. In constructor pass the path of the file where we need to store.
i.
27 | P a g e
5. Copy the temp File class object given by temp file to the destination file using
copyFile. It is a static method (Move the file from tempFile to destinationFile)
a.
6. After execution refresh the project
To create a folder by automation we use apache common.io
If there are multiple pages with the same name getScreenshotAs will take a screensthot of
only last page. To overcome this we use a method of LocalDateTime. It captures the image
as on date and time.
WebElementScreenshot
It is also an interface
Steps:
1. Identify the element which you need to take the screenshot.
2. Call geScreenshotAs and store it in FILE form.
3. Create a destination folder and pass the path of the folder
Actions Class
1. It is a concrete class of selenium
2. It tells the concept to perform the action of mouse and keyboard
3. It has 21 method but these 6 are imp
a. Mouse Hover
b. Drag and drop
c. Right click
d. Double click
e. Key up
f. Key down
i.
b. Create the object class.
i.
28 | P a g e
c. While creating the object, Pass the WebDriver reference as an argument to the
constructor
i.
d. Call the appropriate action class non static method do the action
i.
e. Call the perform() method
i.
1) moveToElement(Webelement target): To perform mouse hovering on targeted
element
2) click(): To perform clicking operation on the entire webpage.
3) click(webElement Target): To perform clicking operation on the targeted element
4) contextClick(): To perform right-click operation anywhere on the webpage.
5) contextClick(webElement): To perform the rightClick operation on the targeted
element.
6) DoubleClick(): To perform double click operation on an entire webpage.
7) DoubleClick(WebElement target): To perform double click on a particular element
8) moveToElement(webElement ele, int xOffset, int yOffset): It will move the cursor from
the targeted element towards the left side of it and perform the action. And if we give
the negative value it will move to the right side.
9) moveByOffset(int xOffset, int yOffset): this medhod is used to move the mouse either
horizontally or vertically respected to given pixel value.
10) dragAndDrop: It will drag and drop tha particular element to the targeted
element
11) clickAndHold(): It will hold a random element in the webPage
12) clickAndHold(WebElement ele): it will click on particular element and hold. It
doesn’t click and move to next page
13) release(): To perform release operation on the webpage
14) release(webElement Targeted): to perform release operation on the targeted
element.
15) Perform(): To perform action on the webpage we use perform
16) sendKeys(CharSequence): It pass the value randomly on the webpage, used to
perform scrolling
17) sendKeys(webElement targeted, argument): it pass the value to the particular
element like enter the data, clicking, delete, backspace
18)
29 | P a g e
---------------------------------------------------------------------------------------------------------------
. Difference between click() and click(webElement ele) is that click is used to click on
the webpage whereas click(WebElement) is used to click on particular element.
. To move curser over any element we use moveToElement() after hovering
. DoubleClick()
. ---------------------------------------------------------------------------------------------------------------
. RightClick() aka ContextClick
. Difference between contextClick() and contextClick(webElement) is that contextClick
is used to click on randomly on the webpage whereas contextClick(WebElement) is used to
click on particular element.
SelectClass
. It is a class used to handle dropDown in selenium
. It is a concrete class in selenium package
. There are two types of drop-down. Single List and multi list.
30 | P a g e
. Using non static method of select class we can handle both the type of List box.
. It provides lot of selection and de-selection/with the help of select class we can select or
de-select the element in dropDown.
a.
2. Create the object of select class.
a.
3. Pass the targeteListBox as input to the select class constructor.
a.
4. Call the non static method of select class to perform the desired action.
a.
e.
3. SelectByVisibleText()
a. To select the elements in the dropdown on the basis of text.
31 | P a g e
b. It is a parametrized method that accept the <OPTION> tag text in the form of
string
c. Return type is void
d. Pass argument in the form of String
e.
4. SelectByValue()
a. It is used to select the element of the dropdown using the value attribute of
the option
b. Pass argument in the form of string
c. Return type is void
d. ./basicSearch/
GetOptionsOrangeHRM
5. selectByIndex()
a. It is used to select the element on the basis of index value
b. Index start from 0.
c. Return type is void
d. Pass argument in the form of string.
e.
6. deselectByIndex()
a. It will be used to deselect
b. We use this method when we know the number of the options present.
7. deselectByValue()
8. deselectByVisibleIndex()
9. deselectAll()
a. It will deselect all the element selected in the dropdown
10.Amazon>language>changecountry or region>selectIndia
Synchronisation:
- It is a process of matching two different event according to the requirement of the
system to the desired outcome.
- Application loading speed depend on the internet.
- Automation script execution speed and automation will always be faster.
32 | P a g e
- If application loading speed and automation script execution speed both should be
matched then we get the proper output.
- If application loading speed and automation script execution speed both speed are
not matching we get the noSuchElementException.
- TO slowdown the speed of execution we use delay(wait).
33 | P a g e
For YES CONDITION(When internet Is very fast)
1st cycle----findElement()------element is present---perform the
action
It will be applied in all the findElement() and findElements()
2. Explicit Wait
3. Fluent Wait
4. Thread.sleep():
Alternate name: bilnd wait coz it won’t leave the control to the next line
till the given time is completed. In this we aplly the wait in the form of
mliseconds
It is applicable only for current line
5. pageLoadTImeOut
34 | P a g e