11

I have about 20 spec files and most of them use the same functions repeated in each one. Can I put global functions in the conf.js file that each spec file can use? I read this page http://stackoverflow.com/questions/21320400/protractor-angularjs-global-variables, but it wasn't very helpful and I can't get it working. I tried putting a function in onPrepare, but the spec files can't find it. I also tried doing global.viewByAds = function () {...};

If anyone can help me I'd really appreciate it!

Brendan
  • 1,403
  • 4
  • 18
  • 37

1 Answers1

10

you could simply add a js file and use require

helper.js:

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

in your specs:

//require helper.js at specs
var helper = require('./helper.js');
helper.doSomething()
Alex Arvanitidis
  • 4,403
  • 6
  • 26
  • 36
nilsK
  • 4,323
  • 2
  • 26
  • 40
  • Thanks a lot for the help, but I can't seem to get it working. I made a file in the same directory called `helper.js` and added the `var helper = require('helper');` line to my spec file, but it says it can't find the helper module. Any ideas? – Brendan Apr 11 '14 at 15:11
  • 1
    @Brendan try require('../helper'); if you dont want it there. depends how you set up your project ... – nilsK Apr 11 '14 at 20:14
  • 1
    I had to use require("../helper.js.coffee") but that could just be a coffeescript thing. – map7 Oct 12 '14 at 23:25