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;
.isPresent()method returns a Promise, which means it's asynchronous. It returns immediately, and when its work completes later the function passed into.then()will be called.