What are Selenium waits?
When executing a Selenium-based automated test, there is a high possibility that some of your tests would fail occasionally, even though you are certain that the elements are correctly located and the tests are correctly written, i.e. they become flaky. Everyone hates flaky tests especially when you need to run them a couple of times during the day due to the high development workload.
The first step that every automation engineer would do is inspect the code for having a properly implemented Selenium waits.
Selenium waits are special commands that are used when we want to pause for a specific period of time during test execution before moving further with the next step. This “wait for a specific period of time” can be hard-coded, meaning, we can set how many seconds the execution to wait before proceeding or we can set some condition to be satisfied before continuing.
Selenium waits can become really handy, especially if you want to stabilize the tests and the execution itself. They allow the test script to correctly wait for a specific element before being fetched, check element visibility conditions or perform some activity like, click, get value, or input some text.
Why do we need waits?
Evey webpage out there contains elements in a form of links, text, buttons, dropdowns, images, videos, etc. When the page is opened, those elements are being loaded into the DOM of the page. Depending on the server workload that the webpage is hosted, the internet connectivity, or the current webpage traffic, there can be issues with the element not being loaded to the webpage in a specific period of time.
Usually, in a perfect environment, the elements would load pretty fast, almost immediately when you open a webpage, but that’s not always the case, especially when you run automated UI tests frequently. That’s why we need to make sure that we use proper waits no matter the external disruptions we are going to experience during the test execution. Every UI automation test framework needs to have waits implemented, no matter which framework they are based on.
Types of waits
There are three types of Selenium waits used across all of the test automation frameworks. All of them have advantages and can be used depending on which activity you want to do afterward. I will go through each and every one of them and will also show some good examples of when is the appropriate scenario to implement them.
Implicit wait
The implicit wait is a type of Selenium wait where we tell the WebDriver to wait a specific time for the page to load all elements before throwing an exception. The default value is always 0 so you need to specify the number of seconds in which you expect all the elements to be pulled (loaded) in the webpage. If for example an element is not found after you implicitly wait for it, you will receive NoSuchElementException.
Let’s take a TestNG (Java based) example:
@Test
public void implicitWaitTest() {
WebDriver driver = new ChromeDriver();
driver.get(https://www.qamind.com);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
actualTitleValue= driver.findElement(By.id("title")).getText();
assertThat(actualTitleValue, is(equalTo("QAMIND")))
}
After we initialize the driver and navigate to www.qamind.com, we set an implicit wait for 10 seconds. This will be the total amount of time the test will wait for all the elements to be successfully loaded. We are fetching the title value so we assume that in those 10 seconds that element will be loaded in the DOM. If not, an exception will be thrown and e would that the elements need more time to be loaded or some other underlying issue is causing that element not to load immediately.
Explicit wait
This type of wait is preferred to be used in most cases. It is widely recommended since it dynamically waits for the element based on some predefined condition. If we want to fetch an element but we want to add an explicit wait before the actually fetching, we can write something like this:
wait.until(ExpectedConditions.elementToBeClickable(element));
The above command will wait for the element which is passed as an argument, to become clickable. Here we can specify a default timeout that will be thrown if the element does not become clickable. The ExpectedConditions class consists of many wait methods with different purposes and outcomes.
There are many ways to dynamically wait for an element like, wait for the element to become visible to the page, to be selected, to be clickable, to contain title, etc.
Let’s take another example:
@Test
public void explicitWaitTest() {
WebDriver driver = new ChromeDriver();
WebDriverWait explicitWait= new WebDriverWait(driver, 30);
driver.get(https://www.qamind.com);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("title"))));
actualTitleValue= driver.findElement(By.id("title")).getText();
assertThat(actualTitleValue, is(equalTo("QAMIND")))
}
After we initialize the chrome and the waiting driver, we navigate to the page and we dynamically wait for the element to become visible on the page. visibilityOf() method checks if the element that’s already presented in the DOM is also having a height and width larger than zero and it is displayed.
With this, we are ensuring that no matter the external factors that can disrupt our execution, we wait for the element for N period of time before proceeding.
Fluent wait
There is another type of wait that has the same concept as explicit waits but a minor difference. Fluent wait allows us to set a maximum time for a condition to be satisfied and we can also specify how often we would check that condition during that wait time. It searches for the element at regular intervals before throwing an error.
Wait wait = new FluentWait(driver)
.withTimeout(45, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS);
wait.until(ExpectedConditions.elementToBeSelected());
We initialize the FluentDriver and we set the total timeout and the regular checking time. Dynamically waiting for an element to be selected up to 45 seconds and every 5 seconds, we’re checking that condition.
Conclusion
Which type of selenium waits you will be using depends on many factors like how much time the element needs to be loaded, are we waiting for all the elements to be loaded first, do we need to strictly stay with a specific type of wait, etc. Whichever you choose, make sure you have properly implemented waits.
Interested to learn more about automating tests using Selenium? Enroll Now for Selenium Certification Training By Edureka and increase your chances to get hired by Top Tech Companies.
Share This Post
Latest Posts
- Test Analyst’s Responsibilities in Risk-Based Testing
- Crucial Aspects Of Superior Test Code Quality
- Streamline Your Cucumber BDD Tests with Gherkin Lint: A Comprehensive Guide
- How to Test Authentication and Authorization with Confidence + Best Tips
- Exploring the Three Scrum Artifacts: Grasping Their Essence and Effective Utilization