Need Help in creating a function to wait

public void testLogin_ExplicitWait() {
WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.presenceOfElementLocated(loginScreen)).click();
wait.until(ExpectedConditions.presenceOfElementLocated(username)).sendKeys(AUTH_USER);
wait.until(ExpectedConditions.presenceOfElementLocated(password)).sendKeys(AUTH_PASS);
wait.until(ExpectedConditions.presenceOfElementLocated(loginBtn)).click();
wait.until(ExpectedConditions.presenceOfElementLocated(getLoggedInBy(AUTH_USER)));

}

Hi I’m using this explicit wait to do my task, I want to create a function to use this command with that function

Can anyone help, thanks in advance

The is not much to do… just copy it to an external function and call it with the right params… basically the only param should be of ExpectedCondition class.

  • use java generics.

example:

public <T> T myWaitFunction(ExpectedCondition<T> condition) {
return wait.until(condition);
}

in your test method run:
myWaitFunction(presenceOfElementLocated(loginScreen)).click();

and if you only use presenceOfElementLocated by the way, you should not. you should use elementToBeClickable expected condition before clicking an element. but, if you decide to use only presenceOfElementLocated you can do like so:

public <T> T myWaitForPresenceFunction(WebElement element) {
return wait.until(presenceOfElementLocated(element));
}

and then from your test method just call:
myWaitForPresenceFunction(loginScreen).click();

For me, I just create a util with all the common actions (click, getText etc…)
then just call clickElement(loginButton) for example.