|
| 1 | +package dev.selenium.waits; |
| 2 | + |
| 3 | +import dev.selenium.BaseChromeTest; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.openqa.selenium.By; |
| 7 | +import org.openqa.selenium.ElementNotInteractableException; |
| 8 | +import org.openqa.selenium.Keys; |
| 9 | +import org.openqa.selenium.NoSuchElementException; |
| 10 | +import org.openqa.selenium.WebDriver; |
| 11 | +import org.openqa.selenium.WebElement; |
| 12 | +import org.openqa.selenium.interactions.Actions; |
| 13 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 14 | +import org.openqa.selenium.support.ui.FluentWait; |
| 15 | +import org.openqa.selenium.support.ui.Wait; |
| 16 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | + |
| 20 | +public class WaitsTest extends BaseChromeTest { |
| 21 | + @Test |
| 22 | + public void fails() { |
| 23 | + driver.get("https://www.selenium.dev/selenium/web/dynamic.html"); |
| 24 | + driver.findElement(By.id("adder")).click(); |
| 25 | + |
| 26 | + Assertions.assertThrows(NoSuchElementException.class, () -> { |
| 27 | + driver.findElement(By.id("box0")); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void sleep() throws InterruptedException { |
| 33 | + driver.get("https://www.selenium.dev/selenium/web/dynamic.html"); |
| 34 | + driver.findElement(By.id("adder")).click(); |
| 35 | + |
| 36 | + Thread.sleep(1000); |
| 37 | + |
| 38 | + WebElement added = driver.findElement(By.id("box0")); |
| 39 | + |
| 40 | + Assertions.assertEquals("redbox", added.getDomAttribute("class")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void implicit() { |
| 45 | + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1)); |
| 46 | + driver.get("https://www.selenium.dev/selenium/web/dynamic.html"); |
| 47 | + driver.findElement(By.id("adder")).click(); |
| 48 | + |
| 49 | + WebElement added = driver.findElement(By.id("box0")); |
| 50 | + |
| 51 | + Assertions.assertEquals("redbox", added.getDomAttribute("class")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void explicit() { |
| 56 | + driver.get("https://www.selenium.dev/selenium/web/dynamic.html"); |
| 57 | + WebElement revealed = driver.findElement(By.id("revealed")); |
| 58 | + Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(1)); |
| 59 | + |
| 60 | + driver.findElement(By.id("reveal")).click(); |
| 61 | + wait.until(d -> revealed.isDisplayed()); |
| 62 | + |
| 63 | + revealed.sendKeys("Displayed"); |
| 64 | + Assertions.assertEquals("Displayed", revealed.getDomProperty("value")); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void explicitWithOptions() { |
| 69 | + driver.get("https://www.selenium.dev/selenium/web/dynamic.html"); |
| 70 | + WebElement revealed = driver.findElement(By.id("revealed")); |
| 71 | + Wait<WebDriver> wait = new FluentWait<>(driver) |
| 72 | + .withTimeout(Duration.ofSeconds(1)) |
| 73 | + .pollingEvery(Duration.ofMillis(300)) |
| 74 | + .ignoring(ElementNotInteractableException.class); |
| 75 | + |
| 76 | + driver.findElement(By.id("reveal")).click(); |
| 77 | + wait.until(d -> { |
| 78 | + revealed.sendKeys("Displayed"); |
| 79 | + return true; |
| 80 | + }); |
| 81 | + |
| 82 | + Assertions.assertEquals("Displayed", revealed.getDomProperty("value")); |
| 83 | + } |
| 84 | +} |
0 commit comments