Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example about how to override the Explicitly Wait using Selenium WebDriver
public class ExplicitlyWaitExampleTest {
private WebDriver driver;
private WebDriverWait wait;
@BeforeClass
public void preCondition() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("https://codepen.io/eliasnogueira/full/MWywxVr");
driver.switchTo().frame("result");
}
@BeforeMethod
public void beforeEachTest() {
wait = new WebDriverWait(driver, Duration.ofSeconds(5));
}
@Test
public void thisTestWillPass() {
driver.findElement(By.id("trigger_4")).click();
By elementVisibleAfter4Seconds = By.id("after_4");
wait.until(ExpectedConditions.visibilityOfElementLocated(elementVisibleAfter4Seconds));
}
@Test(priority = 2)
public void thisTestWillPassBecauseOfTheTimeoutOverride() {
driver.findElement(By.id("trigger_13")).click();
By elementVisibleAfter13Seconds = By.id("after_13");
// overriding the default timeout only for this wait
wait.
withTimeout(Duration.ofSeconds(15)).
withMessage("timeout while waiting for upload to finish").
until(ExpectedConditions.visibilityOfElementLocated(elementVisibleAfter13Seconds));
}
@Test(priority = 3)
public void thisTestWillFail() {
driver.findElement(By.id("trigger_6")).click();
By elementVisibleAfter6Seconds = By.id("after_6");
wait.until(ExpectedConditions.visibilityOfElementLocated(elementVisibleAfter6Seconds));
}
@AfterClass
public void postCondition() {
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment