Fluent wait in selenium is one type of explicit wait in which selenium script will wait for maximum wait time until the expected condition satisfied and verifying the condition frequently before throwing an exception like ElementNotVisibleException.
In simple language, Fluent wait is checking the Web Element in regular interval before throwing the expected condition.
Here the time script will check the WebElement frequently in regular time is known as Frequency. Frequency is also known as polling period.
This is recommended to define default / equal polling period throughout the script which will make the script consistency.
You may think that why fluent wait is required if it is mostly equal to explicit wait. Let’s understand this with a simple scenario.
Let’s say because of the Ajax and page loading issue, one WebElement is taking 10 sec time to load properly and sometimes it is taking 20 also. WebElement loading is not fixed and varying time to time. So if we declare the explicit wait with maximum time 10 then sometimes ElementNotVisibleExceptionwill be thrown coz webelement is not loaded fully.
To overcome this problem, we need to use Fluent Wait. So that it will check the Webelement visibility before throwing ElementNotVisibleException Exception.
Syntax:
Wait wait = new FluentWait(WebDriver reference) .withTimeout(timeout, SECONDS) .pollingEvery(timeout, SECONDS) .ignoring(Exception.class);
If you are using selenium version less than 3.11, then you can us the above syntax otherwise user the below code to define the fluent wait.
Wait wait = new FluentWait(WebDriver reference) .withTimeout(Duration.ofSeconds(SECONDS)) .pollingEvery(Duration.ofSeconds(SECONDS)) .ignoring(Exception.class);
Let’s discuss with the example .
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; public class FluentWait { WebDriver driver; @Test public void checkFluentWait() throws InterruptedException { String pageTitle = "TestingLPoint"; String actualTitle = "" ; driver = new FirefoxDriver(); driver.get("https://www.testinglpoint.com" ); driver.manage().window().maximize() ; actualTitle = driver.getTitle(); if (actualTitle.contentEquals(pageTitle)) { System.out.println( "Test Passed") ; } else { System.out.println( "Test Failed" ); } Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement seleniumlink = wait.until(new Function<WebDriver, WebElement>(){ public WebElement apply(WebDriver driver ) { return driver.findElement(By.id("selenium")); } }); clickseleniumlink.click(); driver.close() ; } }
Explanation:
To work with Fluent wait, we need some mandatory package which need to be imported. Below is the list of the package.
- ExpectedConditions;
- FluentWait;
- Wait;
- WebDriverWait;
As we mentioned above the, to define the fluent wait we need to define FluentWait(), withTimeout(), pollingEvery(), ignoing() properly.
- FluentWait(): This will accept the webdriver reference in which fluent wait need to be implemented.
- withTImeout(): This will accept the maximum time driver will wait before throwing an Exception.
- pollingEvery(): this will accept the regular time driver will check the visibility of the WebElement. For example, if we have given withTimeout as 30 sec and pollingEvery as 5 sec, then in every 5 sec driver will check the visibility of the web driver reference we have mentioned in FluentWait() till 30 sec. if it does not find the element till 30 sec, it will throw an Exception and proceed with the next test case.
- ignoring(): This will accept the exception class. We need to mention that Exception class which you want to ignore till the maximum time mentioned in withTimeout and at every pollingEvery() time when it checks the visibility of an Element.
