1

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;  
2
  • 2
    Looks like your .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. Commented Jan 6, 2019 at 14:53
  • Yes Pointy, not sure how to make this work, first the variables are called, then the anonymous function is called - this is making my tests fail. Commented Jan 6, 2019 at 15:10

1 Answer 1

2

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...
    }   
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Yong, for your exceptionally well written answer - It resolved my issue. I struggled 4 hours to resolve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.