Waits in selenium – we learn here about the different kind of waits in selenium . First we need to understand what is the need of using waits in selenium. Many times we observe while automating a test case that webpage takes time to load , webpages has lot of javavscript elements and AJAX and each element take different time to load on the web page, so we need to wait for the elements to be visible on the page to perform the operation like click, send text etc . Suppose we do not wait for the elements to appear on the web page then we will get the NoSuchElementExceptions as driver will not able to locate the element. So to avoid this exception we use the waits in selenium script.
There are 3 types of Waits in selenium
Implicit Wait in Web driver Selenium
Implicit wait means that selenium webdriver will wait for a certain amount of specified time for each task whatever webdriver is performing before throwing any exception . We just need to define the implicit wait once in the script and it will applied to all webdriver events like waiting for page to load , waiting for an element to appear etc.
Syntax for Implicit Wait
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //
Here in the above code two parameters are passed , one is 10 ( time ) and second is time unit (second , milli second )
Java code for the Implicit Wait
package com.selenium_basics; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class waits_tutorials { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\drivers\\geckodriver.exe"); WebDriver driver=new FirefoxDriver(); //openbrowser // implicit wait is defined with max waiting time of 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.facebook.com/"); //open url // webdriver will wait for 20 seconds till the below text box appears and send keys driver.findElement(By.id("email")).sendKeys("padhle"); // send the keys to text box } }
Note : If we have defined the implicit wait of 10 seconds that does not means that web driver will wait for 10 seconds , the web driver keeps on searching the element till 10 seconds and when ever it finds the element it will perform the operation and go to the next statement, Hence if web driver finds the element in 2 second then it will execute the command and moves on to the next statement and if it is not able to locate the element in 10 second it will throw the exception.
Explicit Wait in Web driver Selenium
Explicit waits are used to wait at a particular condition , suppose we have the particular web page which takes 30 seconds to load so we cannot define implicit wait here for 30 seconds as it will wait maximum time for 30 seconds for all events, hence selenium introduce explicit wait for such type of conditions. Explicit waits wait for the certain conditions.
Syntax for Explicit Wait
// WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds) //
Here two parameter is passed , one is web driver reference and other is time in seconds.
Java Program for the Explicit Wait
package com.test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Explicit_wait { public static void main(String[] args) { WebDriver driver=new FirefoxDriver(); //openbrowser // explicit wait is defined with max waiting time of 10 seconds WebDriverWait wait = new WebDriverWait(driver, 10); driver.get("https://www.facebook.com/"); //open url // webdriver will wait maximum for 20 seconds till the below text box appears WebElement wb = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))); wb.sendKeys("padhle"); // send the keys to text box } }
Note : If the webdriver wait is set to 30 seconds then the web driver will wait maximum for 30 seconds only, after that it will throw the exception , If webdriver get the element within 30 seconds then it will go to next statement( it will not wait for 30 seconds ).
Expected Conditions that can be used with webdriver wait
1) alertIsPresent()
2) elementSelectionStateToBe()
3) elementToBeClickable()
4) elementToBeSelected()
5) frameToBeAvaliableAndSwitchToIt()
6) invisibilityOfTheElementLocated()
7) invisibilityOfElementWithText()
8) presenceOfAllElementsLocatedBy()
9 )presenceOfElementLocated()
10) textToBePresentInElement()
11) textToBePresentInElementLocated()
12) textToBePresentInElementValue()
13) titleIs()
14) titleContains()
15) visibilityOf()
16) visibilityOfAllElements()
17) visibilityOfAllElementsLocatedBy()
18) visibilityOfElementLocated()
Fluent Waits in Web driver selenium
Fluent wait is one more type of wait which is similar to the web driver wait with one addition that we can set the frequency to locate the element before it will throw the “ElementNotVisibleException“.
Syntax for the Fluent Wait
Here we pass the 3 parameters in fluent wait
1) Timeout in seconds – that is maximum time for a webdriver will wait.
2) Polling time in seconds – that is frequency for a web driver to locate the element.
3) Ignoring Exception – Define the exception you want to ignore
Wait wait = new FluentWait(WebDriver reference) .withTimeout(timeout, SECONDS) .pollingEvery(timeout, SECONDS) .ignoring(Exception.class);
Java Code to Implement Fluent Wait
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; public class Fluent_Wait { public static void main(String[] args) { WebDriver driver=new FirefoxDriver(); //open browser // Fluent wait is defined with max waiting time of 10 seconds // Webdriver will wait for maximum 20 seconds and 2 second is polling time that means // it will check after every 2 seconds the viibility of element Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(20, TimeUnit.SECONDS) .pollingEvery(2, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); driver.get("https://padhle.com/automation-practice-demo/"); //open url // webdriver will wait maximum for 20 seconds with interval //of 2 seconds till the below text box appears WebElement wb = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("textbox"))); wb.sendKeys("padhle"); // send the keys to text box } }
This is all about the web driver waits in selenium using java. Now you clearly identify the difference between the implicit wait and explicit wait which is one of the most important interview question of selenium.