Nightwatch:覆盖页面对象中的功能

时间:2018-04-04 10:23:27

标签: javascript automation nightwatch.js

我在Nightwatch中创建了一个页面对象模型,但我想覆盖一些自定义功能。我怎么做到这一点?

module.exports = {
url: 'http://localhost:63916/Login/Login', 

commands: [{
    navigateToImportBatch() {
        this
            .click('@importManager')
            .click('@importBatch')

            return this.api;
    }
};

如何覆盖navigateToImportBatch函数?谢谢。

1 个答案:

答案 0 :(得分:1)

全局覆盖:

// your current module to override, e.g. loginPageModel.js
let module = {
  url: 'http://localhost:63916/Login/Login', 

  commands: [{
    navigateToImportBatch() {
      this.click('@importManager')
        .click('@importBatch');

      return this.api;
    }
  }]
};
module.exports = module;

然后定义一个新模块,覆盖您需要的任何命令并将其导出。

// loginOverride.js
let LoginOverride = require('./loginPageModel'); // whatever the path to your module is called

LoginOverRide.commands[0].navigateToImportBatch = function() {
   // your new implementation
}

module.exports = LoginOverride;

然后只需在步骤定义中导入覆盖基本实现的模块。

let LoginModel = require('./loginOverride');

// use as normal

或者,您可以在步骤定义中使用相同的策略,而无需定义新的页面模型,只需从步骤定义中覆盖实现。