量角器在同一模块中调用导出的函数

时间:2022-01-17 23:12:30

I'm newer to using Protractor for automation, so forgive me if this ends up being a dumb question. I have a helper.js module with a bunch of functions that I or other team members can use. One of the functions from helper.js needs to call to one of the existing functions in the module.

我是较新的使用Protractor进行自动化,所以请原谅我,如果这最终成为一个愚蠢的问题。我有一个helper.js模块,其中包含一些我或其他团队成员可以使用的函数。 helper.js的一个函数需要调用模块中的一个现有函数。

Is this possible? I have tried several different ways to do this and so far none have worked other than to break the helper functions into a separate js file that I need to call to.

这可能吗?我已经尝试了几种不同的方法来实现这一点,到目前为止,除了将辅助函数分解为我需要调用的单独的js文件之外,没有其他方法可行。

Example: helper.js:

module.exports = {
    newbrowsertab: function(){
        <code>
    },
    anotherfunction: function(){
        <code>
        <call to newbrowsertab();>
        <code>
     },
     anotherfunction2: function(){
         <code>
     }
};

In the call to the newbrowsertab function, I've tried:

在调用newbrowsertab函数时,我尝试过:

  • module.newbrowsertab();
  • this.newbrowsertab();
  • self.newbrowsertab();

2 个解决方案

#1


0  

You could use Prototypal inheritance then:

您可以使用Prototypal继承:

// helper.js functions

// create object
var Util = function() {};

// extend object
Util.prototype.enterPassword = function() {
    // code
};

// extend object
Util.prototype.clickLogin = function() {
    // code
};

// use `this` to call functions in same module
Util.prototype.fullLogin = function() { // extend object
    this.enterPassword();
    this.clickLogin();
};

module.exports = new Util();

Then in your test file:

然后在您的测试文件中:

var Util = require('./path/to/helper.js);

Util.fullLogin();

etc...

#2


0  

Expanding on the prototypal convention.

扩展原型会议。

Any functions that are only helpers for other exported functions could be named with an underscore and declared to be executed later.

任何仅作为其他导出函数的辅助函数的函数都可以使用下划线命名,并声明稍后执行。

function _helperFunction(){
  // do something
  // return something
}
var exposedFunction = function() {
  // do something
  var x = _helperFunction();
  // Do something else
}


module.exports = {
  exposedFunction : exposedFunction
};

#1


0  

You could use Prototypal inheritance then:

您可以使用Prototypal继承:

// helper.js functions

// create object
var Util = function() {};

// extend object
Util.prototype.enterPassword = function() {
    // code
};

// extend object
Util.prototype.clickLogin = function() {
    // code
};

// use `this` to call functions in same module
Util.prototype.fullLogin = function() { // extend object
    this.enterPassword();
    this.clickLogin();
};

module.exports = new Util();

Then in your test file:

然后在您的测试文件中:

var Util = require('./path/to/helper.js);

Util.fullLogin();

etc...

#2


0  

Expanding on the prototypal convention.

扩展原型会议。

Any functions that are only helpers for other exported functions could be named with an underscore and declared to be executed later.

任何仅作为其他导出函数的辅助函数的函数都可以使用下划线命名,并声明稍后执行。

function _helperFunction(){
  // do something
  // return something
}
var exposedFunction = function() {
  // do something
  var x = _helperFunction();
  // Do something else
}


module.exports = {
  exposedFunction : exposedFunction
};