1

I have written a Selenium Java Framework that uses both implicit waits and explicit waits.

I am using Page Object Model so all of the elements are declared at the top of the page class as shown here:

@FindBy(how = How.ID, using = "firstname")
public WebElement firstname;

The elements are instantiated using Page Factory as shown here:

public BasePage() {
    PageFactory.initElements(driver, this);
}

When I first move to a new webpage I use an explicit wait to check that one of the elements on the page is there. To do this, I temporarily set the implicit wait to 0 (so it doesn't clash) then use a WebDriverWait to wait for the element as shown here:

    CommonUtil.waitForElementVisible(save,properties.getProperty("TimeOutSeconds"));

//This is the actual method in CommonUtil:

public static void waitForElementVisible(WebElement element, String timeoutSecondsAsString) {

    //Temporarily set the default(implicit) timeout to 0 seconds otherwise it will override the timeout specified in this method
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    WebDriverWait wait = new WebDriverWait(driver, Integer.parseInt(timeoutSecondsAsString));
    wait.until(ExpectedConditions.visibilityOf(element));

    //Set the default (implicit) timeout back to the default
    long timeout = Long.valueOf(properties.getProperty("TimeOutSeconds"));
    driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
}

Once I have done the explicit wait for one of the objects, I use the implicit wait for the rest of the objects, so that I can sendKeys, click etc really easily without having to use any wait commands as shown here:

    firstname.sendKeys("firstName");
    lastname.sendKeys("Surname");
    checkout.click();

I have been told that I should not be using implicit waits alongside explicit waits. Is this correct? It seems fine to me and is working perfectly. I would have to write a lot more code if I have to write an explicit wait before clicking or sendingkeys to every field and defeats the object of the Page Object Model being so simple and neat.

Matt
  • 773
  • 2
  • 15
  • 30
  • U actually are not mixing implicit and explicit wait in the same call. When u are using visibility expectedcondition u are setting implicit to 0. If this was not set to 0 you will get unpredictable wait times - https://stackoverflow.com/questions/29474296/clarification-of-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc. You should not have problems if use waits this way – Grasshopper Feb 28 '20 at 12:03
  • Thats what I thought! – Matt Feb 28 '20 at 14:02

0 Answers0