How can we handle windows-based pop up?

904    Asked by RishabhDwivedi in QA Testing , Asked on Jan 5, 2020

There is a scenario where I working on a web based application testing project and during the time of my test implementation, I have encountered a scenario where a windows based pop up was disturbing my flow of automated test. How can I troubleshoot and resolve this particular issue? 

Answered by Rishabh Dwivedi

Here are the steps given of how you can troubleshoot and resolve the issue of windows based pop-up:-

Identify and switch to pop-up window

First, you would need to try the pop-up window by using its handle or title and switch the WebDriver Focus to that window.

Interactions with elements in the popup window

Once you have switched to the pop-up window, now you can interact with its element like input fields, button etc as needed for your test requirements.

Switch back to main window

After completing actions on the popup window now you can switch back the WebDriver focus and back to the main window for the purpose of continuing the test implementation.

Here are the coding given of all these above steps:-

Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;
Import java.util.Set;
Public class PopupHandlingExample {
    Public static void main(String[] args) {
        System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver”);
        WebDriver driver = new ChromeDriver();
        Driver.get(https://example.com);
        // Get the main window handle
        String mainWindowHandle = driver.getWindowHandle();
        // Click on a button that triggers the pop-up window
        WebElement popupButton = driver.findElement(By.id(“popupButton”));
        popupButton.click();
        // Get all window handles
        Set windowHandles = driver.getWindowHandles();
        // Switch to the pop-up window
        For (String handle : windowHandles) {

            If (!handle.equals(mainWindowHandle)) {

                Driver.switchTo().window(handle);
                Break;
            }
        }
        // Perform actions on the pop-up window
        WebElement popupInputField = driver.findElement(By.id(“popupInputField”));
        popupInputField.sendKeys(“Test Input”);
        WebElement popupSubmitButton = driver.findElement(By.id(“popupSubmitButton”));
        popupSubmitButton.click();
        // Switch back to the main window
        Driver.switchTo().window(mainWindowHandle);
        // Continue with other test actions on the main window
        // …
        // Close the WebDriver session
        Driver.quit();
    }

Here are the coding given in python programming language:-

From selenium import webdriver

From selenium.webdriver.common.by import By

From selenium.webdriver.support.ui import WebDriverWait

From selenium.webdriver.support import expected_conditions as EC

# Initialize the WebDriver (assuming you have downloaded the appropriate driver for your browser)

Driver = webdriver.Chrome(executable_path=”path_to_chromedriver”)
Driver.get(https://example.com)
# Get the main window handle
Main_window_handle = driver.current_window_handle
# Click on a button that triggers the pop-up window
Popup_button = driver.find_element(By.ID, “popupButton”)
Popup_button.click()
# Wait for the pop-up window to appear and switch to it
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
For window_handle in driver.window_handles:
    If window_handle != main_window_handle:
        Driver.switch_to.window(window_handle)
        Break
# Perform actions on the pop-up window
Popup_input_field = driver.find_element(By.ID, “popupInputField”)
Popup_input_field.send_keys(“Test Input”)
Popup_submit_button = driver.find_element(By.ID, “popupSubmitButton”)
Popup_submit_button.click()
# Switch back to the main window
Driver.switch_to.window(main_window_handle)
# Continue with other test actions on the main window
# …
# Close the WebDriver session
Driver.quit()

Here is the coding given for HTML:-




<meta</span> charset=”UTF-8”>

<meta</span> name=”viewport” content=”width=device-width, initial-scale=1.0”>

Pop-up Window Example

[removed]

Function openPopup() {
    // Open a new window with specified URL and dimensions
    Var popup = window.open(https://www.example.com, “Example Pop-up”, “width=600,height=400”);
    // Focus on the new pop-up window
    Popup.focus();
}

[removed]



Pop-up Window Example






Your Answer

Interviews

Parent Categories