Latest post from this blog

Test Scenarios vs. Test Cases: Understanding the Basics

What Are Test Scenarios Test scenarios represent high-level ideas or conditions that need to be validated to ensure the application works as expected. They provide a broader perspective and are typically used during the test planning phase. Purpose: To capture the "what to test" without going into granular details. Example of a Test Scenario: Verify that a user can successfully log in to the application using valid credentials. Verify the behavior of the login page when invalid credentials are entered. Verify the application behavior when the login button is clicked without entering any credentials. What Are Test Cases Test cases are detailed documents that define the specific steps to execute a test. They cover inputs, execution steps, expected results, and actual outcomes. Purpose: To guide the tester step-by-step on "how to test." Example of a Test Case (for the first scenario): Test Case ID TC_01_Login_Valid_Credentials Test Scenario Verify user login with val...

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

Common Exception in Selenium

Test Scenarios vs. Test Cases: Understanding the Basics

Handling Shadow DOM in Selenium WebDriver

Handling HTTPS Websites and SSL Certificate Errors in Selenium

Normalize-space Function In Xpath