Protractor: initialise Global variable in one js a

2019-08-11 07:52发布

问题:

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.

回答1:

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'
  },