Types Of Wait Functions In Selenium And Their Implementation
Posted By : Manish Kumar | 27-Dec-2017
First of all, we should know, Why we need to use Wait Functions in the Selenium while writing our test script
As per the designs and technology in trend, all the applications are being developed in AJAX and Javascript and thus it is certain to happen that different element loads in the different times of interval which we want to interact with. This causes trouble to a Script Writer or Quality Analyst to locate the element as well as it will throw an exception "ElementNotVisibleException" or "NoSuchElementFound".
Using Wait Functions we can get this problem solved, we can provide implicit and explicit waits differently in order to make our script with such smartness that it will wait for element load instead of throwing "NoSuchElementException"
Types of Waits in Selenium: Basically there are only two Types of Waits which are
1) Implicit Wait
2) Explicit Wait
And one derived Wait which has some advanced uses in cases of random time loading functions, i.e.
3) Fluent Wait
Let us discuss them one by one:
Implicit Wait: This is an influenced idea from the Watir which is a library of Ruby and helps user to check documentation
Implicit waits help a user to set the default time for WebDriver, exception will be thrown only if the system does not find the desired element even in this period of time. Similar will be followed for all elements in the given application.
Also, After applying an Implicit Wait, it will last long till time the WebDriver instance gets finished
Syntax for Implicit Wait can be given as
"driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);"
Implicit Wait accepts two parameters, first one is the value part and second is the unit of measurement which can be seconds, nanosecond, microsecond or milliseconds.
Explicit Wait: This type of wait gives User an extra functionality that certain conditions can be applied and until these conditions are not satisfied the system will wait. These conditions may be Visibility and Clickability
Syntax for Explicit Wait can be given as: "WebDriverWait wait = new WebDriverWait(driver, Waittime);" "wait.until(ExpectedConditions.visibilityOf(username));" Example for Wait.Until: WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.presenceofElementLocated(By.cssSelector(".shw-pasw > i:nth-child(2)")));
Here system will wait until element username is visible.
Fluent Wait: This type of wait is used when user need to check the same element again and again and if it is found then waits gets finished and desired actions can be
performed, It is most likely that we are using a loop and break statement.
Hope, this will give a clear idea about the Wait functions used in selenium.
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manish Kumar
Manish is Adaptive and passionate to learn new technical skills, have knowledge of testing and love to play badminton in free time