Leslie Chong
Leslie Chong

Reputation: 1345

Page Objects - level of abstraction for methods

I searched around on the internet a bit but thought I might get some insight by just posting on stackoverflow and seeing if there were any opinions out there.

I'm wondering if anyone has an opinion of which is preferred between these two ways of setting up a page object:

public class LoginPage extends PageObject{
   public void typeEmail(String email){
       driver.findElement(EMAIL_SELECTOR).sendKeys(email);
   }

   public void typePassword(String pw){
       driver.findElement(PASSWORD_SELECTOR).sendKeys(email);
   }

   public void submit(){
       driver.findElement(SUBMIT_SELECTOR).click();
   }
}

...and...

public class LoginPage extends PageObjects{
    public void login(String email, String password){
       driver.findElement(EMAIL_SELECTOR).sendKeys(email);
       driver.findElement(PASSWORD_SELECTOR).sendKeys(email);
       driver.findElement(SUBMIT_SELECTOR).click();
   }
}

Originally, I thought the second way would be better since if the login flow changes for some reason (this is unlikely with a login, but you could theorize this happening for other types of forms), you could update the login() method and this change would affect all the tests which required login.

However, if you want to verify error conditions or more things before submit(), the second solution isn't flexible enough.

Any insights would be welcome.

Upvotes: 1

Views: 357

Answers (2)

Stormy
Stormy

Reputation: 641

Personally I prefer to use another level of abstraction like:

public void typeEmail(String email){
       fillField(EMAIL_SELECTOR, email);
   }

And an implementation in your SeleniumWrapper class

public void fillField(WebElement selector, String text){
       driver.findElement(selector).sendKeys(text);
   }

This makes code more good-looking

This is not directly related to PageObjects, but still this is a way to prettify your code if you dont use BDD, or keyword-driven approach

Upvotes: 0

e1che
e1che

Reputation: 1251

Page object definition : "A PageObject need not represent an entire page. It may represent a section that appears many times within a site or page, such as site navigation."

The keys points of a PageObject :

- The public methods represent the services that the page offers
- Try not to expose the internals of the page
- Generally don't make assertions
- Methods return other PageObjects
- Need not represent an entire page
- Different results for the same action are modelled as different methods

SOURCE

Your two settings aren't PageObject but there is some similarities.

Upvotes: 1

Related Questions