0

Following the Intern user guide, I wrote a simple page object:

define(function(require) {

  function ListPage(remote) {
    this.remote = remote;
  }

  ListPage.prototype = {
    constructor: ListPage,

    doSomething: function(value) {
      return this.remote
        .get(require.toUrl('http://localhost:5000/index.html'))
        .findByCssSelector("[data-tag-test-id='element-of-interest']")
        .click().end();
    }
  };

  return ListPage;
});

In the test, I want to call doSomething twice in a row, like this:

define(function(require) {

  var registerSuite = require('intern!object');
  var ListPage = require('../support/pages/ListPage');

  registerSuite(function() {

    var listPage;

    return {
      name: 'test suite name',

      setup: function() {
        listPage = new ListPage(this.remote);
      },

      beforeEach: function() {
        return listPage
          .doSomething('Value 1')
          .doSomething('Value 2');
      },

      'test function': function() {
        // ...
      }
    };
  });
});

However, when I run the test, I get this error:

TypeError: listPage.doSomething(...).doSomething is not a function

I tried some approaches described in this question, to no avail.

Andrew Liu
  • 319
  • 1
  • 2
  • 13

1 Answers1

1

A better way to implement page objects with Intern is as helper functions rather than Command wrappers. Groups of related helper functions can then be used to create Page Object modules.

// A helper function can take config parameters and returns a function
// that will be used as a Command chain `then` callback.
function doSomething(value) {
    return function () {
        return this.parent
            .findByCssSelector('whatever')
            .click()
    }
}

// ...

registerSuite(function () {
    name: 'test suite',

    'test function': function () {
        return this.remote.get('page')
            // In a Command chain, a call to the helper is the argument
            // to a `then`
            .then(doSomething('value 1'))
            .then(doSomething('value 2'));
    }
}
jason0x43
  • 3,363
  • 1
  • 16
  • 15
  • This might be true, but I'm surprised that such complicated code (e.g. a function that returns a function that returns a Command) is required. In the meantime, I am trying WebdriverIO instead, which supports synchronous-style code. – Andrew Liu Apr 13 '16 at 13:31