0

I need to automate the following test: after the login on the web page a new pop-up window (with app) opens and all steps should be done in this new window.

Question: how to code to switch from the current login window to the new pop-up window?

Thank you!

Bob Bolden
  • 93
  • 1
  • 2
  • 8

1 Answers1

1

If you want to handle child window then you have to use handles in seleneium, Kindly refer below code:

String parentWindowHandle = driver.getWindowHandle(); // get the current window handle

//Perform action on your parent window 
//Perform clcik() action on your parent window that opens a new window    

for (String winHandle : driver.getWindowHandles()) {

         if(!winHandle.equals(parentWindowHandle))
         {
            driver.switchTo().window(winHandle); // Here yor switching control to child window so that you can perform action on child window
            System.out.println("Title of the new window: " +
            driver.getTitle());
            //code to do something on new window
            System.out.println("Closing the new window...");
            driver.close();
         }

   }   

driver.switchTo().window(parentWindowHandle);
System.out.println("Parent window URL: " + driver.getCurrentUrl());
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
  • there is a problem. IE closes the parent window (no idea why). sometimes the test works, sometimes it fails with the error NoSuchWindowException: Window is closed in the code line: String parentWindowHandle = driver.getWindowHandle(); – Bob Bolden Apr 16 '20 at 17:48
  • what issue are you facing ? – SeleniumUser Apr 16 '20 at 17:49
  • it looks like the app and IE 11 is doing the following: after the log-in in the parent window it opens a new pop-up window and closes the parent one without focus in the new pop-up window. If I add the code you mention above, sometime it works, sometime it does not. Without your code it shows an error: NoSuchWindowException: Unable to get browser. – Bob Bolden Apr 16 '20 at 17:53
  • Check with chrome and firefox because if parent window is closinng on them then you need to raise this with dev team – SeleniumUser Apr 16 '20 at 17:55
  • App is compatible with IE only :( probably I can just modify this driver.switchTo().window(winHandle); without checking the parent window? – Bob Bolden Apr 16 '20 at 17:56
  • I would say first check expected behavior if you want to continue with child window then don't switch back to parent window again – SeleniumUser Apr 16 '20 at 18:08
  • It looks like this is IE issue. Many thanks for your help! – Bob Bolden Apr 16 '20 at 20:00