0% found this document useful (0 votes)
6 views56 pages

Selenium With Java - Day1

Uploaded by

anask6281
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)
6 views56 pages

Selenium With Java - Day1

Uploaded by

anask6281
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/ 56

Selenium with Java

📘 Introduction to Selenium
🔹 What is Selenium?
●​ Selenium is an open-source automation testing tool used for testing web applications.
●​ It allows automation of browser actions (clicks, typing, navigation, validations, etc.).
●​ It supports multiple programming languages (Java, Python, C#, Ruby, JavaScript).
●​ Works across different browsers (Chrome, Firefox, Edge, Safari) and operating
systems.

✅ Selenium is mainly used for functional testing of web applications.

🔹 Components of Selenium
1.​ Selenium IDE – Record & playback tool (basic, not used much in real projects).
2.​ Selenium RC (deprecated) – Older version, replaced by WebDriver.
3.​ Selenium WebDriver – Core part of Selenium, used to interact directly with browsers.
4.​ Selenium Grid – Used for parallel execution across multiple machines/browsers.

🔹 What is WebDriver?
●​ WebDriver is a browser automation API.
●​ It sends commands to the browser and retrieves results.
●​ It uses browser-specific drivers (e.g., ChromeDriver, GeckoDriver).
●​ Unlike Selenium IDE, WebDriver interacts with the browser like a real user.

🔹 What is a Driver?
●​ A driver is a bridge between Selenium WebDriver and the browser.
●​ Each browser needs its own driver:
○​ Chrome → chromedriver.exe
○​ Firefox → geckodriver.exe
○​ Edge → msedgedriver.exe
●​ The driver translates Selenium code into browser-specific commands.

🔹 Selenium Architecture (Simple Flow)


1.​ Test Script (your code)​
​ ​ ↓
2.​ Selenium WebDriver API​
​ ​ ↓
3.​ Browser Driver (e.g., ChromeDriver)​
​ ​ ↓
4.​ Actual Browser (Chrome, Firefox, etc.) executes actions

🔹 Why Selenium? (The Problem It Solves)


●​ Earlier, web testing was done manually (time-consuming, error-prone).
●​ Some early automation tools like QTP/UFT were paid and platform-dependent.
●​ Selenium came in as:
○​ Free & Open-Source
○​ Cross-Browser (Chrome, Firefox, Edge, Safari, Opera)
○​ Cross-Platform (Windows, Mac, Linux)
○​ Language Independent (supports Java, Python, C#, Ruby, JavaScript, Kotlin).

👉 This helps students see Selenium as the de facto standard for browser automation.

🔹 Where Selenium Fits in Testing


●​ Selenium is mainly used for Functional Testing of Web Applications.
●​ It does not:
○​ Automate desktop apps (use tools like AutoIt, WinAppDriver for that).
○​ Automate mobile apps directly (need Appium).
○​ Do performance testing (JMeter is used).
●​ It is best for:
○​ Regression Testing
○​ Cross-browser Testing
○​ Smoke/Sanity Testing

👉 This avoids the misconception that Selenium is a “one-stop” tool for all testing.

🔹 Selenium Ecosystem
Explain that Selenium is not just WebDriver:

●​ Selenium IDE – A browser plugin for recording & playback (good for beginners).
●​ Selenium RC (Deprecated) – Old tool replaced by WebDriver.
●​ Selenium WebDriver – Core API for automating browsers (most widely used).
●​ Selenium Grid – Used for parallel execution across multiple machines/browsers.

👉 Students will then know that WebDriver is a part of Selenium, not Selenium itself.

🔹 Real-World Use Cases


Give a couple of industry examples:

●​ E-commerce site regression suite (Amazon, Flipkart).


●​ Banking/finance login and transactions testing.
●​ Social media UI workflow automation.

👉 Makes it easier to connect Selenium learning to real work.

🔹 Limitations of Selenium (Reality Check)


●​ No built-in reporting (need TestNG, ExtentReports).
●​ Cannot automate CAPTCHA, OTP (need external solutions).
●​ Slower execution compared to headless tools (like Cypress, Playwright).
●​ Maintenance overhead (locators break if UI changes).

👉 Students will appreciate Selenium better if they know where it shines and where it
struggles.

🔹 How to Setup Selenium in Windows (Step by Step)


Step 1: Install Java
●​ Download JDK from Oracle website.
●​ Set Environment Variables:
○​ JAVA_HOME
○​ Add bin path in PATH.

Step 2: Install Eclipse IDE


●​ Download Eclipse IDE for Java Developers.
●​ Extract & launch Eclipse.
Step 3: Download Selenium WebDriver JARs
●​ Go to Selenium Downloads Page.
●​ Download Selenium Client & WebDriver Language Bindings (for Java).
●​ Add JARs to Eclipse project:
○​ Right click project → Properties → Java Build Path → Add External JARs.

Step 4: Download Browser Driver


●​ Chrome → ChromeDriver
●​ Firefox → GeckoDriver
●​ Edge → EdgeDriver

Place the driver .exe in a known folder (e.g., C:\drivers).

🔹 First Selenium Program (Java + Eclipse)


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstSeleniumTest {


public static void main(String[] args) {
// Set driver path
System.setProperty("webdriver.chrome.driver",
"C:\\drivers\\chromedriver.exe");

// Launch Chrome browser


WebDriver driver = new ChromeDriver();

// Open website
driver.get("https://www.google.com");

// Print page title


System.out.println("Page Title: " + driver.getTitle());

// Close browser
driver.quit();
}
}

🔹 Key Notes
●​ Always match driver version with your browser version.
●​ Selenium works only for web applications (not desktop or mobile directly).
●​ For advanced automation, Selenium can be integrated with:
○​ TestNG / JUnit (for test management)
○​ Maven / Gradle (for dependency management)
○​ Jenkins (for CI/CD automation)

Locators in Selenium
🔹 Introduction
●​ We can identify various elements in a webpage using locators.
●​ Locator: An address that uniquely identifies a web element within a page.
●​ Types of locators can be defined as:
○​ Directly found in DOM
○​ Customized locators (when not directly found)

🔹 Types of Locators
1.​ ID
2.​ Name
3.​ LinkText
4.​ PartialLinkText
5.​ ClassName
6.​ TagName
7.​ CSS Selector
○​ Tag and ID
○​ Tag and class
○​ Tag and attribute
○​ Tag, class, and attribute
8.​ XPath
○​ Absolute XPath
○​ Relative XPath

🔹 Locator Notes
●​ Sometimes, we may use a locator and it might not work, maybe because the attribute of
the locator keeps on changing.
●​ In such cases, we can use XPath or CSS Selector to locate elements.
●​ Hence, Selenium provides multiple locator options.
🔹 Finding Elements
●​ We can search for a locator of any element by moving on a webpage and inspecting its
elements.
●​ To locate an element using locators, we use:

driver.findElement(By.id("value"))

🔹 findElement
●​ Used when we want to interact with a single web element.
●​ Return type of findElement → a single WebElement.

🔹 Characteristics of a Good Locator


1.​ Unique → Should uniquely identify the element.
2.​ Descriptive → Should clearly describe the element.
3.​ Resilient → Should not break when HTML page attributes change.
4.​ Shorter in Length → Short and meaningful locator is better.

1. ID Locator
●​ ID is the fastest locator to search for an element.
●​ Each element has a unique ID attribute (if available).
●​ Syntax to interact with any element using ID locator:

driver.findElement(By.id("value"));

2. Name Locator
●​ Uses the name attribute value of any element to interact with it.
●​ Syntax to interact with any element using Name locator:

driver.findElement(By.name("value"));

3. LinkText Locator
●​ Used to locate links on a webpage.
●​ Works only for <a> (anchor) tag.
●​ Example:

<a href="www.approach.com">Tablet</a>

●​ Points:
○​ LinkText is available only for links (<a> tag).
○​ LinkText matches the visible text inside the link.
○​ If <a> has inner text, it is LinkText, else it is innerText.
●​ Syntax:

driver.findElement(By.linkText("Tablet")).click();

●​ We can also perform a click() operation directly on LinkText:

driver.findElement(By.linkText("textValue")).click();

4. Partial LinkText Locator


●​ Works same as LinkText Locator, but we don’t need to pass the complete link text
value.
●​ Instead, we can simply pass a partial link text value.

Syntax:

driver.findElement(By.partialLinkText("value"));

Example:

●​ Link Text Value = "Tablet"

driver.findElement(By.partialLinkText("Tab"));

Notes:

●​ Partial LinkText is not preferred because partial values may locate some
different/incorrect elements as well.
●​ It is preferred only when there is something common in 2 or more link texts.

5. TagName Locator
●​ TagName and ClassName locators are used when we want to locate a group of
elements.
●​ They are not preferred for locating a single element.
●​ TagName is used when we want to locate all elements of a specific tag, like:
○​ <input>
○​ <button>
○​ <img>
○​ <a>, etc.

Syntax:

driver.findElement(By.tagName("value"));

●​ We use findElements() method for TagName because it returns multiple elements.


●​ For single elements → findElement() is used.

Example:

List<WebElement> elements = driver.findElements(By.tagName("value"));

●​ findElements() returns multiple elements, and to store them we have to go with


either:
○​ List collection
○​ Set collection

6. ClassName Locator
●​ ClassName = a category of elements.
●​ The class attribute is mainly used for:
○​ Buttons
○​ Checkboxes
○​ Radio buttons
○​ Dropdowns, etc.
●​ Same category of elements share the same class attribute.
●​ Hence, ClassName is used when we want to get a group of web elements.

Syntax:

driver.findElements(By.className("value"));

●​ We use findElements() for ClassName because it may return multiple elements.


●​ For single elements → findElement() is used.

Example (storing multiple elements):

List<WebElement> elements =
driver.findElements(By.className("value"));

Important:

●​ The return type of findElements() = List of WebElements.


●​ List is preferred because it allows duplicates.

🔹 Assignments
1.​ Go to http://www.demo1sage.com/links.html​
(i) Find total number of links.​
(ii) Find total number of images.​
(iii) Click on any product link using LinkText / PartialLinkText.

⭐ CSS Selector Locator


●​ CSS Selector is called a customized locator because it is not directly available in
HTML; we have to build it.
●​ There are many types of selectors, and most of them are in combination with
tagname:
1.​ Tagname and ID
2.​ Tagname and class
3.​ Tagname and attribute
4.​ Tagname, class and attribute
5.​ Tagname and class

🔹 Question: If we already have ID and Class, why do we need CSS selectors with other

👉
locators?​
Because for complex webpages, locating elements with a single locator might not be
possible. Hence, we need to combine it with more than one tag/attribute.

(i) Tagname and ID – CSS Selector

Syntax: driver.findElement(By.cssSelector("tagname#id"));

(ii) Tagname and Class – CSS Selector

Syntax: driver.findElement(By.cssSelector("tagname.classname"));

(iii) Tagname and Attribute – CSS Selector


Syntax:

driver.findElement(By.cssSelector("tagname[attribute='value']"));

Example: input[type='text'] or any other attribute.

(iv) Tagname, Class and Attribute – CSS Selector


Syntax:
driver.findElement(By.cssSelector("tagname.classname[attribute='value'
]"));

📌 Assignment
1.​ Go to: “demowebsite.com”
2.​ Try to work out all types of locators to identify elements by using all the combinations.

⭐ XPath Locator:
●​ XPath Locator is the most important locator and is preferred to use.
●​ 80–90% of the time it is used to locate elements due to its accuracy.
●​ XPath is derived from XML Path.
●​ XPath is an address of an element.
●​ XPath can be captured from the DOM (HTML).
●​ DOM = Document Object Model → generated at runtime when we write a webpage.

👉 XPath for an element will only be captured when the DOM is fully loaded. If the DOM is not
loaded, we cannot find the element with correct XPath.

Types of XPath:
1.​ Absolute XPath (Full XPath)
2.​ Relative XPath (Partial XPath)

We can capture the XPath of any element directly from the webpage by right-clicking the
element and copying Full XPath / Relative XPath.

Absolute XPath:

●​ Starts with / → it represents the root node.


●​ Does not use attributes.
●​ Traverses through each node until it finds the element.

Example: /html/body/header/div/div/div[2]/div/input

Relative XPath:

●​ Starts with // → can search anywhere in the DOM.


●​ Uses attributes to locate the element directly.

Example: //*[@name='search']

Comparison: Absolute vs Relative XPath


1.​ Absolute XPath is faster than Relative XPath.
2.​ Relative XPath is slower than Absolute XPath.
3.​ Absolute XPath is long and breaks easily if structure changes.
4.​ Relative XPath is short, reliable, and widely used.

⭐ More Details
●​ Most of the time Relative XPath is preferred because:
1.​ The developer might add/remove elements between nodes → this will break
Absolute XPath.
2.​ Relative XPath does not traverse the full DOM, it directly identifies elements
using attributes.
3.​ Absolute XPath makes code lengthy & unreadable, while Relative XPath is
short and clean.

⭐ Absolute XPath (Deep Dive)


1.​ Absolute XPath can be generated automatically using tools like Firepath, DevTools.
2.​ It can also be generated manually by following the hierarchy of elements from the root
<html> tag.

Example: /html/body/header/div/div/div[2]/div/input

●​ Absolute XPath is also known as Full XPath.


●​ It always starts with the root of the HTML page.
●​ Not advisable in most cases because:
○​ Lengthy & non-readable.
○​ Breaks if minor structural changes happen in DOM.

👉 Use Absolute XPath only when Relative XPath cannot be generated.


Relative XPath
1.​ Relative XPath can be generated automatically via automation tools like Firepath,
SelectorHub, etc.

Syntax (Single Attribute): //tagname[@attribute='value']


Example: //img[@title='MacBook']

Syntax (Multiple Attributes):​


//tagname[@attribute1='value1'][@attribute2='value2']

Example: //input[@name='search'][@placeholder='Search']

Creating XPath Using AND & OR Operators


Using AND Operator:
//tagname[@attribute1='value1' and @attribute2='value2']

Example: //input[@name='search' and @placeholder='Search']

Using OR Operator:
//tagname[@attribute1='value1' or @attribute2='value2']

Example: //input[@name='search' or @placeholder='Search']

Creating XPath Using Inner Text


1.​ Use when we want to capture XPath of an element without using its attribute but
instead with its inner text.​

Inner Text → Text between opening and closing tags.​


Example: <input>InputField</input>

To locate with inner text, use text() method: //tagname[text()='innerTextValue']

Example: //a[text()='Desktop']

2.​ LinkText are also InnerText, but not vice-versa.


3.​ This XPath method works only when inner text is available.
4.​ text() method can only be used when interacting via InnerText.

Creating XPath with contains() Method

1.​ The contains() method is like text() but it allows partial matches of attribute values.

Syntax: //tagname[contains(@attribute,'partialValue')]

Example: //input[contains(@placeholder,'Sea')]

Can also be used with text() as well: //tagname[contains(text(),'partialText')]

Creating XPath with starts-with() Method

1.​ Similar to contains() but checks only the starting value of attribute.

Syntax: //tagname[starts-with(@attribute,'startingValue')]

Example: //input[starts-with(@placeholder,'sea')]

Difference between contains() and starts-with()

●​ contains() → Value can be anywhere in the string.


●​ starts-with() → Value must be at the beginning of the string.

Dynamic Elements
●​ Dynamic elements are those whose attribute values change after some actions (page
refresh, loading, re-rendering, etc).
●​ To handle them, we use contains() and starts-with() methods.

Handling Dynamic Elements


Scenario 1
Suppose there is a button on a webpage having an id attribute.​
Initially, the id attribute value is "start", but as soon as we click on it, the id value changes to
"stop".​
This shows that the id attribute value is dynamically changing based on certain actions.
👉 To successfully handle such dynamic elements, we have 3 approaches:
Approach 1: Using or operator
//input[@id='start' or @id='stop']

Approach 2: Using contains() method


//input[contains(@id,'st')]

Approach 3: Using starts-with() method


//input[starts-with(@id,'st')]

Scenario 2

name = xyz001 → xyz002 → xyz003 → … → xyz001…

👉 Approach:
//name[contains(@name,'xyz001')]

Scenario 3

name = 1xyz → 2xyz → 3xyz → … → nxyz

👉 Approach:
//name[contains(@name,'xyz')]

Scenario 4

name = 101xyz → 201xyz → 301xyz

👉 Approach:
//name[contains(@name,'01')]

Chained XPath
(i) Scenario:
When we do not have any attribute or inner text for an element, we use Chained XPath.

●​ Chained XPath = Combination of Absolute XPath and Relative XPath.

(ii) Explanation:
●​ Relative XPath comes into picture when the element we want to interact with has no
attribute or inner text.
●​ Then, we move to its parent node.
●​ If the parent also doesn’t have any attribute or text, we move further up until we find an
element with attribute/text.

Example: //img[@id='logo']/../img

Here:

●​ //img[@id='logo'] → Relative XPath


●​ /../img → Absolute XPath

XPath Axes
Node Tree Structure
●​ 6 is the focus node.
●​ 7, 9, 10 are the child nodes of node 6.
●​ 1, 4 are the ancestor nodes of node 6.
●​ 2, 3 are the following-siblings of node 6.
●​ 5 is the preceding-sibling of node 6.
●​ 8 is the descendant of node 7.

Key Points

●​ With XPath axes, navigating from one element to another is easy.


●​ Nodes represent elements.
●​ Based on the condition, we decide which relative node to interact with to access
elements.

Types of XPath Axes

1) self:
●​ Current node is the self node.
●​ Syntax: //tagname[@attribute='value']/self::tagname
●​ Example: //a[contains(text(),'India Tourism De')]/self::a

2) child:

●​ Immediate bottom node of the self node is the child node.


●​ Traverses all child elements of the current HTML tag.
●​ Syntax: //tagname[@attribute='value']/child::tagname
●​ Example: //a[@attribute='value']/child::tagname

3) parent:

