我正在使用nightwatch编写selenium测试(正如您所做的)。
我定义了一个回调函数,我希望最终返回一个值(夜视中的自定义命令)。问题是,retValue变量从未改变其值,即使我在console.log中看到了值的变化。
var _someCallBack = function() {
var retValue = false;
browser
.variousNightwatchTasks() //place holder for other things
.execute(function (d) {
//Other custom work injected into the page
return $("span.header_title").length > 0; //I need this true|false
}, [], function(r) {
retValue = r.value; //has the changes; the following outputs as expected
console.log("r = {0} {1}".format(JSON.stringify(r), retValue));
});
//Other things ...
console.log(retValue); //always false which is the problem.
return retValue; //This return needs to execute. I must return the value.
}我确信这是由于我错过了一些javascript的细微差别,所以我如何才能解决这个问题,以及发生了什么?(前者比后者更重要。)
更新: return retValue是必需的。此函数用于自定义nightwatch命令的回调,该命令在if语句中使用返回值。
发布于 2016-07-14 21:39:12
由execute注入的函数是异步执行的,并且在_someCallBack返回时尚未被调用。您可以提供一个回调函数来接收结果:
var _someCallBack = function(callback) {
browser
.variousNightwatchTasks() //place holder for other things
.execute(function (d) {
//Other custom work injected into the page
return $("span.header_title").length > 0; //I need this true|false
}, [], function(r) {
callback(r.value);
});
}
_someCallBack(function(result){
console.log(result);
});https://stackoverflow.com/questions/38384323
复制相似问题