Page Object pattern

Here are some important aspects of a good framework design, which we tend to base our decision on:

  • Avoiding duplication of code
  • Tests should be more readable
  • Tests should be easy to maintain
  • Accommodating changes should be easy
  • Enhanced reliability
  • A structure that is easy to scale with the growth of the project

Page Object pattern is about modelling your app's UI as an object. A Page Object wraps the UI of a page with an app-specific API, which allows us to manipulate page elements. Let's understand the same with respect to the following image. The following page serves the purpose of both login and registration. It's the first page that gets displayed when we launch the app; for the sake of our conversation, let's call this a landing page. The page contains UI elements such as skip, mobile number text field, continue button, and Facebook and Google sign in buttons:

When we apply the Page Object concept, the preceding page will typically perform the following services for any user using the app:

  • Skip to home page
  • Register as a new user
  • Log in using Facebook
  • Log in using Google

In this case, the Page Object has the complete knowledge of the page elements and the services it can perform on that page. When we model a page this way, it can hide the UI elements from the consumer of the page and expose only the service one can perform on that UI element via accessor methods. Let's create a sample page class for the shown page:

public class LandingPage {
AppiumDriver appiumDriver;

@FindBy(id = "skip")
private WebElement skipLink;

@FindBy(id = "login_register_view")
private WebElement mobileOrEmailField;

@FindBy(id = "continue_login")
private WebElement continueButton;

@FindBy(id = "fb")
private WebElement fbButton;

@FindBy(id = "sign_in_button")
private WebElement googleButton;

public void skipToHomePage() {
skipLink.click();
}

public void registerByMobileOrEmail(String mobileorEmail) {
mobileOrEmailField.sendKeys(mobileorEmail);
continueButton.click();
}

public void signInByFacebook() {
fbButton.click();
}

public void signInByGoogle() {
googleButton.click();
}
}

So, we have declared the UI elements as private and the accessor methods as public so that they can allow anyone to perform any operation on those UI elements.

These are simple, straightforward actions that a page is doing, and we believe that it belongs to that page class. There are some discussions around as to whether a Page Object should include assertions or not. We will come to this a little later; before that, let's refactor the existing code to create another page class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.48.181