Passing global variable from onPrepare() in protractor

China☆狼群 提交于 2020-03-20 12:49:54

问题


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.


回答1:


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');
});



回答2:


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!




回答3:


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);

     });
     });



回答4:


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.



来源:https://stackoverflow.com/questions/37742217/passing-global-variable-from-onprepare-in-protractor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!