Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

Handling Pop-Ups and Multiple Windows in Selenium

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

Popular posts from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java