When to use explicit wait vs implicit wait in Selenium Webdriver?

I use:

driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS); 

But it still does not work continuously for the bottom element

  driver.findElement(By.id("name")).clear(); driver.findElement(By.id("name")).sendKeys("Create_title_01"); 

I added a wait code:

 for (int second = 0;; second++) { if (second >= 120) fail("timeout"); try { if (isElementPresent(By.id("name"))) break; } catch (Exception e) {} Thread.sleep(1000); } 

Should I not mean waiting, waiting until an item is found? It would also be better if I used Explicit wait instead of the code I added that has Thread.sleep() ?

+46
java selenium selenium-webdriver
May 01 '12 at 20:50
source share
6 answers

TL; DR: always use explicit wait. Forget that implicit expectation exists.




Here is a summary of the differences between explicit and implicit expectation:

Explicit Expectation:

  • documented and defined behavior.
  • works in the local part of selenium (in the language of your code).
  • works in any conditions you can think of.
  • returns either success or a timeout error.
  • can determine the absence of an element as a condition for success.
  • You can adjust the delay between retries and exceptions to ignore.

Implicit wait:

  • undocumented and almost vague behavior.
  • works in the remote part of selenium (the part that controls the browser).
  • only works with item search methods.
  • returns either an element found or (after a timeout) not found.
  • if checking for the absence of an element should always wait before the timeout.
  • cannot be configured other than global timeout.



Code examples with explanations. First implicit wait:

 WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement")); 

Now explicit expectation:

 WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebDriverWait wait = new WebDriverWait(driver, 10); WebElement myDynamicElement = wait.until( ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement"))); 

Both code examples do the same. Find a specific item and give up if not found after 10 seconds. Implicit waiting can do just that. It can only try to find an item with a timeout. The strength of the explicit expectation is that he can expect any conditions. Also set a timeout and ignore certain exceptions.

Examples of possible conditions: elementToBeClickable , numberOfElementsToBeMoreThan or invisibilityOf . Here is a list of built-in expected conditions: https://seleniumhq.imtqy.com/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

More explanation:

Implicit latency only affects findElement* methods. If set, then all findElement* will "wait" for the set time before declaring that the item cannot be found.

How findElement* will wait is not defined. It depends on the browser or operating system or version of selenium. Possible implementations:

  • repeatedly try to find the item before the timeout. return as soon as the item is found.
  • try to find the item. wait until the timeout expires Try again.
  • wait until the timeout expires to try to find the item.

This list is made up of observations and reading error reports and a brief reading of the selenium source code.




My conclusion: implicit waiting is bad. The possibilities are limited. The behavior is undocumented and implementation dependent.

Explicit waiting can do everything; implicit waiting can do much more. The only drawback to explicit waiting is a bit more verbose code. But this verbosity makes the code explicit. And explicit is better than implicit. Right?




Further reading:

+91
Jan 21 '15 at 12:40
source share

Have you tried a cursory glance? Implementation of the Wait interface, which can have its own timeout and polling interval, configured on the fly. Each FluentWait instance determines the maximum timeout for the condition, as well as the frequency of the status check. In addition, the user can configure wait to ignore certain types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

see this link current description of the expectation

In particular, I used my free time this way:

 public WebElement fluentWait(final By locator){ Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until( new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); } } ); return foo; } ; 

As you noticed, a quick return of expectations found a web element. Thus, you simply pass the locator with type By and then you can perform any action on the found web element.

 fluentWait(By.id("name")).clear(); 

Hope this helps you)

+5
Sep 03
source share

Implicit waiting is a global setting , applicable to all elements, and if the element appears before the specified time than the script, then the execution will be executed otherwise the script will throw a NoSuchElementException , The best way to use it in the setting method. Use only By.findelement() .

Thread.sleep() - the script time will sleep, it is not very good to use it in the script since it sleeps without any conditions. What to do if in 5% of cases 2 seconds is not enough?

Explicit wait : Wait until it indicates contains / modifies an attribute. It is more used when the application provides an AJAX call to the system and receives dynamic data and visualization in the user interface. In this case, WebDriverWait is suitable.

+4
Mar 12 '13 at 16:54
source share

ImplicitWait:

  1. Static Wait 2. UnConditional Wait (No conditions are given) 3. Applicable throughout the program 

Java implicit wait declaration - Selen:

 driver.manage().timeout().implicitWait(20, TimeUnit.Seconds()); 

When to use implicit Wait?

Implicit wait is not recommended to be used anywhere in the automation package, since it is static, and we do not know when the web element will appear on the website.

i.e. Suppose you set the implicit wait to 5 seconds, and the driver can identify the web element in 2 seconds, since we used the implicit wait driver, which will wait another 3 seconds (up to 5 seconds). This will slow down the automation process.

Explicit Expectation:

  1. Dynamic wait
  2. Conditional wait.
  3. Not applicable throughout the program.

Declare an explicit wait in Java Selenium.

 WebDriverWait wait=new WebDriverWait(driver, 20); wait.until(somecondition); 

When to use explicit wait?

We should always use explicit expectation, since it is dynamic.

i.e. Suppose you set an explicit wait of 5 seconds, and the driver can identify the web element in 2 seconds, because we used an explicit wait driver that will not wait another 3 seconds (up to 5 seconds). The driver will continue to work after 2 seconds. This will speed up the automation process.

+3
May 16 '17 at 18:15
source share

Have you tried using 'WebDriverWait'? I imagine that you want:

 WebDriverWait _wait = new WebDriverWait(driver, new TimeSpan(0, 0, 2)); //waits 2 secs max _wait.Until(d => d.FindElement(By.Id("name"))); //do your business on the element here :) 

This will pretty much, as I understand it, do what your current code does. He will constantly check the method (ignoring not found exceptions) until the timeout of the time passed in the time interval is reached, and the third parameter can be entered to indicate sleep in milliseconds. Sorry if this is what implicitlyWait does too!

Edit: I read today and better understand your question and understand that this does exactly what your implicit wait setting should do. Let's leave it here just in case the code itself can help someone else.

+2
May 01 '12 at 21:56
source share

Implicit expectations are used to provide a wait time (say, 30 seconds) between each successive stage of testing throughout the test script or program. The next step is only performed after 30 seconds (or any other time) after the previous step

Syntax:

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

Explicit expectations are used to stop execution until a certain condition is met or the maximum time that is determined is determined. Implicit wait is applied between each successive stage of testing throughout the script test or programs, while explicit wait is applied only for a specific instance.

Syntax:

 WebDriver driver = new FirefoxDriver(); WebDriverWait wait = new WebDriverWait(driver,30); 
0
Aug 08 '17 at 6:13
source share



All Articles