5

We are working with Selenium webdriver to make UI tests for Internet Explorer 11.  In the tested webapplication there are several screens popping up. In several tests we end up with three browserswindows, so also three Driver.WindowHandles.  To switch from one WindowHandle to the other we expected that Driver.WindowHandles would be sorted like the oldest windows first and the newest windows last. But this is not the case: It is totaly random! 

Because a windowhandle is a GUID we ended up creating a dictionary with the WindowHandle GUID as key with the value of the type of page which is loaded in the browserwindow. But this also results in maintaining the dictionary when closing a window for instance. 

This seems to be a lot of work for such a simple matter. Is there a better solution for this?

2
  • I am not that familiar with C# but can construct an answer through Java. Would that cater to your Question? Commented Sep 18, 2017 at 7:11
  • Yes, no problem. Commented Sep 18, 2017 at 17:36

1 Answer 1

4

You are pretty correct when you say:

WindowHandles would be sorted like the oldest windows first and the newest windows last. But this is not the case: It is totaly random!

In a discussion, Simon clearly mentioned that:

While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. The ordering is arbitrary.

So, we will induce an WebDriverWait and then collect the window handles every time we open a new tab/window and finally iterate through the window handles and switchTo().window(newly_opened) as required:

Please adjust the Test Environment if needed [My configuration - Selenium: 3.5.3, IEDriverServer: 3.5.0.0 (64-bit), IE: v10.0]

Java:

package demo;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class NEW_TAB_Handling {

    public static void main(String[] args)  {


        System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
        WebDriver driver =  new InternetExplorerDriver();
        driver.get("http://www.google.com");
        String first_tab = driver.getWindowHandle();
        System.out.println("Working on Google");
        ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
        WebDriverWait wait = new WebDriverWait(driver,5);
        wait.until(ExpectedConditions.numberOfWindowsToBe(2));
        Set<String> s1 = driver.getWindowHandles();
        Iterator<String> i1 = s1.iterator();
        while(i1.hasNext())
        {
            String next_tab = i1.next();
            if (!first_tab.equalsIgnoreCase(next_tab))
            {
                driver.switchTo().window(next_tab);

                System.out.println("Working on Facebook");
            }
        }
        String second_tab = driver.getWindowHandle();
        ((JavascriptExecutor) driver).executeScript("window.open('http://youtube.com/');");
        wait.until(ExpectedConditions.numberOfWindowsToBe(3));
        Set<String> s2 = driver.getWindowHandles();
        Iterator<String> i2 = s2.iterator();
        while(i2.hasNext())
        {
            String next_tab = i2.next();
            if (!first_tab.equalsIgnoreCase(next_tab) && !second_tab.equalsIgnoreCase(next_tab))
            {
                driver.switchTo().window(next_tab);
                System.out.println("Working on Youtube");
            }
        }
        driver.quit();
        System.out.println("Quit the WebDriver instance");
    }
}

Console Output:

Working on Google
Working on Facebook
Working on Youtube
Quit the WebDriver instance

Outro

You can find the based discussion in Open web in new tab Selenium + Python

4
  • Thank you for your answer. But how does it work when there is a third or fourth window? Commented Sep 21, 2017 at 18:21
  • Hi Debanjan, my initial question was about 3 windowhandles. So i cannot accept your answer as the answer. I understand you can switch between two windowhandles but the tricky part is when there are more then two handles. Commented Sep 21, 2017 at 18:45
  • @KoenMeijer Updated my Answer with How to handle 3 windowhandles. Let me know if that Answers your Question. Commented Sep 22, 2017 at 7:40
  • 1
    Thank you Debanjan, so you also have to keep track of all the WindowHandles in an object. And when you close a window you also have to remove the winodwhandle. That's seems to me a lot of work for such a simple matter. Isn't there another solution for this problem? Commented Sep 23, 2017 at 14:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.