Explicit Wait, Implicit Wait, Fluent Wait in Selenium using C#

What are Wait commands?

Waits are the command in Selenium which is very useful for the execution of the automated test scripts. During execution of automated test script, there might be some situation where issues may occur due to variations in time lag for loading web elements. In these situation Wait commands helps in troubleshooting these issues.

Wait commands helps in directing a test script to pause for a certain time before throwing exception.

Types of  Wait Commands

Selenium WebDriver provides three types of wait commands to implement in tests.

1. Implicit Wait
2. Explicit Wait
3. Fluent Wait

Implicit Wait

Implicit wait directs the Selenium WebDriver to wait for a certain measure of time before throwing an exception. Once this time is set, WebDriver will wait for the webelement before throwing exception.

So by implicitly wait, the WebDriver will wait for certain duration to find any webelement. This can helpful when certain elements on the webpage are not available immediately and need some time to load. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session.

Example Using C#

IWebDriver driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.Url = "http://somedomain/url_that delays_loading";
IWebElement dynamicElement = driver.FindElement(By.Name("dynamicElement"));

Explicit Wait

Explicit wait command helps in redirecting the WebDriver to wait until a certain condition occurs before proceeding with executing the code.

Explicit wait is important in the cases where there certain elements that naturally take more time to load. Implicit wait causes unnecessary delay in executing the test script which will wait for the same time frame before loading every web element. Explicit wait is an improvement on implicit wait since it allows the program not to wait the entire provided timespan, if the webelement is available before the timespan it will proceed to the next line of code, thus saving time and executing the script faster.    

Using C#

WebDriverWait wait = new WebDriverWait(driver,30)
wait.until(ExpectedCondition.visibilityOf ElementLocated(By.Xpath("reference of webelement")));

Fluent Wait

Fluent wait command defines the maximum amount of time for Selenium WebDriver to wait for a certain condition to appear.

To be more precise, Fluent wait looks for a web element repeatedly at regular intervals until timeout happens or until the object is found.
















Comments

Popular posts from this blog

Assertion

CI/CD Pipelines & DevOps