首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >夜莺执行方法

夜莺执行方法
EN

Stack Overflow用户
提问于 2016-07-14 21:10:48
回答 1查看 3.2K关注 0票数 0

我正在使用nightwatch编写selenium测试(正如您所做的)。

我定义了一个回调函数,我希望最终返回一个值(夜视中的自定义命令)。问题是,retValue变量从未改变其值,即使我在console.log中看到了值的变化。

代码语言:javascript
复制
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语句中使用返回值。

EN

回答 1

Stack Overflow用户

发布于 2016-07-14 21:39:12

execute注入的函数是异步执行的,并且在_someCallBack返回时尚未被调用。您可以提供一个回调函数来接收结果:

代码语言:javascript
复制
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);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38384323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档