1

This code is not working:

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");
    });

When I run it what I see is:

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

And then I get the timeout! I must be missing something really obvious, because I am using casper.waitFor() elsewhere in this script, with no problems!

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • What happens when you do this only once and use a variable, not three times? – Artjom B. Apr 01 '15 at 14:26
  • 1
    @ArtjomB. Got it. I'd over-tidied up the question - I've just put back the bug. I had `return` on a line by itself: a habit I have in C++ and PHP when returning a logical AND or OR of multiple boolean tests. But in JavaScript the semi-colon is optional. Do you think it is worth a self-answer, or shall I just delete this question? – Darren Cook Apr 01 '15 at 14:45
  • @DarrenCook I think we should keep it, unless there's already a question that gives the same information, and if so, mark as duplicate. If there isn't, self-answer. – Nic Apr 01 '15 at 14:53
  • possible duplicate of [Automatic semicolon insertion & return statements](http://stackoverflow.com/questions/12745743/automatic-semicolon-insertion-return-statements) – Artjom B. Apr 01 '15 at 14:57
  • @QPaysTaxes Good point, there's a duplicate. – Artjom B. Apr 01 '15 at 14:57
  • That other question is more an explanation of why a return shouldn't be on a line by itself. I'd certainly never have found it searching for this CasperJS problem! – Darren Cook Apr 01 '15 at 15:01
  • @DarrenCook And now there is a link to it. If a future visitor happens to have the same problem in CasperJS, they will be shown the framework agnostic version of the problem. After all, it's not wise to waste your breath if it was sufficiently discussed somewhere else. Note that questions closed as duplicate are not automatically deleted as long as they have a non-negative score (>-1). – Artjom B. Apr 01 '15 at 15:19

1 Answers1

0

The problem is the return statement on a line by itself. In JavaScript, semi-colons are optional (see Automatic semicolon insertion & return statements), so that is the same as writing:

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

When returning the AND of multiple booleans it is better (and more readable) to assign them to local vars. So my code now looks like:

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(){ /*...*/ }
    );
Community
  • 1
  • 1
Darren Cook
  • 27,837
  • 13
  • 117
  • 217