●​ Immediate top node of the self node is the parent node.


●​ Traverses parent element of the current HTML tag.
●​ Syntax: //tagname[@attribute='value']/parent::tagname
●​ Example: //a[contains(text(),'India Tourism De')]/parent::td

4) Following
●​ Parallel node on the right side of the parent node is following node.
●​ Traverses all the elements that come after the current tag.

Syntax:

//tag[@attribute='value']/following::tagname

Example:

//a[contains(text(), "India Tourism De")]/ancestor::tr[1]/following::*

5) Preceding
●​ Parallel node on the left side of the parent node is preceding node.
●​ Traverses all the elements that come before the current tag.

Syntax:
//tag[@attribute='value']/preceding::tagname

Example:

//a[contains(text(), "India Tourism De")]/ancestor::tr[1]/preceding::*

6) Following-sibling
●​ Parallel node on the right side of the self node is following-sibling node.
●​ Traverses from current HTML tag to next sibling HTML tag.

Syntax:

//tagname[@attribute='value']/following-sibling::siblingtag[@attribute
='value']

Example:

//a[contains(text(), "India Tourism


De")]/ancestor::tr[1]/following-sibling::tr

7) Preceding-sibling
●​ Parallel node on the left side of the self node is preceding-sibling node.
●​ Traverses from current HTML tag to previous sibling HTML tag.

