Add code examples for "Opening a New Tab Using Selenium WebDriver in Java" (#14001)

Add code examples for "Opening a New Tab Using Selenium WebDriver in Java"
This commit is contained in:
Dmytro Budym 2023-05-14 04:59:37 +02:00 committed by GitHub
parent e1272ca8fe
commit 0a634dfa03
1 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package com.baeldung.selenium.tabs;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;
public class SeleniumOpenNewTabIntegrationTest {
private WebDriver driver;
private static final int TIMEOUT = 10;
private static final int EXPECTED_TABS_COUNT = 2;
@BeforeMethod
public void initDriver() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
@Test
public void whenUseTabsApiOpenWindow_thenNewTabOpened() {
driver.switchTo().newWindow(WindowType.TAB);
waitTabsCount(EXPECTED_TABS_COUNT);
}
@Test
public void whenExecuteOpenWindowJsScript_thenNewTabOpened() {
((JavascriptExecutor) driver).executeScript("window.open()");
waitTabsCount(EXPECTED_TABS_COUNT);
}
@AfterMethod
public void closeBrowser() {
driver.quit();
}
private void waitTabsCount(int tabsCount) {
new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT))
.withMessage("Tabs count should be: " + tabsCount)
.until(ExpectedConditions.numberOfWindowsToBe(tabsCount));
}
}