saurabh04
saurabh04

Reputation: 41

Passing global variable from onPrepare() in protractor

How to pass global variable from conf inside onPrepare(). So that it can be use in different specs.

Inside onPrepare() because i am getting value from function which i am calling in onPrepare(). so want to make that value as global. so that it can be use in all spec.

Upvotes: 1

Views: 4401

Answers (4)

larry
larry

Reputation: 1

Will it work when you set shardTestFiles parameter to true in config file? I have problem with global values when I set it to true. Works when is false.

Upvotes: -1

Ram Pasala
Ram Pasala

Reputation: 5231

I have posted the answer in gitter, you can check that out basically you should use 'global'

helper.js --> common functions/methods you want to execute

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

config.js

   var helper = require('./helper.js’);

   onPrepare: function () {

  global.output = helper.doSomething();

  },

spec.js

    describe(‘global variable test’, function() {

    it(’should print global variable’, function() {

     console.log(output);

     });
     });

Upvotes: 1

Xotabu4
Xotabu4

Reputation: 3091

Here is how i did globals - i am using multiple browsers in tests, so i need some shortcuts to access both browsers easy:

onPrepare: function() {
    // Making manager and user globals - they will be accessible in all tests.
    global.manager = browser;
    global.user = browser.forkNewDriverInstance();

    ...
    //Making Expected Conditions global since it used widely.
    global.EC = protractor.ExpectedConditions;

Then it will be accessible everywhere just by calling

manager.$('blabla').click();
user.$('blabla').click();

user.wait(EC.visiblityOf($('foo')), 5000, 'foo should be visible');

Hope this helps!

Upvotes: 3

Anton Kirzyk
Anton Kirzyk

Reputation: 140

You can use or set browser.params object in onPrepare function:

onPrepare: function () {
    browser.params.YOUR_PARAM = 'VALUE';
}

Using in spec:

it('should...', function () {
    expect(browser.params.YOUR_PARAM).toEqual('VALUE');
});

Upvotes: 3

Related Questions