Syntax:

//tagname[@attribute='value']/preceding-sibling::previous-siblingtag[@
attribute='value']

Example:

//a[contains(text(), "India Tourism


De")]/ancestor::tr[1]/preceding-sibling::tr

8) Ancestor
●​ Parent of parent node is the ancestor node.
●​ Traverses all the ancestor elements (grandparent, parent, etc.) of the current HTML
tag.

Syntax:

//tagname[@attribute='value']/ancestor::tagname

Examples:

1.​ Moving to ancestor:

//a[contains(text(), "India Tourism De")]/ancestor::tr

2.​ If there is no descendant of self, then it can go to ancestor and then go to its child:

//a[contains(text(), "India Tourism De")]/ancestor::tr/child::td

9) Descendant
●​ Child of the child node is the descendant node.
●​ Traverses all the descendant elements (child → grandchild → etc.) of the current
HTML tag.

Syntax:

//tagname[@attribute='value']/descendant::tagname

Examples:

1.​ Moving to descendant of node:

//a[contains(text(), "India Tourism De")]/descendant::tagname

2.​ If there is no descendant of self, then it can go to ancestor and then go to its
descendant:

//a[contains(text(), "India Tourism


De")]/ancestor::tr[1]/descendant::*

WebDriver Methods
WebElement
●​ WebElement is an element on the webpage.
●​ It can be a checkbox, textbox, dropdown, button, etc.
●​ Whatever we see on the webpage is implemented as a WebElement.

5 Common WebDriver Methods (Basic)

1.​ get methods


2.​ conditional methods
3.​ browser methods
4.​ navigation methods
5.​ wait methods

Get Methods

Get methods work on web pages. We can access get methods through WebDriver instance.

1.​ get(url) → Opens the URL in the browser


2.​ getTitle() → Returns the title of the current page
3.​ getCurrentUrl() → Returns the URL of the current page
4.​ getPageSource() → Returns the source code of the page
5.​ getWindowHandle() → Returns ID of the single browser window
6.​ getWindowHandles() → Returns IDs of multiple browser windows

Syntax for all get methods:

driver.get("url");

Example for window handles:

String windowName = driver.getWindowHandle(); // Single window


Set<String> windowNames = driver.getWindowHandles(); // Multiple
windows
Selenium WebDriver – Conditional,
Browser & Wait Methods

1. Conditional Methods
Conditional methods work on WebElements.​
We can access these commands through WebElement.​
These methods return a boolean value (true/false).

