0

I've been using a page object model to find elements and perform actions on them e.g.

public class loginPage extends HelperClass
{

@FindBy(xpath = ("//button[@type='submit']"))
private WebElement loginButton;

public loginPage clickLogin()
{
    loginButton.click()
    return this;
}
}

From within my JUnit test I'd call the function thus:

WebDriver driver = setUpBrowser("FireFox", websiteURL);
loginPage LoginPage = new loginPage(driver);
delay(5000); //delay 5 seconds
LoginPage.clickLogin();

You will have noticed the explicit delay which is not ideal. So I decided to write a wait method like this:

public void waitForElementAndClick(WebElement element, WebDriver driver)
{
    WebDriverWait wait = new WebDriverWait(driver, 30);
    //wait.until(ExpectedConditions.visibilityOf(element));
    wait.until(ExpectedConditions.or
            (
                ExpectedConditions.visibilityOf(element), ExpectedConditions.elementToBeClickable(element) 
            ));
    System.out.println(element);

    element.click();
}

All works well so what is the problem you ask? Well the problem is it is a huge maintenance effort to go and change all the code I've written to accommodate this wait feature. I'll need to change the JUnit calls to include the driver e.g.

LoginPage.clickLogin(driver);

Then in the page object model I'll have to change all the methods as well e.g.

public loginPage clickLogin(WebDriver driver)
{
    waitForElementAndClick(loginButton, driver);
    return this;
}

Is there an easier way of doing this without having to go through all the code applying these changes?

ratsstack
  • 941
  • 1
  • 10
  • 25

2 Answers2

0

public void waitForElementAndClick(WebElement element, WebDriver driver) : Why does this function require a WebDriver object explicitly ?

1) if the wait function is written inside loginPage class, which already has a webdriver constructor argument, the same object can be passed inside the wait function

public class loginPage extends HelperClass
{
WebDriver driver;
//Constructor : 
public loginPage(WebDriver driver) {
this.driver = driver;
 }
}

WebDriverWait wait = new WebDriverWait(driver, 30); : here, the driver variable will be automatically taken

2) If the wait function is written in some other class, the same method can be followed to avoid having a driver variable for wait function

0

See Extend Selenium WebDriver WebElement?

Sers
  • 10,960
  • 2
  • 8
  • 25