diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/ExplicitWaitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/ExplicitWaitLiveTest.java new file mode 100644 index 0000000000..65943fbf5e --- /dev/null +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/ExplicitWaitLiveTest.java @@ -0,0 +1,79 @@ +package com.baeldung.selenium.wait; + +import io.github.bonigarcia.wdm.WebDriverManager; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.ElementNotInteractableException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +final class ExplicitWaitLiveTest { + + private static WebDriver driver; + private static WebDriverWait wait; + private static final int TIMEOUT = 10; + + private static final By LOCATOR_ABOUT = By.xpath("//a[starts-with(., 'About')]"); + private static final By LOCATOR_ABOUT_BAELDUNG = By.xpath("//h3[normalize-space()='About Baeldung']"); + private static final By LOCATOR_ABOUT_HEADER = By.xpath("//h1"); + + private static void setupChromeDriver() { + WebDriverManager.chromedriver().setup(); + final ChromeOptions options = new ChromeOptions(); + options.addArguments("--remote-allow-origins=*"); + driver = new ChromeDriver(options); + options(); + } + + private static void options() { + driver.manage().window().maximize(); + } + + @BeforeEach + public void init() { + setupChromeDriver(); + wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT)); + } + + @Test + void givenPage_whenNavigatingWithoutExplicitWait_thenElementNotInteractable() { + driver.navigate().to("https://www.baeldung.com/"); + + driver.findElement(LOCATOR_ABOUT).click(); + + assertThrows(ElementNotInteractableException.class, () -> driver.findElement(LOCATOR_ABOUT_BAELDUNG).click()); + } + + @Test + void givenPage_whenNavigatingWithExplicitWait_thenOK() { + final String expected = "About Baeldung"; + driver.navigate().to("https://www.baeldung.com/"); + + driver.findElement(LOCATOR_ABOUT).click(); + wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_BAELDUNG)); + + driver.findElement(LOCATOR_ABOUT_BAELDUNG).click(); + wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_HEADER)); + + final String actual = driver.findElement(LOCATOR_ABOUT_HEADER).getText(); + assertEquals(expected, actual); + } + + @AfterEach + void teardown() { + if (driver != null) { + driver.quit(); + driver = null; + } + } +} \ No newline at end of file diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/FluentWaitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/FluentWaitLiveTest.java new file mode 100644 index 0000000000..c41e5619ac --- /dev/null +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/FluentWaitLiveTest.java @@ -0,0 +1,83 @@ +package com.baeldung.selenium.wait; + +import io.github.bonigarcia.wdm.WebDriverManager; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.ElementNotInteractableException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.FluentWait; +import org.openqa.selenium.support.ui.Wait; + +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +final class FluentWaitLiveTest { + + private static WebDriver driver; + private static Wait wait; + private static final int TIMEOUT = 10; + private static final int POLL_FREQUENCY = 250; + + private static final By LOCATOR_ABOUT = By.xpath("//a[starts-with(., 'About')]"); + private static final By LOCATOR_ABOUT_BAELDUNG = By.xpath("//h3[normalize-space()='About Baeldung']"); + private static final By LOCATOR_ABOUT_HEADER = By.xpath("//h1"); + + private static void setupChromeDriver() { + WebDriverManager.chromedriver().setup(); + final ChromeOptions options = new ChromeOptions(); + options.addArguments("--remote-allow-origins=*"); + driver = new ChromeDriver(options); + options(); + } + + private static void options() { + driver.manage().window().maximize(); + } + + @BeforeEach + public void init() { + setupChromeDriver(); + wait = new FluentWait<>(driver) + .withTimeout(Duration.ofSeconds(TIMEOUT)) + .pollingEvery(Duration.ofMillis(POLL_FREQUENCY)); + } + + @Test + void givenPage_whenNavigatingWithoutFluentWait_thenElementNotInteractable() { + driver.navigate().to("https://www.baeldung.com/"); + + driver.findElement(LOCATOR_ABOUT).click(); + + assertThrows(ElementNotInteractableException.class, () -> driver.findElement(LOCATOR_ABOUT_BAELDUNG).click()); + } + + @Test + void givenPage_whenNavigatingWithFluentWait_thenOK() { + final String expected = "About Baeldung"; + driver.navigate().to("https://www.baeldung.com/"); + + driver.findElement(LOCATOR_ABOUT).click(); + wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_BAELDUNG)); + + driver.findElement(LOCATOR_ABOUT_BAELDUNG).click(); + wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_HEADER)); + + final String actual = driver.findElement(LOCATOR_ABOUT_HEADER).getText(); + assertEquals(expected, actual); + } + + @AfterEach + void teardown() { + if (driver != null) { + driver.quit(); + driver = null; + } + } +} \ No newline at end of file diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/ImplicitWaitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/ImplicitWaitLiveTest.java new file mode 100644 index 0000000000..86c401e13a --- /dev/null +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/wait/ImplicitWaitLiveTest.java @@ -0,0 +1,63 @@ +package com.baeldung.selenium.wait; + + +import io.github.bonigarcia.wdm.WebDriverManager; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; + +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +final class ImplicitWaitLiveTest { + + private static WebDriver driver; + private static final int TIMEOUT = 10; + + private static final By LOCATOR_ABOUT = By.xpath("//a[starts-with(., 'About')]"); + private static final By LOCATOR_ABOUT_BAELDUNG = By.xpath("//h3[normalize-space()='About Baeldung']"); + private static final By LOCATOR_ABOUT_HEADER = By.xpath("//h1"); + + private static void setupChromeDriver() { + WebDriverManager.chromedriver().setup(); + final ChromeOptions options = new ChromeOptions(); + options.addArguments("--remote-allow-origins=*"); + driver = new ChromeDriver(options); + options(); + } + + private static void options() { + driver.manage().window().maximize(); + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(TIMEOUT)); + } + + @BeforeEach + public void init() { + setupChromeDriver(); + } + + @Test + void givenPage_whenNavigatingWithImplicitWait_ThenOK() { + final String expected = "About Baeldung"; + driver.navigate().to("https://www.baeldung.com/"); + + driver.findElement(LOCATOR_ABOUT).click(); + driver.findElement(LOCATOR_ABOUT_BAELDUNG).click(); + + final String actual = driver.findElement(LOCATOR_ABOUT_HEADER).getText(); + assertEquals(expected, actual); + } + + @AfterEach + void teardown() { + if (driver != null) { + driver.quit(); + driver = null; + } + } +} \ No newline at end of file