Nightwatch.js cannot set a string variable to an input field?

痞子三分冷 提交于 2019-12-25 03:08:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!