Koe*_*jer 5 c# selenium window-handles selenium-webdriver webdriverwait
我们正在与 Selenium webdriver 合作,对 Internet Explorer 11 进行 UI 测试。\xc2\xa0\n在测试的 Web 应用程序中,会弹出几个屏幕。在一些测试中,我们最终得到了三个浏览器窗口,因此也得到了三个 Driver.WindowHandles。\xc2\xa0\n要从一个 WindowHandle 切换到另一个,我们预计 Driver.WindowHandles 将按照最旧的窗口在前、最新的窗口在后的方式进行排序。但事实并非如此:它完全是随机的!\xc2\xa0
\n\n因为窗口句柄是一个 GUID,所以我们最终创建了一个字典,以 WindowHandle GUID 作为键,其值为浏览器窗口中加载的页面类型。\n但这也会导致在关闭窗口时维护字典。\ xc2\xa0
\n\n对于这么简单的事情来说,这似乎是一个很大的工作量。对此有更好的解决方案吗?
\n当你说:
WindowHandles 将按照最旧的窗口在前、最新的窗口在后的方式进行排序。但事实并非如此:它完全是随机的!
在一次讨论中,Simon 明确提到:
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)需要迭代窗口句柄:
Test Environment如果需要,请调整[我的配置 -Selenium: 3.5.3 ,IEDriverServer: 3.5.0.0 (64-bit) ,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");
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
Working on Google
Working on Facebook
Working on Youtube
Quit the WebDriver instance
Run Code Online (Sandbox Code Playgroud)
您可以在新选项卡中的 Open web 中找到基于python的讨论Selenium + Python
| 归档时间: |
|
| 查看次数: |
4319 次 |
| 最近记录: |