Implicitly Wait in Selenium WebDriver


 Implicitly Wait in Selenium WebDriver


Implicitly wait is associated to a global timeout period and is common to all WebElements. This is used to configure the wait time of WebDriver as a whole for the AUT. Obviously, depending on the network speed test execution time can be decreased or increased. If an application is hosted on a local machine always takes less time comparing the same application hosted on a remote server would take more time to load a page. In addition to that, wait time would be configured considering the cases accordingly so that tests do not spend more time to wait for the page to be loaded or spend less time and timeout. WebDriver provides a method called manage() to handle such issues in order to set the implicitly wait time. Using implicitly wait time, the maximum wait time can be set for all WebElements on the webpage. Furthermore, Before WebDriver reaches the maximum time out; it polls the DOM for the WebElements for a certain period of time on the page. However, the default timeout is set at 0.If a negative timeout is set, page load would be indefinite. Once set, it is set for the life of its object instance. In case of searching for multiple elements, WebDriver polls the DOM until it finds at least one element or the timeout expires. If the locator is used by XPath which is considered to be a slower strategy, it is better to increase implicitly wait timeout judiciously due to its adverse effect.

//wait for 5 secs
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//change the theme
driver.findElement(By.xpath("//div[@='MOa']/div[2]/div[@='Gpa'][contains(text(),'Change theme')]")).click();

In the above code, notice that maximum timeout is set 5 seconds in the browser to wait for the WebElements since it is not present immediately in the page. If it cannot load the elements within the given period of time it throws a NoSuchElementException error; otherwise, it proceeds further with rest of the test.

Comments

Popular posts from this blog

Explicitly Wait in Selenium WebDriver

How to take a screenshot in Selenium WebDriver