Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use global functions in Angularjs Protractor?

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!

like image 823
Brendan Avatar asked Apr 11 '14 14:04

Brendan


People also ask

What is global function in JavaScript?

The global object in JavaScript is an always defined object that provides variables and functions, and is available anywhere. In a web browser, the global object is the window object, while it is named global in Node. js. The global object can be accessed using the this operator in the global scope.


1 Answers

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()
like image 81
nilsK Avatar answered Oct 24 '22 21:10

nilsK