Home > Software engineering >  How do I declare a global WebDriverWait in the BaseTest class?
How do I declare a global WebDriverWait in the BaseTest class?

Time:12-07

I would like to know how can I declare a global WebDriverWait in my BaseTest Java class and to be able to use the .until(ExpectedConditions) methods and such as I would when declaring WebDriverWait inside each Page class.

I would also like to know how to do the same for multiple classes if it's going to be different.

I have tried the following but it's most likely not likely to how it's supposed to be done

BaseTest.java

@Getter
@Setter
@AllArgsConstructor
public class BaseTest {

    private WebDriver webDriver;

    private WebDriverWait wait;

    public void waitForElement() {

         wait = new WebDriverWait (webDriver, Duration.ofSeconds(10));
    }
}

PageFunctionality.java

public PageFunctionality(WebDriver webDriver, WebDriverWait wait) {
    super(webDriver, wait);
    PageFactory.initElements(webDriver, this);
}

CodePudding user response:

You can use the below common method for Explicit Wait, but you can pass the parameters as - WebDriver, locator and timeout only, you can't pass the ExpectedConditions options.

public static WebElement explicitWait(WebDriver driver, By locator, long timeout) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeout));
    wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    return driver.findElement(locator);
}

while calling this method:

explicitWait(driver, By.xpath("<xpath>"), 2).click();

My suggestion is you can create 1 or 2 explicit wait common method (based on the above method) with mostly used ExpectedConditions in your framework.

  • Related