Windows Handling
Windows Handling
package windowHandle;
BaseTest.setup();
driver.get("https://www.amazon.in");
BaseTest.tearDown();
BaseTest.setup();
try {
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
BaseTest.tearDown();
}
3. Closing Windows
import java.time.Duration;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
// Wait for the button to be clickable and open two new windows
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
((JavascriptExecutor)
driver).executeScript("arguments[0].scrollIntoView(true);", windowButton);
((JavascriptExecutor) driver).executeScript("arguments[0].click();",
windowButton);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
((JavascriptExecutor) driver).executeScript("arguments[0].click();",
windowButton);
wait.until(ExpectedConditions.numberOfWindowsToBe(3));
// Get all window handles
Set<String> allWindows = driver.getWindowHandles();
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
}
BaseTest.tearDown();
BaseTest.setup();
driver.get("https://demoqa.com/browser-windows");
BaseTest.setup();
driver.get("https://www.amazon.in");
Thread.sleep(2000);
link.sendKeys(Keys.CONTROL, Keys.RETURN);
// Get all window handles (tabs are treated as windows)
Set<String> allHandles = driver.getWindowHandles();
BaseTest.tearDown();
BaseTest.setup();
driver.get("https://demoqa.com/browser-windows");
wait.until(ExpectedConditions.elementToBeClickable(windowButton));
BaseTest.tearDown();
WindowHandlingExamples.tearDown();
}
}
WindowHandlingExamples.tearDown();
}
}