Replace implicit wait with explicit wait (selenium webdriver & java)
Asked Answered
W

5

5

How can I replace this implicit wait with an explicit one?

driver = new ChromeDriver(capabilities);

driver.manage().deleteAllCookies();

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

This is used in the Before Method. I was able to replace all the Thread.sleep()'s in the code, but I'm not sure what do to for this one.

Wynd answered 16/8, 2017 at 11:32 Comment(6)
What is the reason for doing that ? Implicit wait is better than explicit ones in most of the cases.Eponymous
I understood that as a best practice is recommended not to mix implicit and explicit wait. Also I chose to use explicit wait because it's saving time for the Maven Run testWynd
@AbhishekSingh Using implicit wait is not a good practice (should be avoided) while using explicit wait is a best practice.Crescint
@Crescint I am really finding weird some people say we should use explict others implicit, Where guys do you find your references?Linkwork
@eusoubrasileiro Some of it is from reading lots of articles, etc. but I don't remember one off the top of my head. I do remember seeing Simon Stewart at seleniumconf a couple years ago in his keynote speech saying not to use it. Simon Stewart is the lead of the Selenium project. You can find his keynote at youtu.be/gyfUpOysIF8?t=2200. That's queued up to where he comments on implicit wait and not to use it. There are others that I saw at seleniumconf that year where contributors were commenting about it but I don't remember which videos.Crescint
thank you very much @Crescint I could also think this could be an wiki answer but let it be for now... regardsLinkwork
N
3

Implicit wait is defined once right after the driver initialization for the driver life time, and it sets the maximum amount of time for the driver to look foe WebElement.

Explicit wait is used to wait up to the given amount of time for the WebElement to be in cretin condition, and need to be used each time you are waiting for condition to met.

You can't "replace" the implicit wait definition with explicit wait, as they are different thing and there is no condition to wait for in this point.

Narthex answered 16/8, 2017 at 11:50 Comment(0)
L
2

ExplicitWait

As per the documentation, an ExplicitWait is a code block you define, configure and implement for the WebDriver instance to wait for a certain condition to be met before proceeding for the next line of code. There are some methods that helps us to implement ExplicitWait that will wait only as long as required. WebDriverWait in combination with ExpectedConditions is one of the way ExplicitWait can be achieved.


An Example

driver.get("http://www.example.com/");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_css"))).click();

Explanation

This implementation of ExplicitWait waits up to 10 seconds before throwing a TimeoutException or if it finds the element then it will return within 0 to 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true or a not-null object.


Expected Conditions

There are some frequently encountered conditions when automating Web Browsers for testing Web/Mobile applications. The Java, C# and Python bindings include those convenient methods so we don’t have to write-up an ExpectedCondition class ourselves or create our own utility package for them. Some of the Expected Conditions are:

  • alertIsPresent()
  • elementToBeClickable(locator)
  • elementToBeSelected(WebElement)
  • frameToBeAvailableAndSwitchToIt(locator)
  • invisibilityOf(element)

Here you can find about the all the methods supported by Expected Conditions.


Inducing ExplicitWait

To induce ExplicitWait, first you have to remove all the calls to implicitlyWait() in your Test Framework. Start a fresh execution and observe where you face the exception for an element attribute. The exceptions we will be facing will be either one of the follows:

  • NoSuchElementException
  • ElementNotVisibleException
  • ElementNotSelectableException

Now, we need to confirm the particular attribute of the WebElement for which we need to wait. If the WebElement in consideration is to be clicked we will consider Expected Conditions as elementToBeClickable(locator).

Element is Clickable implies Element is Displayed and Enabled.


Outro

Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

Louanneloucks answered 16/8, 2017 at 14:5 Comment(0)
K
1

once you declare implicitlyWait it will apply to your all element through out the script run. So declare it initially to prevent from script getting fail.

Now if there is element which requires explicit wait then just declare it just before to do some action or use refernce of same. explicit wait is not applied through out like implicitlyWait.

Example :-

WebElement seleniumlink;
seleniumlink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='add_files_btn']")));
seleniumlink.click();

Refer below link for more details :-

https://www.guru99.com/implicit-explicit-waits-selenium.html

Kampong answered 16/8, 2017 at 11:41 Comment(3)
It sounds like you are talking about having both implicit and explicit waits in the same script which is not recommended.Crescint
I will like to know the complication of using both in same script ... please provide me some links so I can understand betterKampong
See the link (and text from that link) in my answer. There are other sources too but that's from the official docs. You can also reference some of the links in DebanjanB's answer for links to videos where Selenium contributors recommend against using implicit wait at all.Crescint
C
1

Implicit waits are set once and apply throughout the life of the driver instance so there is no real replacement for that line. You should just remove it because you don't want to mix implicit and explicit waits according to the official docs.

WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.

Once you remove that line, you will need to run your scripts and add explicit waits in the areas where a wait is needed.

Crescint answered 16/8, 2017 at 14:52 Comment(0)
A
0

Using implicit and Explicit waits.

Though we weigh Explicit wait to be more advisable, you should check for the scenario you are catering for.

For example in your code, you are calling the wait just to get the time for the delete call to complete. Which can be improved without using any wait at all.

 Set<Cookie> cookies = driver.manage().getCookies();
 for(;cookies.size() != 0;){
   driver.manage().deleteAllCookies();
 }

**You can use While loop i suppose **

Having said that, to use explicit wait. I have used it for taking actions on the pop up, that I get during the app testing.

WebDriverWait wait = new WebDriverWait(appDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
appDriver.switchTo().alert().dismiss();

So use no wait if you can verify the condition of execution, and use explicit wait when you know what to wait for. Hope this helps a bit.

Agnate answered 11/3, 2019 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.