Is there a set that we get in driver driver.getWindowhandles () to save the order

I have confusion as to whether the set that returns the windows in selenium preserves the order in which the windows open, I mean that the first window will be in the first position, the next window is open in the next position, etc.

This is the code:

Set<String> handles = driver.getWindowhandles()
+4
source share
3 answers

First, consider a method call getWindowhandles().

getWindowhandles

So, it is clear that the method getWindowhandles()returns a Settype String, so we are pretty correct when we mention:

Set<String> handles = driver.getWindowhandles();

Now I would keep my answer limited Mozilla Firefoxsince it follows a separate line of specification c W3C Specs.

WebDriver W3C Specification clearly mentions the following:

The Get Window Handles command returns a list of window handles for every open top-level browsing context. The order in which the window handles are returned is arbitrary.

WebDriver W3C Specification :

Each browsing context has an associated window handle which uniquely identifies it. This must be a String and must not be "current".

The web window identifier is the string constant "window-fcc6-11e5-b4f8-330a88ab9d7f".

, , .

:

, Selenium focus , , Set window handle driver.switchTo().window(win_handle); :

String parent_window = driver.getWindowHandle();
System.out.println("Parent Window ID is : "+parent_window);
element2.click();   // WebElement which opens a new window
Set<String> allWindows = driver.getWindowHandles();
for(String child_1:allWindows)
    if(!parent_window.equalsIgnoreCase(child_1))
        driver.switchTo().window(child_1);
System.out.println(driver.getTitle());
String child1_window = driver.getWindowHandle();
System.out.println("Child 1 Window ID is : "+child1_window);
driver.findElement(By.linkText("Link")).click();    //Link which opens a new window
Set<String> all_Windows = driver.getWindowHandles();
for(String child_2:all_Windows)
    if(!parent_window.equalsIgnoreCase(child_2) && !child1_window.equalsIgnoreCase(child_2))
        driver.switchTo().window(child_2);
String child2_window = driver.getWindowHandle();
System.out.println("Child 2 Window ID is : "+child2_window);
0

Set - interface. , - SortedSet,

0

. , - . WebDriver protocol , :

The Get Window Handles command returns a list of window handles for each open top-level view context. The order in which the window is located descriptors are returned randomly.

This is probably why all the descriptors are placed in Setso that you cannot access the descriptor by index.

If you have only 2 windows, just switch to the one that is not the current window:

Set<String> handles = driver.getWindowHandles();
handles.remove(driver.getWindowHandle());
driver.switchTo().window(handles.iterator().next());

But if you have more than two windows, keep an eye on each new window or repeat each window until you get the expected one:

Set<String> handles = driver.getWindowHandles();
handles.remove(driver.getWindowHandle());

for (String hwnd : handles) {
    driver.switchTo().window(hwnd);

    if (driver.getCurrentUrl().contains(...)) {
        ...
    }
}
0
source

Source: https://habr.com/ru/post/1684946/


All Articles