1.

How To Handle Multiple Popup Windows In Selenium?

Answer»

In many SCENARIOS, when you open a web site, the application displays multiple windows. Those can be advertisements or a kind of information showing on popup windows. Using Windows Handlers in selenium WebDriver, we can handle multiple.

  • STEP 1: Open website and get the main window handle by using driver.getWindowHandle();
  • Step 2: Now get all the window HANDLES by using driver.getWindowHandles();
  • Step 3: Compare all the window handles with the main Window handles and perform the operation on the window which you need.

In the below example we will see how to handle multiple windows and close all the CHILD windows which are not REQUIRED. Compare the main window handle to all the other window handles and close them.

package multialert; import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test;  public class WindowExamples {          static WebDriver driver;          public void test_CloseWindowsExceptMainWindow() {                      driver = new FirefoxDriver();                      // Opens Naukri.com website with multiple windows                         driver.get("http://www.naukri.com/");                    // get main window handle                      String winTitle= getCurrentWindowTitle();                      String mainWindow = getMainWindowHandle(driver);                         Assert.assertTrue(closeAllOtherWindows(mainWindow));                      Assert.assertTrue(winTitle.contains("Jobs - Recruitment-Job Search"), "window title is not matching");          }                         public String getMainWindowHandle(WebDriver driver) {                      return driver.getWindowHandle();          }          public String getCurrentWindowTitle() {                      String winTitle = driver.getTitle();                      return winTitle;          }          //Except main window, close all windows.          public static boolean closeAllOtherWindows(String openWindowHandle) {                      Set<String> allWindowHandles = driver.getWinHandle();                      for (String currentWindowHandle : allWindowHandles) {                                  if (!currentWindowHandle.equals(openWindowHandle)) {                                                 driver.switchTo().window(currentWindowHandle);                                                 driver.close();                                 }                      }                         driver.switchTo().window(openWindowHandle);                      if (driver.getWindowHandles().size() == 1)                                  return true;                      else                                  return false;          } }

The below image shows you the multiple windows that open in the application. Three windows are open. One is the main window and the other two are child windows.



Discussion

No Comment Found