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!

1 Answer 1

9

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()
Sign up to request clarification or add additional context in comments.

3 Comments

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 try require('../helper'); if you dont want it there. depends how you set up your project ...
I had to use require("../helper.js.coffee") but that could just be a coffeescript thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.