Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
153 views
by The go-to Tester (324 points)
Please describe...how can we handle web based pop up?

1 Answer

1 like 0 dislike
by The go-to Tester (181 points)

Web based pop up are of many types.

  • In page pop up
  • In page pop up with iframe
  • Pop up in seperate window

In case of, your pop up is part of your page, you can simply identify element and interact with it.

In case of your pop up is a part of your page, but displayed inside iframe, you will have to first store current window handle into String variable, say mainWindowHandle . You can now use driver.switchTo().frame() and pass the iframe locator. It will switch to the iframe. You can now complete your interaction and use driver.switchTo().window() pass your mainWindowHandle to switch back to the main window.

If your pop up is opening in a new window, you can first use driver.getWindowHandles() it will return you all window handles in form of List of String. Now you can store current window handle as mainWindowHandle. You can start the loop and say driver.switchTo().window() to switch to the new window. Complete your interactions and switch back to the main window.

e.g.

// Store the current window handle
String mainwindowHandle = driver.getWindowHandle();
 
// Perform the peration that opens new window
 
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
if(!mainwindowHandle.equals(winHandle))
driver.switchTo().window(winHandle);
}
 
// Perform the actions on new window
 
// Close the new window, if that window no more required
driver.close();
 
// Switch back to original browser (first window)
driver.switchTo().window(mainwindowHandle);
 

// Continue with original browser (first window) 

 

Hope that helps!


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...