如何链接Intern Page Object函数调用?

时间:2016-04-08 22:47:35

标签: javascript deferred intern

Intern user guide之后,我写了一个简单的页面对象:

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;
});

在测试中,我想连续两次调用doSomething,如下所示:

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() {
        // ...
      }
    };
  });
});

然而,当我运行测试时,我收到此错误:

  

TypeError:listPage.doSomething(...)。doSomething不是函数

我尝试了this question中描述的一些方法,但没有用。

1 个答案:

答案 0 :(得分:1)

使用Intern实现页面对象的更好方法是作为辅助函数而不是Command包装器。然后可以使用相关辅助函数组来创建页面对象模块。

// 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'));
    }
}
相关问题