Waits in Selenium
Waits in Selenium
Most of the web elements are written using Java script or AJAX.
They may take certain amount of time to load on page.
In this situation chances of getting ‘NoSuchElementException’ are more.
To avoid such exceptions, Selenium has provided two types of Waits - Implicit Wait & Explicit Wait.
1. Implicit Wait:
This wait is set for the lifetime of WebDriver instance.
Wherever we use WebDriver instance before that line wait will be applied.
This wait, polls for 500milliseconds (This time depends on the browsers implementation class of
webdriver. For firefox it is 500milliseconds, it depends on type and version of browser).
If element becomes available in first 500milliseconds then element will be returned else it will wait for
next 500 milliseconds. Our script will wait for max timeout.
We can specify default time out.
If element is not available in timeout value then ‘NoSuchElementException’ will be thrown.
We can specify timeouts in terms of: NanoSeconds, MicroSeconds, MilliSeconds, Seconds, Minutes,
Hours and Day.
E.g
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
In above example, our script will wait before clicking all three elements (Element1, Element2 and Element3 ).
We can achieve explicit wait using two classes of Selenium: FluentWait and WebDriverWait.
1. Using FluentWait:
FluentWait class implements Wait interface.
We can set our own timeout and polling time using FluentWait.
We can also ignore exceptions while waiting for particular WebElement.
We can wait till certain condition of a web element.
This wait can be applied to particular WebElement unlike ‘Implicit Wait’ which applied for entire life
span of WebDriver instance.
In above example, wait is configured for maximum 10 seconds. If element is not available in 10
seconds, then NoSuchElmentException will be thrown.
3. ignoring(exceptionType):
This method will ignore specific types of exceptions while waiting for a condition.
E.g.
wait.ignoring(NoSuchElementException.class);
4. until(ExpectedConditions):
After configuring wait using until() method, script will keep on waiting until one of the following
encounters:
Until Expected condition is encountered.
Until the timeout expires
Until the current thread is interrupted
E.g.
wait.until(ExpectedConditions.alertIsPresent());
2. Using WebDriverWait:
WebDriverWait (WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut )
Interview Questions -
Implicit Wait: Implicit waits are used to provide a default waiting time (say 30 seconds) between each
consecutive test step/command across the entire test script. Thus, subsequent test step would only execute
when the 30 seconds have elapsed after executing the previous test step/command.
Explicit Wait: Explicit waits are used to halt the execution till the time a particular condition is met or the
maximum time has elapsed. Unlike Implicit waits, explicit waits are applied for a particular instance only.
2. What is Implicit Wait in Selenium WebDriver?
Implicit waits tell to the WebDriver to wait for a certain amount of time before it throws an exception. Once we
set the time, WebDriver will wait for the element based on the time we set before it throws an exception. The
default setting is 0 (zero). We need to set some wait time to make WebDriver to wait for the required time.
3. What is WebDriver Wait in Selenium WebDriver?
WebDriverWait is applied on a certain element with defined expected condition and time. This wait is only
applied to the specified element. This wait can also throw an exception when an element is not found.
4. Write a code to wait for a particular element to be visible on a page. Write a code to wait for an alert
to appear.
We can write a code such that we specify the XPath of the web element that needs to be visible on the page
and then ask the WebDriver to wait for a specified time. Look at the sample piece of code below:
FluentWait can define the maximum amount of time to wait for a specific condition and frequency with
which to check the condition before throwing an “ElementNotVisibleException” exception.
6. Difference b/w implicitly Wait and Explicit wait.
1. Valids for life time of webdriver instance hence Can apply to particular web element
once used applicable to all elements
3. Polling time is browser & version specific and Polling time and timeouts is customizable.
fixed and timeouts is customizable.
4. this wait doesn’t provide a feature to wait till a this wait provides a feature to wait till a certain
certain condition occur condition occur
5. We can’t ignore any exception using implicit We can ignore exception using explicit Wait
Wait
7. What are the different types of WAIT statements in Selenium WebDriver? Or the question can be
framed like this: How do you achieve synchronization in WebDriver?
There are basically two types of wait statements: Implicit Wait and Explicit Wait.
Implicit wait instructs the WebDriver to wait for some time by polling the DOM. Once you have declared
implicit wait, it will be available for the entire life of the WebDriver instance. By default, the value will be 0. If
you set a longer default, then the behavior will poll the DOM on a periodic basis depending on the browser/ driver
implementation. Explicit wait instructs the execution to wait for some time until some condition is achieved.
Some of those conditions to be attained are:
elementToBeClickable
elementToBeSelected
presenceOfElementLocated
8. What are the Expected Conditions that can be used in Explicit Wait ?
- The following are the Expected Conditions that can be used in Explicit 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()