Friday, September 13, 2019

Post#44.Implicit Wait in Selenium


Implicit wait in Selenium WebDriver
Due following reasons application element may take a while to load.
·       Poor Server Response time
·       Ajax Loading
·       Size of the page

·       Internet Speed
·       System hardware
·       Browser slowness / less responsiveness
·       Element created using javascript

Selenium WebDriver tries to find the element without bothering about whether elements are loaded or not, and selenium WebDriver throws NoSuchElementException if element is not present.

Implicit wait is one of the way to request selenium not throw any exception until provided time. Default wait time of the selenium is 500 milli-seconds, implicitly wait overrides the default wait time of the selenium WebDriver.
If element is found before implicit wait time, selenium moves to next commands in the program without waiting to complete the implicit wait time, this wait is also called dynamic wait.

WebDriver driver = new ChromeDriver();
// set implicit wait tme as 30 Seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Scope of Implicitly Wait

Implicit wait is set for the entire duration of your WebDriver from the moment of declaration .and generally it is set at the start of your program. Most of the automation tester writes the implicit wait after creation of browser object.  So scope of the implicit will be applied on all the elements with same driver instance.
Implicitly Wait is applicable only to findElement and findElements no other statement in selenium.
Once set, the implicit wait is set for the life of the WebDriver object instance. You can re-assign the implicit wait time anywhere you want.

Scenario 1 : (driver.findElements())Consider a page having 100 checkboxes and applied implicit wait is 30 Seconds
When the page is loading selenium tries to find all the matching elements, but at that moment there no matching elements .

After 10 seconds of wait 1 check box is loaded, now selenium finds that one checkbox and concludes that there is only one element, so it ends the wait and moves to next command.

Selenium does not wait till 100 check boxes loaded into page, as WebDriver does not know how many elements going to be present on the page.

Scenario 2: Consider a page having 0 checkboxes and applied implicit wait is 30 Seconds
Selenium tries to find all the matching elements in DOM, but so far no check box is loaded.

Now there are zero matches so selenium waits till 30 seconds, and if does not find element after 30 seconds selenium WebDriver throws 
NoSuchElementEXception.

Supported Time units with Implicit Wait

Implicitly wait in selenium WebDriver supports all the time units from nano-Seconds to Days, all the units are present in the TimeUnit abstract class.

All the values (variable ) present in the 
TimeUnit are Constants, I guess you already got that from the UPPERCASE style itself. UPPERCASE is used to mean a value is constant.
  • TimeUnit.NANOSECONDS : represents nano seconds (10pow-9 of a second is nano second)
  • TimeUnit.MICROSECONDS : represents micro seconds (10pow-6 of a second is micro second)
  • TimeUnit.MILLISECONDS : represents mill seconds (10pow-3 of a second is micro second i.e 1000 milli seconds is a second)
  • TimeUnit.SECONDS : represent a second
  • TimeUnit.MINUTES : represent minutes, a minute is 60 seconds
  • TimeUnit.HOURS : represent hour, a hour is 60 minutes
  • TimeUnit.DAYS : represents a day, a day is 24 hours


No comments:

Post a Comment