Replace implicit wait with explicit wait (selenium webdriver & java)

Viewed 4844

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.

5 Answers

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.

Related