Skip to content

Instantly share code, notes, and snippets.

@eliasnogueira
Created August 20, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasnogueira/0034a9535298211b79b92bc2a5a9824d to your computer and use it in GitHub Desktop.
Save eliasnogueira/0034a9535298211b79b92bc2a5a9824d to your computer and use it in GitHub Desktop.
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