(i) isDisplayed()

●​ Checks whether a WebElement is displayed (present on the webpage).

(ii) isEnabled()

●​ Checks whether a WebElement is enabled (can work or not on the webpage).

(iii) isSelected()

●​ Checks whether a WebElement is selected or not.


●​ Mostly used for radio buttons, checkboxes, dropdowns.

✅ Common Syntax
driver.findElement(By.locator("value")).isDisplayed();
driver.findElement(By.locator("value")).isEnabled();
driver.findElement(By.locator("value")).isSelected();

📌 isEnabled() is mostly used for:


●​ Input fields
●​ Dropdowns
●​ Radio buttons
●​ Checkboxes

2. Browser Methods
Browser methods work on the browser window.​
We can access them through the WebDriver instance.

(i) close()

●​ Closes the browser window on which the driver is currently focused.

driver.close();

(ii) quit()

●​ Closes all browser windows opened by the driver instance.

driver.quit();

3. Wait Commands Methods


🔹 Purpose of Wait Commands
●​ Used to tackle synchronization problems between:
○​ Speed of automation script execution
○​ Speed of browser/webpage loading
●​ Synchronization issue occurs when the element is not loaded/available at the time of
script execution.

⚠️ Problem:
●​ If WebElement is not available, script execution fails.
●​ Wait commands help handle such issues.

(A) Thread.sleep(milliseconds)

●​ It is a Java command, not a Selenium WebDriver command.

✅ Advantages
1.​ Very easy to use.

❌ Disadvantages
1.​ If wait time is insufficient → Exception will occur.
2.​ Always waits for the maximum time → reduces script performance.
○​ Example: Thread.sleep(3000) waits full 3 sec, even if element loads in 1
sec.
3.​ Needs to be written multiple times for multiple waits.

📌 Hence, Thread.sleep() is not preferred in automation scripts.


(B) Selenium Waits

Selenium WebDriver provides 2 types of waits:

1.​ Implicit Wait


2.​ Explicit Wait (Fluent Wait)

1. Implicit Wait

●​ Applied to all elements in the script.


●​ Written just after the driver instance creation.
●​ Handles synchronization problem automatically.

✅ Syntax
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

(Default = 0 seconds)

✅ Key Points
●​ Written only once in the script.
●​ Waits for the element until the max time is reached.
●​ If element found earlier, execution continues immediately.

Advantages

1.​ Write once → applicable to all elements.


2.​ Doesn’t always wait for full time (saves execution).
3.​ Easy to use.

Disadvantages

●​ If provided wait time is insufficient → NoSuchElementException occurs.


2. Explicit Wait

●​ Works on a specific element (not all).


●​ Applied with conditions.

✅ Steps
1.​ Declaration of wait
2.​ Usage with conditions​

✅ Syntax
WebDriverWait mywait = new WebDriverWait(driver,
Duration.ofSeconds(10));
WebElement element =
mywait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/
/input[@placeholder='username']")));
element.sendKeys("Anakat01");

📌 Key Points
●​ Waits until condition is satisfied OR max time is reached.
●​ Mostly used for elements facing synchronization problems.

Conditions available in Explicit Wait:

●​ alertIsPresent()
●​ elementToBeClickable()
●​ elementToBeSelected()
●​ frameToBeAvailableAndSwitchToIt()
●​ presenceOfElementLocated()
●​ textToBePresentInElementLocated()
●​ titleContains()
●​ urlContains()
●​ visibilityOf()
●​ visibilityOfAllElements()
●​ visibilityOfElementLocated() ✅ (most used)
✅ Advantages of Explicit Wait
1.​ Condition-based → handles waits more effectively.
2.​ Finding elements is inclusive (for some conditions).
3.​ Will wait for the condition to be true until the timeout.

❌ Disadvantages of Explicit Wait


1.​ Must write the wait statements multiple times for multiple elements.
2.​ If condition fails → leads to exception.

5. Fluent Wait
●​ Works similar to Explicit Wait but gives more control (polling frequency, ignoring
exceptions, etc.).
●​ Must first declare and then use it.

✅ Declaration
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(10)) // max wait time
.pollingEvery(Duration.ofSeconds(2)) // polling interval
.ignoring(NoSuchElementException.class); // ignore exception

✅ Usage
WebElement elementname = wait.until(new Function<WebDriver,
WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("locator"));
}
});

📌 Key Points about Fluent Wait


●​ More effective than Explicit Wait.
●​ Standard: 10 sec timeout, 2 sec polling interval.
●​ Can avoid exceptions by wrapping in try-catch block.
●​ If element not found within time → performance issue with the webpage.
●​ Default time = 0 sec, Maximum time = 10 se
📘 Selenium WebDriver – Navigation &
Window Handling

6. Navigation Commands
There are 4 navigation commands in Selenium WebDriver:

1.​ navigate().to(url) → Visit provided URL


2.​ navigate().back() → Visit previous page
3.​ navigate().forward() → Visit next page (if visited earlier)
4.​ navigate().refresh() → Refresh current page

✅ Syntax
driver.navigate().to("https://example.com");

📌 Difference: driver.get(url) vs driver.navigate().to(url)


●​ get(url) → accepts only String URL.
●​ navigate().to(url) → accepts String URL or URL object.
●​ Internally, navigate().to(url) calls get(url).

7. Handling Browser Windows


Used when we want to switch between multiple windows during automation.

✅ Commands
1.​ driver.getWindowHandles() → Returns all window IDs (Set of Strings).
2.​ driver.switchTo().window(windowID) → Switch to desired window.

✅ Approach
Step 1: Open webpage → click link → new window opens.​
Step 2: Get all window IDs → driver.getWindowHandles().​
Step 3: Convert Set → List (for easy access).​
Step 4: Store Parent & Child window IDs.​
Step 5: Switch using driver.switchTo().window(childID).​
Step 6: Perform operations on child window.​
Step 7: Close child window using driver.close().​
Step 8: Switch back to parent using driver.switchTo().window(parentID).
✅ Example Code
public class WindowHandleExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://opensource-demo.orangehrmlive.com/");

driver.findElement(By.xpath("//a[contains(text(),'OrangeHRM')]")).clic
k();

Set<String> windowIDs = driver.getWindowHandles();


List<String> windowsList = new ArrayList<>(windowIDs);

String parentID = windowsList.get(0);


String childID = windowsList.get(1);

// Switch to child window


driver.switchTo().window(childID);
System.out.println(driver.getTitle());

// Close child window


driver.close();

// Switch back to parent


driver.switchTo().window(parentID);
}
}

📌 If multiple windows → use a for-each loop to iterate through IDs.

How to Interact with Different Types of


Elements
Handling Checkboxes
●​ Radio buttons and checkboxes are almost the same.
●​ Difference:
○​ Checkbox → multiple options can be selected.
○​ Radio button → only one option can be selected.

Checkbox Operations
(1) Selecting a Single Checkbo

●​ Done by identifying a locator that uniquely identifies the element.

Syntax:

driver.findElement(By.locator("locatorValue")).click();

(2) Selecting All the Checkboxes

●​ Sometimes radio buttons are also provided as type = checkbox.


●​ To perform multiple selection, find a common locator that identifies all checkboxes.
●​ Specificity can be achieved via CSS selector.

Syntax:

