如何在Angularjs量角器中使用全局函数?

时间:2014-04-11 14:55:52

标签: javascript angularjs testing protractor

我有大约20个spec文件,其中大多数使用相同的函数重复。我可以将全局函数放在每个spec文件可以使用的conf.js文件中吗?我读了这页http://stackoverflow.com/questions/21320400/protractor-angularjs-global-variables,但它不是很有帮助,我无法让它发挥作用。我尝试在onPrepare中放置一个函数,但是spec文件找不到它。我也试过global.viewByAds = function () {...};

如果有人能帮助我,我真的很感激!

1 个答案:

答案 0 :(得分:10)

你可以简单地添加一个js文件并使用require

helper.js:

module.exports = {
  foo: 'bar',
  doSomething: function () {
    return 1+1;
  }
};

在你的规格中:

//require helper.js at specs
var helper = require('./helper.js');
helper.doSomething()