Intern Page Objects

intern

A page object is a class that we should create to represent a page.  It should 
include the methods, and locators for UI elements.  Using page objects means 
that if the UI of our application is modified, we only need to make a change 
in the page object to fix all your tests. Without page objects, every time 
the views in your application change, you’d need to touch every single test 
that interacts with that part of the UI.

Once you’ve written a page object, your tests will use the page object to 
interact with a page instead of the low-level methods of the this.remote object.

For example, a page object for the index page of a Web site could be written 
like this:

// in tests/support/pages/IndexPage.js
define(function (require) {
  // the page object is created as a constructor
  // so we can provide the remote Command object
  // at runtime
  function IndexPage(remote) {
    this.remote = remote;
  }

  IndexPage.prototype = {
    constructor: IndexPage,

    // the login function accepts username and password
    // and returns a promise that resolves to `true` on
    // success or rejects with an error on failure
    login: function (username, password) {
      return this.remote
        // first, we perform the login action, using the
        // specified username and password
        .findById('login')
        .click()
        .type(username)
        .end()
        .findById('password')
        .click()
        .type(password)
        .end()
        .findById('loginButton')
        .click()
        .end()
        // then, we verify the success of the action by
        // looking for a login success marker on the page
        .setFindTimeout(5000)
        .findById('loginSuccess')
        .then(function () {
          // if it succeeds, resolve to `true`; otherwise
          // allow the error from whichever previous
          // operation failed to reject the final promise
          return true;
        });
    },

    // …additional page interaction tasks…
  };

  return IndexPage;
});

Then, the page object would be used in tests instead of the this.remote object:

'successful login': function () {
  var indexPage = new IndexPage(this.remote);
  return indexPage
    .login('test', 'test')
    .then(function (loggedIn) {
      assert.isTrue(loggedIn,
        'Valid username and password should log in successfully');
      });
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License