Sometimes our test fails due to failure in loading page with all elements due to slow internet connection or due to the poor response time.
Types of wait:
The syntax for implicit wait is:
// opens a Firefox browser WebDriver driver = new FirefoxDriver(); // opening of url driver.get("http://www.javabykiran.com/selenium/demo/"); //providing implicit wait driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
This is not a good option to opt for as this makes driver instance to wait for 15 minutes each time.
Let’s suppose that we have 4G speed but still it is not a good option as unnecessarily one has to wait for that much time for all actions to be done on the browsers.
It’s not dynamic as it always makes browsers to wait for every action we perform.
After 15 seconds it will timeout if the element is not loaded.
We can do as given below for adding an explicit wait.
In this case, it again blindly waits for the mentioned seconds. It is not preferred because if the element is present within this given waiting time, the program does not move further until the waiting time is finished.
Some computers are slow due to which an element may not show up within the given waiting time.
It is a sleep time for test script but not a good way to use in the script as it sleeps without any condition.
An explicit wait is the code you define to wait for a certain condition to occur before proceeding further in the code.
There are some convenient methods provided which helps you to write the code that will only wait as long as required.
“WebDriverWait” in combination with “ExpectedCondition” is one way this can be accomplished.
WebDriverWait wait = new WebDriverWait(driver, 15); WebElement element = wait until(ExpectedConditions.presenceOfElementLocated(By.id("password")));
This waits up to 15 seconds before throwing a “TimeoutException” else if it finds the element it will return it in 0 – 15 seconds.
“WebDriverWait” by default calls the “ExpectedCondition” every 500 milliseconds until it returns successfully.
A successful return is for “ExpectedCondition” whose return type is Boolean and returns true.
There are some common conditions that frequently come across when automating web browsers.
Here below some more examples given on Explicit wait:
WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions.ElementIsVisible(By.Id("elementId")));
WebDriverWait wait = new WebDriverWait(driver,30); wait.Until(ExpectedConditions.elementToBeClickable(By.Id("elementId")));
WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions.visibilityOfElementLocated(By.Id("elementId")));
Fluent Wait uses two parameters – “timeout value” and “pollingfrequency”. Firstly it sets the following values.
The maximum amount of time to wait for a condition.
The frequency to check the success or failure of a specified condition.
Further, more the user configures the wait to ignore specific types of exceptions while waiting, such as ‘NoSuchElementExceptions’ when searching for an element on the page.
Wait< WebDriver > wait = new FluentWait< WebDriver >(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); .wait until(ExpectedConditionElementToBeClickable(By.Id()));
Do not mix implicit and explicit wait. Doing so can cause unpredictable wait times, example setting an implicit wait to 10 second and an explicit wait of 15 second could cause time out to occur after 20 seconds.
Never ever use both implicit wait and explicit wait on same driver. It’s not good a practice.
Implicit wait is on driver or browser.
Explicit wait is on elements.
Thread.sleep is for programming next statements to execute.
Fluent wait:Explicite wait with addition to some more configuration like pooling frequency.