c# selenium selenium-webdriver webdriver window-handles

c# - La mejor manera de realizar un seguimiento e iterar a través de pestañas y ventanas usando WindowHandles usando Selenium



selenium-webdriver webdriver (1)

Tienes bastante razón cuando dices:

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

En una discusión, Simon mencionó claramente que:

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.

Por lo tanto, WebDriverWait un WebDriverWait y luego recopilaremos los identificadores de las ventanas cada vez que abrimos una nueva pestaña / ventana y finalmente iteraremos a través de los identificadores de las ventanas y switchTo().window(newly_opened) según sea necesario:

Ajuste el Test Environment si es necesario [Mi configuración - Selenium : 3.5.3 , IEDriverServer : 3.5.0.0 (64 bits) , 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"); } }

Salida de consola:

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

Estamos trabajando con el controlador web Selenium para realizar pruebas de IU para Internet Explorer 11. En la aplicación probada, aparecen varias pantallas emergentes. En varias pruebas terminamos con tres ventanas de navegador, así que también tres Driver.WindowHandles. Para cambiar de un WindowHandle a otro, esperábamos que Driver.WindowHandles se ordenara como las ventanas más antiguas primero y las ventanas más nuevas al final. Pero este no es el caso: ¡es totalmente aleatorio!

Debido a que un identificador de ventana es un GUID, terminamos creando un diccionario con el GUID de WindowHandle como clave con el valor del tipo de página que se carga en la ventana del navegador. Pero esto también resulta en mantener el diccionario al cerrar una ventana, por ejemplo.

Esto parece ser mucho trabajo para un asunto tan simple. ¿Hay una mejor solución para esto?