List<WebElement> varname =
driver.findElements(By.locator("locatorValue"));
for (int i = 0; i < varname.size(); i++) {
varname.get(i).click();
}

Or:

for (WebElement checkbox : varname) {


checkbox.click();
}

(Enhanced for-loop is preferred here.)

(3) Selecting First 3 Checkboxes

●​ Done using a normal for loop.

Syntax:

for (int i = 0; i < 3; i++) {


checkboxVarname.get(i).click();
}
(4) Selecting Last 3 Checkboxes

●​ First, find how many checkboxes are present on the webpage.


●​ Cannot directly select last 3 indexes without knowing the total.
●​ Steps:
1.​ Find total number of checkboxes.
2.​ Subtract 3 from total to get the starting index.
3.​ Iterate from starting index till the last index.

Example:

●​ Total checkboxes = 7
●​ Need to select last 3 → start index = 7 - 3 = 4

Syntax:

for (int i = 4; i < totalNoOfCheckbox; i++) {


checkboxVarname.get(i).click();
}

(5) Deselecting All Selected Checkboxes

●​ We can only deselect when we know which checkboxes are selected.


●​ If we click all checkboxes directly, it may deselect already selected ones but also select
the unselected ones → undesired behavior.

Syntax:

for (int i = 0; i < checkbox.size(); i++) {


if (checkbox.get(i).isSelected()) {
checkbox.get(i).click();
}
}

Handling Alerts & Popups


In Selenium, there are 3 types of JavaScript alerts:

1.​Normal Alert
2.​Confirmation Alert
3.​Prompt Alert
●​ Until we close/perform some action on alerts, no operation can be performed on the
webpage.
●​ Alerts are not WebElements, hence cannot be inspected.
(1) Normal Alert
●​ To perform actions, switch to alert window.

Syntax (switch to alert):

driver.switchTo().alert();

●​ To store the alert in a variable:

Alert alertname = driver.switchTo().alert();


System.out.println(alertname.getText());

Handling Normal Alert (with OK button)

●​ Only has one option: OK / Confirm / Accept.

Syntax 1 (direct):

driver.switchTo().alert().accept();

Syntax 2 (store in variable):

Alert myalert = driver.switchTo().alert();


myalert.accept();

(2) Handling Confirmation Alert


●​ Confirmation alert has 2 options: OK, Cancel.

To click OK:

driver.switchTo().alert().accept();

To click Cancel (Dismiss):

driver.switchTo().alert().dismiss();

(3) Handling Prompt Alert


●​ Prompt Alert has: OK button, Cancel button, Input box.

Steps:

1.​ Always store alert window in variable.


2.​ To send text, use sendKeys("value").
Syntax:

Alert myalert = driver.switchTo().alert();


myalert.sendKeys("value");
myalert.accept(); // clicking OK

Or:

myalert.dismiss(); // clicking Cancel

Important Note on Alerts

●​ Alerts can be handled with switchTo(), but also with Explicit Wait.

Syntax (explicit wait):

WebDriver driver = new ChromeDriver();


WebDriverWait mywait = new WebDriverWait(driver,
Duration.ofSeconds(10));
Alert myalert = mywait.until(ExpectedConditions.alertIsPresent());
myalert.accept();

Authenticated Pop-ups
1.​ These types of popups are rare to find.
2.​ Authentication popups will not allow entering into a website unless the username and
password are provided.
3.​ We cannot use switchTo() or command or explicit wait to handle this type of popup.
4.​ The only option to handle such type of popup is by using the injection method.
○​ In the injection method, we simply pass the username and password with the
URL of the webpage.
○​ Example (without injection):​
https://the-internet.herokuapp.com/basic_auth

Example (with injection):​


Syntax:​
https://username:[email protected]/basic_auth

Example:​
https://admin:[email protected]/basic_auth

5.​ When we inject the username and password in the URL, we do not get the
authentication popup.

Handling Frames & Iframes


●​ Frames and iframes are not exactly the same, but the way of handling them is exactly
the same.
●​ A frame is ideally a webpage that is embedded in the current webpage.
●​ Frames usually have iframes like frame, iframe, and form.
●​ We cannot interact with frame elements directly, as the driver is focusing on the current
webpage.
●​ To interact with frame or its elements, we need to switch the driver to focus on the frame.

This can be achieved using: driver.switchTo().frame()

Ways to switch to a frame/iframe:

1.​ driver.switchTo().frame(name)
2.​ driver.switchTo().frame(id)
3.​ driver.switchTo().frame(WebElement)
4.​ driver.switchTo().frame(frameIndex) → Preferred only when there is a single
frame

Once we enter a frame and interact with it, we need to come out of it to work on the webpage
again.​

driver.switchTo().defaultContent();

Multiple Frames
If a webpage has multiple frames, we must enter one frame, exit it, then enter another frame,
and so on.​
Example:​

enter frame1
exit frame1
enter frame2
exit frame2

Nested Frames

Example:

enter frame1
enter frame2
enter frame3
exit frame3
exit frame2
exit frame1

Switching Methods
There are 3 types of switching methods:
1.​ Browser Windows: driver.switchTo().window(windowID/windowHandle);
2.​ Alerts:​
driver.switchTo().alert();
driver.switchTo().accept();
driver.switchTo().dismiss();
3.​ Frames​
driver.switchTo().frame(id);
driver.switchTo().frame(name);
driver.switchTo().frame(WebElement);

Handling Different Types of Dropdowns


●​ There are mainly 3 types of dropdowns in applications:
1.​ Select Dropdown
2.​ Bootstrap Dropdown
3.​ Hidden Dropdown

1. Select Dropdown

●​ A dropdown can only be considered a Select dropdown if it is inside the <select> tag.
●​ Select dropdowns are easy to handle as there are built-in methods.
●​ Not used much these days compared to other dropdowns.
●​ The dropdown itself is a web element containing multiple <option> elements.
●​ We usually perform only one action → selecting an option from the dropdown.

Steps:

1.​ Interacting/clicking on the dropdown.


2.​ Selecting an option from the dropdown.

Syntax to locate dropdown WebElement:

WebElement varname = driver.findElement(locator);


Select varname2 = new Select(varname);

Syntax to select option from dropdown (3 ways):

1.​ By Visible Text: varname2.selectByVisibleText("value");


2.​ By Value: varname2.selectByValue("value");
3.​ By Index: varname2.selectByIndex(indexNo);

Working with Dropdowns in Selenium


1. Capturing All Options from a Dropdown
●​ The getOptions() method returns all <option> elements inside a <select>
dropdown.

Example:

List<WebElement> options = select.getOptions();


System.out.println("No. of options in a dropdown: " + options.size());

2. Printing All Options


Example:

for (int i = 0; i < options.size(); i++) {


System.out.println(options.get(i).getText());
}

➡️ options.get(i) → captures the WebElement from the list.​


➡️ options.get(i).getText() → captures the text of the WebElement.

3. Bootstrap Dropdowns
●​ Dropdowns having tags other than <select> are called Bootstrap or Hidden
Dropdowns.
●​ Some dropdowns allow multiple selection (like checkboxes).
●​ In Bootstrap/hidden dropdowns, XPath is the most reliable way to locate elements.

Operations on Bootstrap Dropdowns


(i) Capturing the Dropdown
driver.findElement(By.xpath("value")).click();

