diff --git a/src/test/java/com/agile611/testng/webdriver/MultipleWindowsTest.java b/src/test/java/com/agile611/testng/webdriver/MultipleWindowsTest.java index c72df27..b47ac32 100644 --- a/src/test/java/com/agile611/testng/webdriver/MultipleWindowsTest.java +++ b/src/test/java/com/agile611/testng/webdriver/MultipleWindowsTest.java @@ -1,54 +1,78 @@ package com.agile611.testng.webdriver; -import org.openqa.selenium.By; -import org.testng.annotations.Test; - +import java.time.Duration; import java.util.Set; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; +import org.openqa.selenium.By; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; +import org.testng.annotations.Test; public class MultipleWindowsTest extends BaseTest { @Test public void multipleWindows() { driver.get("http://the-internet.herokuapp.com/windows"); + + String originalWindow = driver.getWindowHandle(); + driver.findElement(By.cssSelector(".example a")).click(); - Object[] allWindows = driver.getWindowHandles().toArray(); - driver.switchTo().window(allWindows[0].toString()); - assertThat(driver.getTitle(), is(not("New Window"))); - driver.switchTo().window(allWindows[1].toString()); - assertThat(driver.getTitle(), is("New Window")); + + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + wait.until(ExpectedConditions.numberOfWindowsToBe(2)); + + String newWindow = ""; + for (String handle : driver.getWindowHandles()) { + if (!handle.equals(originalWindow)) { + newWindow = handle; + } + } + + // Verificar ventana original + driver.switchTo().window(originalWindow); + assertThat(driver.getTitle(), is(not(equalTo("New Window")))); + + // Verificar nueva ventana + driver.switchTo().window(newWindow); + assertThat(driver.getTitle(), is(equalTo("New Window"))); + + // ✅ Cerrar la ventana nueva y volver a la original + driver.close(); + driver.switchTo().window(originalWindow); } @Test public void multipleWindowsRedux() { driver.get("http://the-internet.herokuapp.com/windows"); - // Get initial window handle String firstWindow = driver.getWindowHandle(); - // Create a newWindow variable String newWindow = ""; - // Trigger new window to open + driver.findElement(By.cssSelector(".example a")).click(); - // Grab all window handles + + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + wait.until(ExpectedConditions.numberOfWindowsToBe(2)); + Set allWindows = driver.getWindowHandles(); - // Iterate through window handles collection - // Find the new window handle, storing it in the newWindow variable for (String window : allWindows) { if (!window.equals(firstWindow)) { newWindow = window; } } - // Switch to the first window & verify driver.switchTo().window(firstWindow); assertThat(driver.getTitle(), is(not(equalTo("New Window")))); - // Switch to the new window & verify driver.switchTo().window(newWindow); assertThat(driver.getTitle(), is(equalTo("New Window"))); - } -} + // ✅ Cerrar la ventana nueva y volver a la original + driver.close(); + driver.switchTo().window(firstWindow); + } +} \ No newline at end of file