top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Wait Commands in Selenium WebDriver

While executing your selenium Testscripts , sometimes your tests may fail because of “ElementNotVisibleException” Exception.

This Exception occurs when there is a time lag or delay in loading your webpage by the browser, this makes locating elements difficult if an element is not yet present in the DOM. We can solve this issue with the help of ‘Waits’. In automation testing, wait commands makes your test execution to pause for a certain length of time before moving onto the next step.


Selenium WebDriver provides three commands to implement waits in tests.

1. Implicit Wait

2. Explicit Wait

3. Fluent Wait


Implicit Wait:

It is applied on driver level.An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element not immediately available. The default setting is 0 (zero). Once set, the implicit wait is set for the life of the WebDriver object.


Package to be imported:

import java.time.Duration;


Syntax:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));


Example:


In the above code snippet webdriver will wait for 10seconds until the element is available before throwing a “TimeoutException”.


Explicit Wait:

Explicit wait is applied on certain elements (like dropdown buttons, popup windows, etc..) which may take sometime to load. Explicit wait makes the webdriver to wait until the specified condition is achieved and then moves to the next step.

The following are the “ExpectedConditions” that can be used while performing ExplicitWait.


· alertIsPresent ()

· elementSelectionStateToBe()

· elementToBeClickable()

· elementToBeSelected()

· frameToBeAvaliableAndSwitchToIt()

· invisibilityOfTheElementLocated()

· invisibilityOfElementWithText()

· presenceOfAllElementsLocatedBy()

· presenceOfElementLocated()

· textToBePresentInElement()

· textToBePresentInElementLocated()

· textToBePresentInElementValue()

· titleIs()

· titleContains()

· visibilityOf()

· visibilityOfAllElements()

· visibilityOfAllElementsLocatedBy()

· visibilityOfElementLocated()


Package to be imported:

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;


Syntax:

WebDriverWait explicitwait = new WebDriverWait(driver, Duration.ofSeconds(10)); explicitwait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath")));


Example:

In the above code snippet, the webdriver will wait until the specified webelement is available to be clickable. WebDriver waits for 10 seconds before throwing a “TimeoutException”. If it finds the element before 10 seconds, then it will return immediately. the program will not wait for the entire 10 seconds, thus saving time and executing the script faster.


3.Fluent Wait:

Fluent Wait is quite similar to explicit Wait. It is performed on an element only when you are unaware of the time it might take to be clickable or visible.

Few factors that Fluent offers are:

The polling frequency: This means telling the webdriver to keep an eye on the element after every 'x' seconds.

Ignore Exception: While pooling, if an element is not found, you can ignore some expectations like 'NoSuchElement'.


Package to be imported:

import org.openqa.selenium.support.ui.FluentWait;

import org.openqa.selenium.support.ui.Wait;


Syntax:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

.withTimeout(Duration.ofSeconds(30))

.pollingEvery(Duration.ofSeconds(5))

.ignoring(NoSuchElementException.class);


Example:

In the above code snippet webdriver will poll every 5 seconds until 30 seconds timeout is reached.If the element gets available before 30 seconds, the element will be returned and executes the next step.If not, it will return “ElementNotVisibleException”.

CONCLUSION:

· Waits play a crucial role while locating your webelements in the selenium your test scripts and helps to avoid Exceptions like “ElementNotVisibleException”,” NoSuchElement”.

· Implicit wait is most effective when used in a test case in which the elements are located with the time frame specified in implicit wait

· Explicit wait is most effective when used when the elements are taking a long time to load.


Note:

Applying Waits unnecessarily on webelements may slowdown the execution of your test, so we should use them wisely.

860 views1 comment

1 hozzászólás

0 csillagot kapott az 5-ből.
Még nincsenek értékelések

Értékelés hozzáadása
Vendég
2023. aug. 22.
5 csillagot kapott az 5-ből.

good!


Kedvelés
bottom of page