Here is a udemy course (from "Lets Kode It") to develop a web automation framework with selenium and Java. But, this is not a java question. You only need to know selenium in any of these languages - javascript, python, ruby, c# & java.
The instructor has developed a CustomDriver class which has the method/function given below. The method waits for an element to be clickable, without having to write WebDriverWait
statements everywhere in our code. It first sets the implicit wait to zero, does an explicit wait and then sets the implicit wait to the original value which was being used in the framework.
This approach seems okay to me, but I am not sure about it. Could mixing implicit and explicit waits like this cause any problems?
UPDATE (March 24, 2020) - I already know that mixing implicit and explicit waits is considered a bad practice because it can lead to unpredictable wait times. I am not asking about the unpredictable wait times because there are plenty of questions and articles on that already.
Instead, I am asking that if the implicit wait is set to zero every time before doing an explicit wait, then is that okay? Will that still cause the problems of unpredictable waits? Will it cause other problems ?
/*
Click element when element is clickable
@param locator - locator strategy, id=>example, name=>example, css=>#example,
tag=>example, xpath=>//example, link=>example
@param timeout - Duration to try before timeout
*/
public void clickWhenReady(By locator, int timeout) {
try {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
WebElement element = null;
System.out.println("Waiting for max:: " + timeout + " seconds for element to be clickable");
WebDriverWait wait = new WebDriverWait(driver, 15);
element = wait.until(
ExpectedConditions.elementToBeClickable(locator));
element.click();
System.out.println("Element clicked on the web page");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Element not appeared on the web page");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
}