使用Selenium使用WindowHandles跟踪和迭代选项卡和窗口的最佳方法

时间:2017-09-16 07:44:11

标签: c# selenium selenium-webdriver webdriver window-handles

我们正在使用Selenium webdriver为Internet Explorer 11进行UI测试。 在测试的Web应用程序中,会弹出几个屏幕。在几个测试中,我们最终得到三个browserwindows,还有三个Driver.WindowHandles。 要从一个WindowHandle切换到另一个WindowHandle,我们预计Driver.WindowHandles将像最旧的窗口一样排序,最新的窗口最后。但事实并非如此:它完全是随机的!

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

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

1 个答案:

答案 0 :(得分:2)

当你说:

时,你是非常正确的
  

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

在讨论中,西蒙明确提到:

  

<强> 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.

因此,我们将引出 WebDriverWait ,然后每次打开新的选项卡/窗口时收集窗口句柄,最后遍历窗口句柄并switchTo().window(newly_opened) as需要:

  

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

爪哇:

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");
    }
}

控制台输出:

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