问题
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