My program navigates to different URLs using multiple threads. The way it works is that my program opens max two tabs per browser and then (once the limit is reached) it opens the next set of URLs in a new browser (not just a new tab). Each time I open a new tab (or browser) I retrieve the current window handle name. However, I noticed that when the program opens the second set of URLs (in test program there are a total of 3 URLs so second browser will contain only 1 URL/tab) the window handle matches the window handle of the first tab opened from the first set. It seems like Selenium is reusing window handle ids - does that make sense?
Here's my code:
WebDriver driver = getNewFirefoxWebDriver();
if (driver != null) {
for (int sublistCounter = 0; sublistCounter < subList.size(); sublistCounter++) {
String url = sublist.get(sublistCouter).getURL();
String currentURLWindowHandle = "";
if (sublistCounter > 0 ) {
// Opens URL via JS
navigateToURLInNewTabViaJS(driver, url);
// Returns window handle via getWindowHandle()
currentURLWindowHandle = getCurrentWindowHandle(driver);
}
else if (sublistCounter == 0) {
// Navigates to URL via driver.navigate.to() as this is the first URL in set
currentURLWindowHandle = navToPage(driver, url);
}
Set<String> windowHandles = getCurrentWindowHandles(driver);
// PRINTS: [4294967297] (1) [OK]
// PRINTS: [4294967297, 4294967317] (2) [OK]
// PRINTS: [4294967297] (1) (NOT OK -- Why?)
System.out.println("Window Handles: " + windowHandles.toString() + " (" + windowHandles.size() + ")");
System.out.println("Adding Window Handle: " + currentURLWindowHandle + " of URL: " + url + " to tracker");
}
Set<String> windowHandles = driver.getWindowHandles();
// PRINTS: Window Handle Size: 2 (makes sense becauzse first set contains 2 urls)
// PRINTS: Window Handle Size: 1 (makes sense bec second set contains only 1 URL)
System.out.println("Window Handle Size: " + windowHandles.size());
}
As you can see in above code, the first tab in second browser (second browser only has one tab since we are dealing with 3 urls total) has the same window handle as the first tab in the first browser.
I was under the impression that Selenium would generate a new window handle for each tab/browser. Why does it seem as Selenium is reusing window handles?
Thanks