问题
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?
回答1:
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)
})
来源:https://stackoverflow.com/questions/31240656/nightwatch-js-cannot-set-a-string-variable-to-an-input-field