1

I have a function .getText from which I need to return a variable num3 to compare it on the following pages. But the assert at the end is failing because num3 is "undefined".

var num3;
... 
.getText('my_selector', function(result) {
                const num = (number * result.value) * 0.05;
                const num2 = (number * result.value) - num;
                num3 = Math.round(num2 * 100) / 100;

this.expect.element('my_selector').to.have.value.equal(num3); })
...
.assert.containsText('my_selector2', num3) //undefined
Micah L-C
  • 104
  • 1
  • 10
adg ghd
  • 33
  • 1
  • 6
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Hund Feb 17 '18 at 16:45

1 Answers1

2

I found the solution for this problem. We need the .perform function. http://nightwatchjs.org/api#perform

var num3;
... 
.getText('my_selector', function(result) {
            const num = (number * result.value) * 0.05;
            const num2 = (number * result.value) - num;
            num3 = Math.round(num2 * 100) / 100;

this.expect.element('my_selector').to.have.value.equal(num3); })
...
.perform( function(){
          this.api
              .useXpath()//it's necessary if you use Xpath selector
              .assert.containsText('my_selector2', num3)
            })

For more on why this is necessary, read up on the Nightwatch command queue.

Micah L-C
  • 104
  • 1
  • 10
adg ghd
  • 33
  • 1
  • 6