Introduction
Pop-ups are a common element in modern web applications. They can appear as alerts, confirmation boxes, or new browser windows/tabs. Selenium provides robust methods to handle these scenarios effectively, enabling testers to switch between windows and interact with them seamlessly.
A pop-up is a window that appears on the screen after a specific action. It can happen in different situations depending on the event.
Types of Pop-Ups Pop-ups can be broadly categorized into:
1.JavaScript Alerts: Simple browser alerts triggered by JavaScript, such as alert(), confirm(), or prompt().
2.Browser Windows or Tabs: New browser windows or tabs opened due to user actions or application triggers.
This blog focuses on handling multiple browser windows or tabs using Selenium WebDriver
Working with Multiple Windows or Tabs
Selenium provides the getWindowHandle() and getWindowHandles() methods to manage multiple windows.
1.getWindowHandle() The method gets a unique ID for the current window. This ID helps you identify and switch back to this window later, especially when dealing with multiple windows or tabs. It returns the ID as a String.
Syntax:
Driver.getWindowHandle()
2.getWindowHandles() The getWindowHandles() method retrieves a set of unique identifiers for all open windows or tabs. It is used to iterate through windows and switch between them.
Syntax:
driver.getWindowHandles()
How to Handle Multiple Windows in Selenium
Steps to Handle Multiple Windows
1.Store the Original Window Handle
Save the ID of the current window using getWindowHandle().
2. Perform Actions to Open a New Window
Trigger an action (e.g., clicking a button or link) to open a new window or tab.
3.Switch to the New Window Use getWindowHandles() to iterate through all open windows and switch to the new one.
4.Perform Actions in the New Window
Interact with elements within the new window.
5.Switch Back to the Original Window
Use the stored original window handle to return to the main window.
Example: Handling Multiple Windows
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WindowHandleExample {
public static void main(String[] args) {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
WebDriver driver = new ChromeDriver();
// Open a website
driver.get("https://example.com");
// Store the ID of the original window
String originalWindow = driver.getWindowHandle();
// Click a link that opens a new window
driver.findElement(By.id("openNewWindowButton")).click();
// Switch to the new window
for (String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(originalWindow)) {
driver.switchTo().window(windowHandle);
break;
}
}
// Perform actions in the new window
System.out.println("Title of the new window: " + driver.getTitle());
// Switch back to the original window
driver.switchTo().window(originalWindow);
// Perform actions in the original window
System.out.println("Title of the original window: " + driver.getTitle());
// Close the browser
driver.quit();
}
}
Conclusion
Handling multiple windows or tabs is a vital skill for Selenium testers. By using getWindowHandle() and getWindowHandles(), you can efficiently switch between windows and interact with elements in different contexts. Understanding and mastering these methods will help you write reliable and robust test scripts for applications with multiple windows.
Comments
Post a Comment