1

Right to it. I have a global variable which I would like to export, so I can use the value in following specs. But since protractor is not working synchronously, export happens before the variable gets updated to the right value. The action is a click on button where player gets created and I need the username to be exported. Console.log contains the right value and also export happens as it should, only thing, that it exports the hardcoded value or undefined if I set globalUsername = ""; Anyone can help on how to make this export sinchronized, so it will wait for all describes to finish up or the variable to get updated.

describe ("Quick add player", function() {

    it ("New player is created and credentials are displayed", function() {
        browser.ignoreSynchronization = true;

        var playerData1 = playerData.getText().then(function(text) {
            console.log("1: ", text);
            return text.split("\n")[0];
            //console.log(text.split(" ")[1]);
        });


        var globalUsername = "1234";

        playerData1.then(function(text) {
            globalUsername = text.split(" ")[1];
            //expect(globalUsername).not.toEqual("");
            console.log("*****************\n" + globalUsername);    
        });
    });
});
module.exports = globalUsername;
Lex90
  • 39
  • 1
  • 3

2 Answers2

1

Because you are using browser ignore synchronization, your tests will probably have nested then statements because you need to wait for promises to resolve. Instead of creating multiple then statements, you could put them into a single then statement. Also, you could tie your variable to the global namespace with global.username = username or add it to the global browser object with browser.username = username

describe ("Quick add player", function() {

  it ("New player is created and credentials are displayed", function() {
    browser.ignoreSynchronization = true;
    global.username = "1234";
    playerData.getText().then(function(text) {
      console.log("1: ", text);
      global.username = text.split("\n")[0].split(" ")[1];
      console.log("*****************\n" + global.username);
    });
  });
});

// access using global.username tied to the global namespace
// instead of var username = require('thisfile');
cnishina
  • 5,016
  • 1
  • 23
  • 40
1

So this solved my issue, where I export a string and not the variable....searched google with wrong tags. This is placed outside the first describe at the bottom.

module.exports = {
 exportUsername: function () {
  return globalUsername;
 }
};
Lex90
  • 39
  • 1
  • 3