I am writing Protractor scripts. My issue is, I am unable to store value into global variables. Neither into global.somevariable, nor into browser.params.somevariable.
I have 2 files: 1. login.js 2. helper.js
From a anonymous method which is inside of helper.js file, I am trying to store the value into a global variable. I am calling this helper.js related method from a js file which is a PageObject file.
In config.js, I declared 2 variables - One with global keyword. Second one with, onPrepare() method,so I can use browser.params.someVar. However, nothing is working.
Inside of that method, the values inside of the vairables are fine. However, when I access the same variables outside of that helper.js, they are null/not correct.
config.js
exports.config =
{
params:
{
tempVar:false
},
onPrepare:function()
{
global.result=false;
}
};
loginpage.js
var loginPage = function()
{
var un = element(by.id('un'));
var helper = new help();
helper.verifyElemExists(un);
console.log(global.result);//False,though promise returned true
console.log(browser.params.tempVar); // This is also false
if(global.result===true)
{
// Code will do something...
}
}
module.exports = login;
helper.js
var helper = function()
{
verifyElemExists = function(elem)
{
elem.isPresent().then(function(res)
{
browser.params.tempVar=res;
global.result =res;
});
}
module.exports = helper;
You need to know the process of nodejs executing protractor script. The root cause of your issue comes from following code snippet:
helper.verifyElemExists(un);
// when nodejs execute above line, all promises generated in this function
// will be added into a list (you can call the list as protractor control flow),
// after all sync script be executed, the protractor control flow start to execute
// the promise in the list one by one in the order as they are added into list.
console.log(global.result);
console.log(browser.params.tempVar);
if(global.result===true)
{
// Code will do something...
}
// when nodejs execute above lines, because all of them are sync script,
// the execution result of then return immediately, and at the time point they are executed,
// the promise of `helper.verifyElemExists(un);`
// have not start to execute(or not complete execute),
// thus the `global.result` and `browser.params.tempVar` are still with init value: false
From above protractor script execution process, you can see when promise and sync code executing complete, a promise list will be created for promise code, but not for sync code. Then promises in list are executed.
To fix your issue, you can wrap above sync code into a promise, so that they are executed after the promise generated in helper.verifyElemExists(un);
helper.verifyElemExists(un);
// browser.getTitle() is a promise which be added into promise list after
// helper.verifyElemExists(un);
browser.getTitle().then(function(){
console.log(global.result);
console.log(browser.params.tempVar);
if(global.result===true)
{
// Code will do something...
}
})
// or change the `helper.verifyElemExists()` to return a promise
var helper = function()
{
verifyElemExists = function(elem)
{
return elem.isPresent().then(function(res)
{
browser.params.tempVar=res;
global.result =res;
});
}
module.exports = helper;
helper.verifyElemExists(un).then(function(){
console.log(global.result);
console.log(browser.params.tempVar);
if(global.result===true)
{
// Code will do something...
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.