Subhash Subhash - 4 years ago 139
Node.js Question

Protractor: initialise Global variable in one js and use the same var in other js

I am using protractor to test angularJS apps. I have declared a variable 'logDir' in my test js and want to use it as global variable in other js.

//test.js

var regex = /(.*automation)/;
var result = regex.exec(__dirname);
var baseDir = result[0];
var libDir = baseDir + '/lib';


I want to use libDir variable globally in other js.

I am new to protractor and need help. If more clarification needed, let me know.

Thanks in advance.

Answer Source

You can dump the variable you need globally onto Protractor browser object and use it anywhere in the Protractor run Environment

browser.libDir = baseDir + '/lib';

And then if you need it in any other test case you can directly use it as browser.libDir

A more professional way to handle this would be declaring global variables in onPrepare().

  onPrepare: function() {
       browser.libDir = baseDir + '/lib'
  },
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download