Synchronizing a test with custom-expected conditions
With the explicit wait mechanism, we can also build custom-expected conditions along with common conditions using the ExpectedConditions
class. This comes in handy when a wait cannot be handled with a common condition supported by the ExpectedConditions
class.
In this recipe, we will explore how to create a custom condition.
How to do it...
We will create a test that will create a wait until an element appears on the page using the ExpectedCondition
class as follows:
@Test public void testExplicitWait() { WebDriver driver = new FirefoxDriver(); // Launch the sample Ajax application driver.get("http://cookbook.seleniumacademy.com/AjaxDemo.html"); try { driver.findElement(By.linkText("Page 4")).click(); WebElement message = new WebDriverWait(driver, 5) .until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver d) { return d.findElement(By.id("page4")); } ...