0

I am testing some JavaScript code with Nightwatch.js. I want to read a value from an input tag, increase or decrease it by 1 and then write it back to the input tag. Therefore I wrote this code:

.getValue('#inputConfigReading', function(result){
    val = parseInt(result.value);
    if (val % 2 == 0)
        val++;
    else
        val--;
    val = val+'';
})
.clearValue('#inputConfigReading')
.setValue('#inputConfigReading', val)

I checked it out. The variable val has the correct value after the command val = val+'';. But anyway when I run the code Nightwatch is writing undefined into the input field. Why?

Garrarufa
  • 1,145
  • 1
  • 12
  • 29

1 Answers1

3

Nightwatch is executing those last two steps before val is defined. Those last two steps should be inside the callback function of .getValue:

.getValue('#inputConfigReading', function(result){
    val = parseInt(result.value);
    if (val % 2 == 0)
        val++;
    else
        val--;
    val = val+'';
    browser.clearValue('#inputConfigReading')
    browser.setValue('#inputConfigReading', val)
})
user3613154
  • 101
  • 1