Page Classes

Now that we understand the page object pattern, let's create the page for our example scenario.

First let us create a package named pages inside src->test->java. i.e. at the same level as features and step packages.

Once the package is ready, we will create three page classed as discussed one each for Landing page, SignUp page and Home page.

Also, all the code that we wrote in example steps can be moved to these newly created pages.

Let us do it one by one.

LandingPage

  • It should encapsulate the sign up link.
  • It should encapsulate the action to click on the sign up link.

Here is how it should look.

public class LandingPage {

WebDriver driver;

public LandingPage(WebDriver driver) {
    this.driver = driver;
}


public void she_chooses_to_sign_up() throws Throwable {
    driver.findElement(By.linkText("Sign up")).click();
}
}

Notice the constructor for this class. It takes a web driver instance. This is needed because we are writing code to click a link using the driver in this class.

And similarily, all the other page classes would also need the driver as they would be having methods to work with controls on the pages like enter some text, select from a list, click a button etc.

On similar lines,

SignupPage

  • should encapsulate all the user detail input controls like firstName, lastName etc
  • should encapsulate all the methods that would allow entering data into these controls

So SignupPage class should look like this.

public class SignupPage {

WebDriver driver;

public SignupPage(WebDriver driver) {
    this.driver = driver;
}

public void she_provides_the_first_name_as(String firstName) throws Throwable {
    driver.findElement(By.id("user_first_name")).sendKeys(firstName);
}


public void she_provides_the_last_name_as(String lastName) throws Throwable {
    driver.findElement(By.id("user_last_name")).sendKeys(lastName);
}

public void she_provides_the_email_as(String email) throws Throwable {
    driver.findElement(By.id("user_email")).sendKeys(email);
}

public void she_provides_the_password_as(String password) throws Throwable {
    driver.findElement(By.id("user_password")).sendKeys(password);
}

public void she_provides_the_confirm_password_again_as(String confirmPassword) throws Throwable {
    driver.findElement(By.id("user_password_confirmation")).sendKeys(confirmPassword);
}


public void she_signs_up() throws Throwable {
    driver.findElement(By.name("commit")).click();
}
}

HomePage

  • should encapsulate the logout link
  • should encapsulate the methods that tells us whether the signout link is displayed or not.

Here is how this class would look.

public class HomePage {

WebDriver driver;

public HomePage(WebDriver driver) {
    this.driver = driver;
}

public boolean isSignOutLinkDisplayed() throws Throwable {
   return driver.findElement(By.cssSelector("a[href='/users/sign_out']")).isDisplayed();
}
}

Screen

results matching ""

    No results matching ""