javascript - 为什么 waitFor() 忽略我的返回值?

标签 javascript casperjs

此代码不起作用:

casper.waitFor(function check(){
    //Wait for new line to appear in the table and the input box to be emptied
    console.log("In waitFor:");
    console.log(casper.evaluate(function(){return $("table.packagesListing  tr td:contains('some text')").length;}) );
    console.log(casper.evaluate(function(){return $("table.packagesListing  tr td:contains('some text')").length;}) == 1 );
    return
      casper.evaluate(function(){return $("table.packagesListing  tr td:contains('some text')").length;}) == 1
      //&&
      //(casper.evaluate(function(){return $('input#addNewPackage').val();}) == "")
      ;
    },function then(){},
    function onTimeout(){
        this.capture("screenshots/"+label+".failed_timeout_waiting_for_package_add.png");
    });

当我运行它时,我看到的是:

In waitFor:
1
true
In waitFor:
1
true
...
In waitFor:
1
true
In waitFor:
1
true

然后我就超时了!我一定错过了一些非常明显的东西,因为我在这个脚本的其他地方使用 casper.waitFor() ,没有任何问题!

最佳答案

问题在于 return 语句本身在一行上。在 JavaScript 中,分号是可选的(参见 Automatic semicolon insertion & return statements ),因此这与编写相同:

return null;
casper.evaluate(/*...*/) == 1
;

当返回多个 bool 值的 AND 时,最好(并且更易读)将它们分配给本地变量。所以我的代码现在看起来像:

casper.waitFor(function check(){
    //Wait for new line to appear in the table and the input box to be emptied
    var isInTable = casper.evaluate(function(){return $("table.packagesListing  tr td:contains('some text')").length;}) == 1;
    var inputIsEmptied = casper.evaluate(function(){return $('input#addNewPackage').val();}) == "";
    return isInTable && inputIsEmptied;
    },function then(){},
    function onTimeout(){ /*...*/ }
    );

关于javascript - 为什么 waitFor() 忽略我的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29393329/

相关文章:

javascript - 防止 Bootstrap .table-responsive 样式裁剪工具提示

javascript - 什么时候应该使用setScriptTimeout?

javascript - 从 Rhino 调用脚本时,有没有办法在 javascript 函数中包含可选参数?

javascript - 字段动态添加到表单但不提交到服务器

javascript - 浏览器自动缩放基于什么?我如何使用 CSS 或 Javascript 进行偏移?

javascript - php 执行 phantom js 可以工作,但 casperjs 不工作 权限被拒绝

javascript - 如何每小时自动运行CasperJS脚本?

php-casperjs 获取内部文本

javascript - 卡斯帕 : Getting error Cannot dispatch mousedown event on nonexistent selector

javascript - 等待来自 CasperJS 中选择的 <select> 字段的 AJAX 响应