Waits in Selenium WebDriver | Synchronization in Selenium

 



What are the Waits in Selenium WebDriver?


In Selenium, waits are used to control the execution flow of a script by making the script wait for a certain condition to be met before proceeding with the next step. 


There are several types of waits in Selenium, each with its own use case:


Implicit Wait: An implicit wait is a type of wait that tells Selenium to poll the DOM (Document Object Model) for a certain amount of time when trying to find an element or elements if they are not immediately available. 

The default polling time for an implicit wait is 0 seconds, but it can be set to any value greater than 0.


WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 

In this example, an implicit wait of 10 seconds is set for the web driver. This means that Selenium will poll the DOM for a maximum of 10 seconds when trying to find an element or elements if they are not immediately available.



Explicit Wait: An explicit wait is a type of wait that tells Selenium to wait for a certain condition to be met before proceeding with the next step. 

For example, you can use an explicit wait to wait for an element to be clickable before clicking it. Explicit waits can be set for a specific amount of time or until a certain condition is met.


WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("myBtn")));
element.click();
 


In this example, an explicit wait of 10 seconds is set for the web driver. This means that Selenium will wait for a maximum of 10 seconds for the element with the id "myBtn" to be clickable before clicking it.



Fluent Wait: A fluent wait is a type of explicit wait that allows you to configure the maximum amount of time to wait for a condition, as well as how often to check the condition and what to do when the condition is not met.


WebDriver driver = new ChromeDriver();
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofSeconds(5))
        .ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(By.id("myBtn"));
    }
});
element.click();
 

In this example, a fluent wait of 30 seconds is set for the web driver and it polls every 5 seconds to check for the element until it is found or time out.



Page Load Wait: A page load wait is a type of wait that tells Selenium to wait for a page to finish loading before proceeding with the next step. This is important when interacting with web pages that dynamically load content.


WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
 

In this example, a page load wait of 10 seconds is set for the web driver. This means that Selenium will wait for a maximum of 10 seconds for the page to finish loading before proceeding with the next step.



Sleep: Sleep wait is a type of wait that tells Selenium to pause execution for a certain amount of time. This type of wait is often used when a script needs to wait for a certain amount of time before proceeding with the next step, but it is not recommended as it can slow down test execution.


WebDriver driver = new ChromeDriver();
Thread.sleep(5000);
 



It's important to note that using waits can slow down test execution and make it less efficient, so it's important to use them judiciously. Explicit and Fluent waits are preferable than Implicit wait as they allow you to wait for a specific condition to be met and you can configure it as per the requirement.

It's also important to keep in mind that explicit and fluent waits are only effective when you are waiting for a specific condition on a page, if you want to wait for page load use page load wait.



Post a Comment

Post a Comment (0)

Previous Post Next Post