Details about Implicit and Explicit Waits in Selenium

Software testing is gaining more significance in today’s world. Many test automation tools have made testing easier and effective. However, one needs to have in-depth knowledge in the concerned testing tool to execute the test effectively. This can be gained by joining a tutorial providing training on different tools.

Implicit and Explicit Waits in Selenium

Out of the many test automation tools, Selenium has gained more popularity and hence there is a rush for selenium tutorials. There are several selenium tutorials at present and most of them are online tutorials. In these tutorials, the trainers will provide training on the entire process of testing using selenium. Implicit and explicit waits in Selenium are two major things involved in Selenium testing and in this article, we are providing more details about them.

Why are Waits required in Selenium?

During UI automation, all the elements may not get loaded with the same speed. An event page may get loaded as soon as you trigger but not all the elements in it. In such cases, you may get “Element Not Found” exception when you try to locate the element. In such cases a static wait such as “Thread.sleep ()” can be used which will halt the program for a specific time and then proceed. However the program will continue after the specific time even if the desired element is not loaded. Hence it is not suitable for UI automation. Here, Selenium has different types of waits to take care of this issue. Among the different types implicit wait and explicit wait are the two types of waits in Selenium that are used most widely.

Implicit Waits

Implicit wait is set to the webdriver instance and it is applied to all web elements. Here, the webdriver polls the DOM for checking the availability of the web element for a maximum time period which can be specified and will throw the “Element Not Found” message if the element is not loaded even after the specified time period.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

In the above given code, “20” mentioned against implicit wait is the maximum time in seconds the program will wait before showing “Element Not Found” message.

Explicit Waits

The major difference between the implicit and explicit waits is that explicit wait is applied to each and every web element. Here, certain conditions will be defined and the webdriver will wait for those conditions to be matched before locating web element or performing some actions on them. “Element to be Clickable”, “Presence of Elements Located” etc are some of the most widely used conditions.

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.presenceOfElementLocated(ElementLocator));

In this case the webdriver instance will wait for the condition mentioned, ie. “Presence of Element Located” to meet for a maximum time limit of 15 seconds. The test execution will start as soon as the element is located. It will start execution after 15 seconds even then the element is not located.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *