Nightwatch How to use local variable from one function to another

1.2k Views Asked by At

I want to use local variable pass to another function or compare with any variable. Example:

browser
    .waitForElementVisible("//div[@class='col-xs-7 alignR uppercase']//strong", 5000, function () {
      browser
      .pause(500)
      .getText("//div[@class='col-xs-7 alignR uppercase']//strong", function(result){
        console.log('++++++++++++++',result.value);
        let numb1 = result.value;
        numb = numb1.match(/\d/g);
          numb = numb1.join("");
          console.log("value=", numb1);
          return numb1;
  })
    })

.element('xpath', "(//div[@class='row']//div[@class='col-xs-7 alignR'])[1]", function (present) {
  console.log(present);
  if (present.status == 0) {
    //arrange 
    browser
    .pause(500)
    .getText("(//div[@class='row']//div[@class='col-xs-7 alignR'])[1]", function (result) {
      console.log("string", result.value);
      let numb = result.value;
      numb = numb.match(/\d/g);
      numb = numb.join("");
      console.log("value=", numb);
      return numb;
      })


  }

})

I want to compare numb/numb1 or get the total both of them.

1

There are 1 best solutions below

1
iamdanchiv On BEST ANSWER

Well, if you want to keep your test structure as is (pretty suspicious!), then why don't you declare some test-file global variables?

let numb, numb1;

browser
  .waitForElementVisible("//div[@class='col-xs-7 alignR uppercase']//strong", 5000, function () {
    browser.pause(500)
      .getText("//div[@class='col-xs-7 alignR uppercase']//strong", function(result) {

        numb1 = result.value;
        // > do more stuff here if necessary <
      });
    })
  .element('xpath', "(//div[@class='row']//div[@class='col-xs-7 alignR'])[1]", function (present) {
    console.log(present);

    if (present.status == 0) {
      browser.pause(500)
        .getText("(//div[@class='row']//div[@class='col-xs-7 alignR'])[1]", function (result) {

        numb = result.value;
        console.log(`numb & numb1 values: ${numb} & ${numb1}`);
        // > do more stuff with num & numb1 if necessary <
      });
    }
  });

That being said... a finer approach would be to break-down your big steps & extract the logic into single-purpose functions, so you would either use:

Taking into account your current setup, let's consider adding the following custom command for extracting your num1:

❒ getNumb1.js (should reside in test/custom_commands/):

exports.command = function(callback) {
  this.perform((done) => {
    this.api.waitForElementVisible("//div[@class='col-xs-7 alignR uppercase']//strong", 5000, () => {
      this.api.pause(500);
      this.api.getText("//div[@class='col-xs-7 alignR uppercase']//strong", (result) => {
        let numb1 = result.value;
        // > do some other stuff here if necessary <
        callback(numb1);
      });
    });
  });
};

Similarly, add a second custom-command file (getNumb.js), which will return the value of numb. Then, use the two in your test file and do the data manipulation, or data checking (assertions) as you please:

let numb = browser.getNumb();
let numb1 = browser.getNumb1();

// > do something with numb & numb1 here <

!Note: The examples given above are purely didactical! I don't advise creating custom_commands containing test-specific logic. Only general-purpose and/or utility methods (e.g: closeGDPR, addCookie, getHttpCode, etc.) should reside in the custom_commands folder.

Thus, in the current setup, it would be best if you would add commands such as getNumb & getNumb1 inside the commands section of your page-object.