●​ ⚠️
●​ Captures and clicks/expands the dropdown.
Value should be common & stable (not dynamically changing with page reloads).

(ii) Selecting a Single Option


driver.findElement(By.xpath("value1")).click();

(iii) Capturing All Options & Finding Size


List<WebElement> options = driver.findElements(By.xpath("locator"));
System.out.println("No. of options: " + options.size());

✅ This section covers:


●​ Standard <select> dropdowns.
●​ Printing their options.
●​ Bootstrap/hidden dropdowns and how to interact with them.

(iv) Printing Options from Dropdowns


for (WebElement op : var1) {
System.out.println(op.getText());
}

(v) Selecting Multiple Options


for (WebElement op : var1) {
String var2 = op.getText();
if (var2.equals("val1") || var2.equals("val2") ||
var2.equals("val3")) {
op.click();
}
}

➡ Bootstrap dropdowns do not have any built-in method.

(3) Hidden Dropdowns


●​ Nowadays, most of the applications & platforms are using hidden dropdowns.
●​ In hidden dropdowns, the options are hidden / not visible directly on the webpage.
●​ In hidden dropdowns, the options are not available/visible in the HTML.
●​ These are dynamic dropdowns (also called auto-suggested box) where options are
visible only on the dropdown runtime, and as we clear it, elements will disappear.
●​ In such cases, to capture the options or make them visible in the HTML, we will need to
use the debugger (available in developer tools → Elements).
●​ The debugger provides only 5 seconds time, within this time we have to click on the
dropdown and after 5 seconds page will freeze and then we can see and
interact/capture the options of the dropdown.

Another way to unhide the options of the dropdown

●​ Simply remove the blur event tag from Event Listener.


●​ Steps:
○​ Open Console → Event Listeners → blur → remove

Syntax to Click on Dropdown


driver.findElement(locator).click();

Syntax for Selecting Single Option from Dropdown


driver.findElement(locator).click();

Syntax to Count Number of Options from Dropdown


List<WebElement> var1 = driver.findElements(locator);
System.out.println("No of options: " + var1.size());

Syntax for Printing Options from Dropdown


for (WebElement var2 : var1) {
System.out.println(var2.getText());
}

Assignment
1.​ Handle Training Dropdowns with/without Select Class​
https://phptravels.com/demo/jquery-dependent-dropdown-list-countr
ies-and-states/
○​ (a) Count total number of options
○​ (b) Print all options
○​ (c) Select one option
2.​ Hidden Dropdown
○​ Login to OrangeHRM → PIM → Employee Status

🔗
3.​ Custom Multi-Select Box​
http://testautomationpractice.blogspot.com/
(4) Auto-Suggest Dropdowns
●​ An example of auto-suggest dropdown is the Google Search bar.
●​ Auto-suggest dropdown is a dynamic dropdown but the options keep on changing.
●​ To handle auto-suggest dropdowns:

Step 1: Visit the page, capture the dropdown and search for some value using a unique XPath
or locator.

Step 2: Wait for 5 seconds as it takes some time to suggest the options.

Step 3: Write an XPath that matches all the elements (including the options).

Step 4: Interact and use the options based on the XPaths.

Example: Google Search Engine


●​ Searched "Selenium" and found 10 options after waiting for 5 seconds.
●​ To print and capture all options:

List<WebElement> list =
driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::di
v[@class='wM6W7d']"));

Perform Action
●​ After this, simply perform whatever action we want.
●​ Once clicking on an option, we should break the loop as we do not need other options.

for (int i = 0; i < list.size(); i++) {


System.out.println(list.get(i).getText());

if (list.get(i).getText().equals("Selenium")) {
list.get(i).click();
break;
}
}
Web Tables
Types of Web Tables
1.​ Static Web Table
○​ Fixed rows & columns
2.​ Dynamic Web Table
○​ Fixed columns, rows can change
3.​ Web Table with Pagination
○​ Table is distributed across multiple pages due to large no. of records

Static Web Tables


●​ Tables are under <table> tags.
●​ The architecture of a table in HTML DOM is:​

<table> <!-- table -->


<tbody> <!-- table body -->
<tr> <!-- table row -->
<th> <!-- table header -->
</th>
</tr>
<tr> <!-- table row -->
<td> <!-- table column -->
</td>
</tr>
</tbody>
</table>

Example Web Table


BookName AuthorName Subject Price

Learn Sel Anas Seleniu 100


m

Learn Java Ashkar Java 200

JS Mukesh JS 200

Python Faisal Python 400


API Mukesh API 500

Important Notes
●​ The order can be different depending on table to table.
●​ Different operations can be performed on a table.

Operations on Web Tables


1) Counting Number of Rows in a Table

●​ This can be done by simply counting the <tr> tags in the table.

Syntax: driver.findElements(By.xpath("locator_value")).size();

Example:

driver.findElements(By.xpath("//table[@name='BookTable']//tr")).size()
;

or

driver.findElements(By.tagName("tr")).size();

(ii) Counting total number of columns in a table

●​ This can be found by counting the number of <th> or <td> tags in the table.

Syntax:

driver.findElements(By.xpath("value")).size();

or

driver.findElements(By.tagName("td")).size();

(or th tag depending on table)

(iii) Counting/Reading data from specific row and column

●​ This can be achieved by writing that particular <tr> tag and <td> tag in the table.
Syntax:

driver.findElement(By.xpath("value")).getText();

👉 Example:
// When text or string is stored at some location
// xpath expression example:
"//table[@name='BookTable']/tbody/tr[3]/td[1]"

(iv) Reading data from all the rows and columns


●​ For this, we will have to use Nested For Loops:
○​ Outer loop → counts rows
○​ Inner loop → counts columns
●​ We need to dynamically provide row and column numbers in order to read data.
●​ For this, we will have to find number of row count and column count.

Syntax:

int row = driver.findElements(By.xpath("value")).size();


int column = driver.findElements(By.xpath("value")).size();

for (int i = 2; i <= row; i++) {


for (int j = 1; j <= col; j++) {
String varname = driver.findElement(
By.xpath("//table[@name='BookTable']/tbody/tr[" + i +
"]/td[" + j + "]")
).getText();

System.out.print(varname + " ");


}
System.out.println();
}

🔑 Note:
●​ i = 2 because tr[1] holds headers (<th>).
●​ If no headers are present, loop can start from i = 1.
●​ When we want to pass values from a string or row/col dynamically, we must append
using + operator.

(vi) Fetching data from table based on conditions


●​ This can be achieved by simply manipulating row and column indexes, then reading data
from each location, and comparing conditions to perform an action.
Example: Print book names whose author is Mukesh.

for (int i = 2; i <= rows; i++) {


String authorName = driver.findElement(
By.xpath("//table[@name='BookTable']/tbody/tr[" + i +
"]/td[2]")
).getText(); // 2nd column contains author names

if (authorName.equals("Mukesh")) {
String bookName = driver.findElement(
By.xpath("//table[@name='BookTable']/tbody/tr[" + i +
"]/td[1]")
).getText(); // 1st column contains book names
System.out.println(bookName);
}
}

(vii) Extract all the prices and calculate the total price
This can be done in 3 steps:

1.​ Step 1: Initialize variable → int total = 0;


