BAEL-6117 Explicit Wait vs Implicit Wait in Selenium Webdriver (#13799)

This commit is contained in:
AndiCover 2023-04-13 02:39:41 +02:00 committed by GitHub
parent 3aaf85dcca
commit 725deaa8d5
3 changed files with 225 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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<WebDriver> 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;
}
}
}

View File

@ -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;
}
}
}