使用 Selenium 使用 WindowHandles 跟踪和迭代选项卡和窗口的最佳方法 [英] Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium

查看:42
本文介绍了使用 Selenium 使用 WindowHandles 跟踪和迭代选项卡和窗口的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在与 Selenium webdriver 合作,为 Internet Explorer 11 进行 UI 测试.在经过测试的 Web 应用程序中,会弹出几个屏幕.在几个测试中,我们最终得到了三个浏览器窗口,还有三个 Driver.WindowHandles.为了从一个 WindowHandle 切换到另一个,我们希望 Driver.WindowHandles 将按照最旧的窗口在前,最新的窗口在后进行排序.但事实并非如此:这完全是随机的!

因为 windowhandle 是一个 GUID,所以我们最终创建了一个字典,以 WindowHandle GUID 作为键,其中包含浏览器窗口中加载的页面类型的值.但这也会导致在关闭窗口时维护字典.

对于这么简单的事情,这似乎是很多工作.有没有更好的解决方案?

解决方案

你说得非常正确:

<块引用>

WindowHandles 将按照最旧的窗口在前,最新的窗口在后进行排序.但事实并非如此:它完全是随机的!

在一次讨论中,Simon 明确提到:

<块引用>

虽然用于存储句柄列表的数据类型可以按插入排序,但 WebDriver 实现迭代窗口句柄以插入它们的顺序不需要稳定.顺序是任意的.

因此,我们将引入一个 WebDriverWait 然后在每次打开新标签/窗口时收集窗口句柄,最后遍历窗口句柄和 switchTo().window(newly_opened) 根据需要:

<块引用>

如果需要请调整测试环境 [我的配置 - Selenium: 3.5.3, IEDriverServer:3.5.0.0(64位)IE:v10.0]

Java:

打包演示;导入 java.util.Iterator;导入 java.util.Set;导入 org.openqa.selenium.By;导入 org.openqa.selenium.JavascriptExecutor;导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.chrome.ChromeDriver;导入 org.openqa.selenium.firefox.FirefoxDriver;导入 org.openqa.selenium.ie.InternetExplorerDriver;导入 org.openqa.selenium.support.ui.ExpectedConditions;导入 org.openqa.selenium.support.ui.WebDriverWait;公共类 NEW_TAB_Handling {公共静态无效主(字符串 [] args){System.setProperty("webdriver.ie.driver", "C:\Utility\BrowserDrivers\IEDriverServer.exe");WebDriver 驱动程序 = 新 InternetExplorerDriver();driver.get("http://www.google.com");String first_tab = driver.getWindowHandle();System.out.println("在谷歌上工作");((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");WebDriverWait wait = new WebDriverWait(driver,5);wait.until(ExpectedConditions.numberOfWindowsToBe(2));设置<字符串>s1 = driver.getWindowHandles();迭代器<字符串>i1 = s1.iterator();而(i1.hasNext()){String next_tab = i1.next();如果 (!first_tab.equalsIgnoreCase(next_tab)){driver.switchTo().window(next_tab);System.out.println("在 Facebook 上工作");}}String second_tab = driver.getWindowHandle();((JavascriptExecutor) driver).executeScript("window.open('http://youtube.com/');");wait.until(ExpectedConditions.numberOfWindowsToBe(3));设置<字符串>s2 = driver.getWindowHandles();迭代器<字符串>i2 = s2.iterator();而(i2.hasNext()){String next_tab = i2.next();如果 (!first_tab.equalsIgnoreCase(next_tab) && !second_tab.equalsIgnoreCase(next_tab)){driver.switchTo().window(next_tab);System.out.println("在Youtube上工作");}}驱动程序退出();System.out.println("退出WebDriver 实例");}}

控制台输出:

在 Google 上工作在 Facebook 上工作在 YouTube 上工作退出 WebDriver 实例

<小时>

尾声

您可以在以下位置找到基于 的讨论在新标签页 Selenium + Python 中打开网页>

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?

解决方案

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

这篇关于使用 Selenium 使用 WindowHandles 跟踪和迭代选项卡和窗口的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