2.​ Step 2: Extract all price values from the table inside a for loop.
3.​ Step 3: Add each price to total.
○​ Make sure to typecast each price from String to Integer.

Code Example:

int total = 0;
for (int i = 2; i <= rows; i++) {
String price = driver.findElement(
By.xpath("//table[@name='BookTable']/tbody/tr[" + i +
"]/td[3]")
).getText(); // 3rd column holds prices

total = total + Integer.parseInt(price);


}
System.out.println("Total price: " + total);
(2) Dynamic WebTables with Pagination
●​ In a dynamic webtable, number of columns remains same but number of rows
changes.
●​ In a dynamic webtable, data within the table apart from rows can also change.
●​ In a dynamic webtable with pagination, a large number of records are stored in the
table in different pages.
●​ In dynamic tables with pagination, first we need to find total number of pages.

✅ Basically there are 3 major/important things to do:


1.​ Count the total number of pages.
2.​ Click and visit each page.
3.​ Read data from each page.

(i) How to count total number of pages in dynamic webtable with pagination
(Other method is counting total number of tags in which each page is present).

Page numbers can be found in many ways.​


One way is to extract it from the string below the table where it mentions total pages.​

Example: "Showing 1 to 10 of 1909 (1909 Pages)"

Here, we can get the total pages (i.e. 1909) by simply using substring method:​
substring(26, 30); // 1909

⚠️ Problem:​
Since the table is dynamic, values and data will change and hence page numbers can
sometimes be 4 digits, sometimes 3, 2, or 1.​
Here, the hardcoded index will change and script will fail.

✅ Solution:
To overcome this, we need to find something static, something that remains in the string
consistently and use them.

Example:​
Output string:

"1909 Pages"
●​ Here we can use their index:
○​ index of "(" = 1st index of number.
○​ index of "Pages" = last index of number.

🔹 Dynamic substring method to capture the page number:


String pages = string.substring(
string.indexOf("(") + 1,
string.indexOf("Pages") - 1
);

Since page number extracted is a string (substring from the captured text), we need to
convert it to an integer using​
Integer.parseInt(...).

✅ Final Syntax with Example:


String text = driver.findElement(
By.xpath("//div[contains(text(),'Page')]")
).getText();
int totalPages = Integer.parseInt(
text.substring(text.indexOf("(") + 1, text.indexOf("Pages") - 1)
);

(ii) Click on each page until we reach the last page


●​ This can be achieved via a for loop.
●​ We need not click on page 1, since in most cases the first page is already visible.

Syntax with Example:

for (int p = 1; p <= totalPages; p++) {

if (p > 1) {
// Since page 1 is by default visible,
// consider if page is greater than 1
WebElement activePage = driver.findElement(
By.xpath("//ul[@class='pagination']//a[text()='" + p +
"']")
);
activePage.click();
}
}

(iii) Reading data from each page


●​ To read data from table, the first step is counting total number of rows (except header).

int rows = driver.findElements(By.xpath("value")).size();

Extracting Data from Each Page (Example: Customer Name in 2nd Column)
To extract data (e.g., Customer Name which is in the 2nd column) from all rows of a table:

for (int x = 1; x <= noOfRows; x++) {


String customerName = driver.findElement(
By.xpath("//table[@name='custTable']/tbody/tr[" + x +
"]/td[2]")
).getText();
}

✅ Similarly, we can do the same for other columns and details.


NOTE:
1.​ The above are only examples. Scripts will vary based on the design and type of tables.
2.​ Sometimes tables can be in <div> tags rather than <table> tags.
○​ Example can be found at: orangeHRM demo → Admin
○​ Here, row & column data are present in div tags.
3.​ Such type of tables are called Bootstrap Tables (practice such tables too).

ASSIGNMENT
●​ Go to: "testautomationpractice.blogspot.com"
●​ Do the dynamic webtable:
1.​ Count total pages.
2.​ Visit each page.
3.​ Read data from each page.
4.​ Click on checkbox as per certain condition.
📅 Date Pickers / Calendars in Automation
🔹 Types of Web Elements
1.​ Standard Elements
○​ Same type across almost every website.
○​ Easy to automate as their structure is consistent.
2.​ Non-Standard / Customized Elements
○​ Differ across websites depending on how developers design them.
○​ Automation is harder since there’s no fixed structure.
○​ Date Pickers / Calendars usually fall under this category.

🔹 Why Automation is Challenging


●​ No universal automation script for all date pickers.
●​ Different websites have different designs.
●​ But for a single application, if date pickers appear multiple times, one script can work
everywhere.

🔹 General Observations
●​ Sometimes, simply using the sendKeys() command works for inputting dates.
●​ On other pages, you may need to automate switching between day, month, and year.
●​ If dropdowns are present, automation is easier as you can just click/select values.

📌 Examples of sample date pickers:


●​ jQuery Datepicker
●​ Visa Application Form Dummy

🔹 Types of Date Pickers


1.​ Input Box Base
○​ Allow direct entry of date using sendKeys().
○​ Simplest to automate.
2.​ Dropdowns
○​ Provide separate dropdowns for day, month, year.
○​ Easy automation using standard dropdown handling.
3.​ Custom Calendars
○​ Complex UI.
○​ Requires building custom logic (clicking arrows, navigating months/years, etc.).

🔹 Handling Input Box Date Pickers


Steps to automate:

1.​ Locate the date picker element (input box).


2.​ Activate the input field.
3.​ Use sendKeys() to send the date value.

⚠️ Note: If sendKeys() doesn’t work, you must handle it with custom logic (e.g., selecting
from the UI).

Handling Date Picker When SendKeys


Doesn’t Work
●​ In case, when sendKey does not work, we will need to handle the datepicker.
●​ It can be done by following the below logic.
●​ Example date picker: date = 7, month = January, year = 2025.

Steps:
Step 1: Click on the date picker field.

Step 2: Capture the displayed month and year values into variables and then compare them
with your expected date and year.

●​ If both are same, simply select the date.


●​ If they are not the same, then we should click on the forward / backward arrow mark.

Step 3: Keep on comparing the month and year with input month and year until we find our
match.

Step 4: Once match is found, simply select the date and done.

Step 5: We will have to keep on clicking until expected month and year is not found because we
do not know how many end condition will come.

Step 6: Since ending condition is not known, hence we will use WHILE LOOP in this case.
Logic / Pseudocode:
boolean x = true;

while(x == true) {
At_month = driver.findElement(By.xpath("value")).getText();
At_year = driver.findElement(By.xpath("value")).getText();

if(At_month == month && At_year == year) {


x = false;
}
else if(At_year > year) { // If input year is smaller than
actual year
left_arrow = driver.findElement(By.xpath("value")).click();
}
else if(At_year < year) { // If input year is greater than
actual year
right_arrow = driver.findElement(By.xpath("value")).click();
}
}

