0

I am attempting to switch to a popup window using selenium web driver on IE. When I get to the line that switches to my popup window the code just hangs. I'm looking for ways that I can either attempt to switch the window 10 times and try another attempt after 20 seconds as I tried to do below or a better way to ensure the windows switches properly. If I manually close the popup I get noSuchWindow Exceptions and the code bombs out. I've reviewed other stackoverflow articles before posting this, but I believe my issue is unique. Here is my scenario:

    Scenario:
    1. Retrieve parent window handle
    2. Perform action launching popup window
    3. Get the popup window handles and store them in a string set
    4. Loop through window handles until there are no more. Retrieve Popup window handle
    5. Loop until the popup window does not match the parent windows handle and if 20 seconds has passed
    6. Switch to popup ---Code Hangs Here---
    7. Retrieve popup title
    8. Close popup
    9. Switch to parent
    10. Verification of title

Below is all relevant code to the above scenario:

    String popupWindow = "";
    String parentWindow = "";
    int saveCount = 0;
    int getPopupCount = 0;
    int tryCount = 0;

    // Get Parent window handle
    parentWindow = driver.getWindowHandle();
    System.out.println("parentWindow: " + parentWindow);
    Thread.sleep(500);

    //Perform Action launching popup window

    //Get the popup window handles and store them in a string set
    Set<String> popups =  driver.getWindowHandles();
    saveCount = getPopupCount;
    try {
        tryCount = 0;
        //Loop until the popup count does not equal the save count or until 10 tries (20 seconds) have passed
        while (saveCount == getPopupCount && tryCount++ < 10) {
            //Wait 2 second
            Thread.sleep(2000);

            getPopupCount = popups.size();
            System.out.println("getPopupCount: -" + getPopupCount + "-");
        }//end while
        if (tryCount >= 10) {
            System.out.println("Failed after 10 tries");
        }//end if

        //Loop through window handles until there are no more. Retrieve Popup window handle
        Iterator<String> myIterator = popups.iterator();
        while (myIterator.hasNext()) {
            popupWindow = myIterator.next();
            System.out.println("popupWindow: " + popupWindow);
            System.out.println("Boolean should be false: " + parentWindow.equalsIgnoreCase(popupWindow));
            Thread.sleep(5000);

            //fetch starting time
            long startTime = System.currentTimeMillis(); 

            //Loop until the popup window does not match the parent windows handle and if 20 seconds has passed
            while(!parentWindow.equalsIgnoreCase(popupWindow) && (System.currentTimeMillis()-startTime) < 20000) {
                try{
                    Thread.sleep(500);

                    //Switch to the Popup window            
                    //TODO - This is where it fails
                    driver.switchTo().window(popupWindow);
                    Thread.sleep(500);
                    System.out.println(driver.getTitle());
                    popupTitle = driver.getTitle();

                    //Close the Popup Window
                    driver.close();
                } catch (Exception e) {
                    throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName()
                            + "...Error: " + e.getMessage());
                }//end catch
            }//end if
        }//end while
    } catch(Exception e) {
        throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName()
                + "...Error switching to and closing popup: " + e.getMessage());
    }//end catch

    //Switch to parent window.
    driver.switchTo().window(parentWindow);
    driver.manage().window().maximize();
    Thread.sleep(2000);

    //Verification of title
    Assert.assertTrue(popupTitle.contains("MYTITLE"));

CI Info:

JDK: 1.8.0_66

Java: Version 8

IE: 11

Other similar issues that didn't answer my question:

switchWindow does not work in IE

How to switch to the new browser window, which opens after click on the button?

How to exit a while loop after a certain time?

Any help or feedback is greatly appreciated!

1 Answers1

0

Using the below code I tested against both my private code and the http://demo.guru99.com/popup.php demo pop up site. My code works fine against that site, but fails on my private site. I implementing wait periods, however I don't believe it's a timing issue. I simply believe the popup isn't compatible on IE with Selenium on my private site. Posting my code that works on the dummy site as an answer in case someone else has similar issues as the code is valid.

    //Retrieve parent window handle
    parentWindow = driver.getWindowHandle();        

    //Loop through the window handles until you are on the popup window
    for(String popupWindow : driver.getWindowHandles()){
       if (driver.switchTo().window(popupWindow).getTitle().equals(myTitle)) {
         break;
       } 
       else {
          driver.switchTo().window(parentWindow);
       } 
    }

    //Store the title
    popupTitle = driver.getTitle();

    //Close the Popup Window
    driver.close();     

    //switch to parent window.
    driver.switchTo().window(parentWindow);
    driver.manage().window().maximize();

    //Verification of title
    Assert.assertTrue(popupTitle.toUpperCase().contains(myTitle));