From 0a634dfa0322527c2850357f5c01d5860a14a160 Mon Sep 17 00:00:00 2001 From: Dmytro Budym <46810751+dbudim@users.noreply.github.com> Date: Sun, 14 May 2023 04:59:37 +0200 Subject: [PATCH] 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" --- .../SeleniumOpenNewTabIntegrationTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/tabs/SeleniumOpenNewTabIntegrationTest.java diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/tabs/SeleniumOpenNewTabIntegrationTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/tabs/SeleniumOpenNewTabIntegrationTest.java new file mode 100644 index 0000000000..4316fedebf --- /dev/null +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/tabs/SeleniumOpenNewTabIntegrationTest.java @@ -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)); + } + +}