It can happen that the element you want to check only appears for a fraction of a second. This can produce false failures when the element appears in between the standard 500ms when WebDriver checks the wait condition. What you can do is to set up a FluentWait object with a specific polling time. It works almost the same as other kinds of wait functions in WebDriver.

The code is the following:



	Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
				.withTimeout(20, java.util.concurrent.TimeUnit.SECONDS)
				.pollingEvery(200, java.util.concurrent.TimeUnit.MILLISECONDS)
				.ignoring(NoSuchElementException.class);
		
		Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>() {
			
			@Override
			public WebElement apply(WebDriver driver) {
				return driver.findElement(exportSnackbarCss);
			}		
		};
		
		wait.until(function);


Let dissect the important differences:
`.withTimeout(20, java.util.concurrent.TimeUnit.SECONDS)` – set up the 20second timeout

`.pollingEvery(200, java.util.concurrent.TimeUnit.MILLISECONDS)` – here we set up how often WebDriver should check the condition defined in the overwritten apply function. WebDriver’s default is 500ms. Here we had to use 200 ms.
Make sure you decrease this time only when you must, WebDriver function calls are expensive and can cause heavy network load especially if you run multiple test suites in parallel.

`.ignoring(NoSuchElementException.class);` – this makes the test running after the first check. In our case we use driver.findElement() to get our element. This throws a NoSuchElementException if the element is not found, which could be the case if the element doesn’t appear during our first check, by ignoring it we will have a standard wait function.

In WebDriver 3.3 these functions are removed to force people to use java 1.8 lambda expressions.

Posted By Tihomir Turzai

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>