Dale
Dale

Reputation: 191

Trouble Using Selenium Page Object Pattern Mix with WebDriverWait

I used a selenium page object in my project, and also used WebDriverWait to wait until the element is added.

@FindBy(how = How.ID, using = "username")
private WebElement username;

@FindBy(how = How.ID, using = "password")
private WebElement password;

public void login(String username, String password) {
    WebDriverWait waiter = new new WebDriverWait(driver, 5, 200);
    waiter.until(ExpectedConditions.presenceOfElementLocated(
       By.id("username")));
    this.username.sendKeys(username)
}

Two questions:

  1. Since we only need:

    waiter.until(ExpectedConditions.presenceOfElementLocated( By.id("username"))).sendkey(username);

and not the page object username to return the element you wanted, is the page object pattern useless?

  1. If the page object pattern is necessary, how do I deal with the string "username"? Do I need a new Class for maintaining constants such as:

    public final static String USERNAME = "username";

so we can call it on my page?

Upvotes: 1

Views: 572

Answers (2)

Breaks Software
Breaks Software

Reputation: 1761

"...is the page object pattern useless?"

Hardly! For instance, your login method has not yet entered a value in the password field. so, to login without a LoginPage.login() method, you would at miniumum need two long lines of code in each test to log in.

Additional value can be added to your LoginPage.login() method if it recognizes things like expected errors on the login page, when it can throw custom exceptions that your test can then respond to. I bet that there are other things on that login page that you may need to interact with, so additional methods to add to the LoginPage class.

Do I need a new Class for maintaining constants

I generally prefer to keep strings for the locators inside the class where they will be used. So, I would have a private variable in LoginPage for USERNAME_ID, which you would then use elsewhere.

Upvotes: 1

Solomon Raja
Solomon Raja

Reputation: 1790

Use methods like

waiter.until(ExpectedConditions.visibilityOf(username)); 
//org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf(WebElement element)

instead of

waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
//org.openqa.selenium.support.ui.ExpectedConditions.presenceOfAllElementsLocatedBy(By locator)

Upvotes: 0

Related Questions