date =
driver.findElement(By.xpath("//a[contains(@class,'ui-state-default')][
text()='7']")).click();

Handling Dynamic Datepickers / Calendars


●​ In datepickers/calendars, the locations of each date are not same for every month as
they are dynamic (they change their position).
●​ Hence, in such cases we need to extract all the dates from the datepicker in a list of
WebElements and then iterate on the datepicker.
●​ Whenever date (input actual) matches, we click on it.
●​ Iteration is done via for loop.

Example Code: Selecting Date


static void selectDate(WebDriver driver, String date) {
List<WebElement> alldates =
driver.findElements(By.xpath("value"));
// For each date: alldates

for(WebElement d : alldates) {
if(d.getText().equals("12")) { // if
(d.getText().equals(date))
d.click();
break;
}
}
}

Creating User Defined Method to Extract &


Compare Month & Year
●​ We need to write this method only once and wherever required we can call this method.

Method: Select Month and Year


static void selectMonthAndYear(WebDriver driver, String month, String
year) {
while(true) {
String currentMonth =
driver.findElement(By.xpath("value")).getText();
String currentYear =
driver.findElement(By.xpath("value")).getText();

if(currentMonth.equals(month) && currentYear.equals(year)) {


break;
}
driver.findElement(By.xpath("value")).click(); //
click on arrow (left/right)
}
}

✍️ Margin Notes from your page:


●​ Methods to select date
●​ Method to select month & year​

Mouse Actions in Selenium


To perform mouse actions, Selenium provides a predefined class called Actions.

●​ Using this class and its methods, we can perform mouse operations like:
○​ Mouse hover
○​ Left click
○​ Right click
○​ Double click
○​ Drag & Drop

Common Mouse Actions


1.​ Mouse Hover → moveToElement(element)
2.​ Left Click & Drag & Drop
3.​ Right Click → contextClick(element)
4.​ Double Click → doubleClick(element)

Actions Class
●​ The Actions class contains multiple methods for handling advanced user interactions.
●​ To use it, we need to:

Import the package:​


import org.openqa.selenium.interactions.Actions;

1.​ Create an Actions object:​


Actions act = new Actions(driver);

1. Mouse Hover → moveToElement(element)


●​ Mouse hover means placing the mouse pointer over an element.

Method used: moveToElement(elementLocator)

✅ Syntax:
act.moveToElement(driver.findElement(By.xpath("elementLocator"))).perf
orm();

●​ perform() → executes and completes the action.


●​ Earlier build().perform() was used, but now perform() is enough.
✅ Example:
WebElement desktop = driver.findElement(By.xpath("locatorValue"));
WebElement mac = driver.findElement(By.xpath("locatorValue"));
Actions act = new Actions(driver);
act.moveToElement(desktop).moveToElement(mac).click().perform();

➡️ This performs hover on Desktop → then hover on Mac → then click on Mac.
2. Right Click Action → contextClick(element)
To perform a Right Click on an element:

Steps:
1.​ Capture the element.
2.​ Create an Actions class object.
3.​ Use contextClick(element) method.

✅ Example:
WebElement button = driver.findElement(By.xpath("value"));
Actions act = new Actions(driver);

act.contextClick(button).perform();

3. Double Click Action → doubleClick(element)


To perform a Double Click on an element:

Steps:
1.​ Capture the element.
2.​ Create an Actions class object.
3.​ Use doubleClick(element) method.

✅ Example:
WebElement button = driver.findElement(By.xpath("value"));
Actions act = new Actions(driver);
act.doubleClick(button).perform();
4. Drag and Drop → dragAndDrop(sourceElement,
targetElement)
●​ Drag and Drop is possible only if the application supports it.

Steps:
1.​ Capture the source and target elements.
2.​ Create an Actions class object.
3.​ Use dragAndDrop(source, target) method.

✅ Example:
WebElement source = driver.findElement(By.xpath("value1"));
WebElement target = driver.findElement(By.xpath("value2"));

Actions act = new Actions(driver);

act.dragAndDrop(source, target).perform();

Keyboard Actions in Selenium


●​ The Actions class can be used to perform certain keyboard-based actions such as:​

○​ Select All
○​ Copy–Paste
○​ Enter
○​ Tab
○​ Backspace
○​ etc.

Common Keyboard Operations using Actions Class:

1.​ Select All → Ctrl + A


2.​ Copy → Ctrl + C
3.​ Paste → Ctrl + V
4.​ Move to Next Tab → Tab key

Example: Performing Keyboard Operations


// Create Actions object
Actions act = new Actions(driver);

// Select All → Ctrl + A


act.keyDown(Keys.CONTROL)
.sendKeys("a")
.keyUp(Keys.CONTROL)
.perform();

// Copy → Ctrl + C
act.keyDown(Keys.CONTROL)
.sendKeys("c")
.keyUp(Keys.CONTROL)
.perform();

// Move to Next Box → Tab


act.sendKeys(Keys.TAB).perform();
// Paste → Ctrl + V
act.keyDown(Keys.CONTROL)
.sendKeys("v")
.keyUp(Keys.CONTROL)
.perform();

Important Methods in Keyboard Actions:


1.​ keyDown() – Presses a key (without releasing it).
2.​ keyUp() – Releases a pressed key.

👉 Both are usually used together.


Example: Ctrl + Shift + A
act.keyDown(Keys.CONTROL)
.keyDown(Keys.SHIFT)
.sendKeys("a")
.keyUp(Keys.SHIFT)
.keyUp(Keys.CONTROL)
.perform();

✅ Similarly, we can handle other keys like Enter, Tab, Esc, etc.

Upload Files in Selenium


🔹 By default, Selenium does not recommend automating file upload/download.
●​ Such test cases should ideally not be automated.
●​ However, if required, Selenium supports single and multiple file uploads.

1. Uploading a Single File


Steps:

1.​ Visit the target URL and maximize the browser.


2.​ Pass the file location using the sendKeys() method.

Example:

driver.findElement(By.xpath("//input[@type='file']"))
.sendKeys("C:\\path\\to\\file1.txt");

2. Uploading Multiple Files


Steps:

1.​ Visit the URL and maximize the browser.


2.​ Store each file location in separate variables.
3.​ Pass all file paths using sendKeys() with newline (\n) separator.

Syntax:

driver.findElement(By.xpath("//input[@type='file']"))
.sendKeys(file1 + "\n" + file2 + "\n" + file3);

Example with Validation


// Upload file
driver.findElement(By.xpath("//input[@type='file']"))
.sendKeys("C:\\path\\to\\file.txt");

// Validate uploaded file name


if (driver.findElement(By.xpath("//input[@type='file']"))
.getText().equals("file.txt")) {
// Pass statement
} else {
// Fail statement
}
Screenshots in Selenium
📸 Screenshots are mainly taken for failed test cases.
Selenium allows capturing screenshots of:

1.​ Full Page


2.​ Specific Area of Page (introduced from Selenium 4 onwards)
3.​ WebElement

Important Notes
●​ A single variable can hold only one screenshot.​
For multiple screenshots, multiple variables must be used.
●​ The temporary location of the screenshot is unknown.​
We must copy it to a target file for saving.
●​ To save the screenshot, we use File class.​

File srcFile = new File("Path");

// System property for current user directory


System.getProperty("user.dir");

●​ To copy screenshot file to target:

srcFile.renameTo(targetFile);

Example – Full Page Screenshot


driver.get("http://demo.nopcommerce.com/");
driver.manage().window().maximize();

// Capture screenshot
TakesScreenshot ts = (TakesScreenshot) driver;
File srcFile = ts.getScreenshotAs(OutputType.FILE);

// Define target location


File targetFile = new File(System.getProperty("user.dir") +
"/screenshots/fullpage.png");
// Save screenshot
srcFile.renameTo(targetFile);

Handling SVG Elements

Handling Broken Links

You might also like