title: CasperJS/PhantomJS, some tests fail randomly
body: <p>I have some scenarios in which some tests fail randomly (maybe 5/65), often with svg, auto-completion, etc... The CPU is fine though, so it comes from my way to script it.</p>
  
  <p>But I don't see how to alter them to make them robust and reliable. It's when I use wait() function -> sometimes I have the timeout error, sometimes it passes. </p>
  
  <p>The strangest is that when i split my script in two script, there are less fails than with one ....</p>
  
  <hr>
  
  <p><strong>Add waitForResource try</strong></p>
  
  <p><em>Thx to Artjom B. for the help.</em></p>
  
  <pre><code>casper.test.begin('\n********* Navigation on directories : ***********', 4,{
  setUp: function(test) {
      setLevel("normal");
  },
  
  tearDown: function(test) {
      getJSON();
  },
  
  test: function(test){
      "use strict";
      var url = 'http://www.linternaute.com/';
      casper.start()
     .thenOpen(url + "/ville/", function(){
          if(this.exists('.ui-dialog-titlebar-close')){this.click('.ui-dialog-titlebar-close');}
          if(this.exists('#top_oas')){this.click('#top_oas &gt; .right');}
          this.test.assertExists(".jODMainSearch", "Search toolbar present");
          this.sendKeys(".jODMainSearch", "Ren");
          this.click(".submit.search");
      })
      .waitForSelector(".odListGlossary", function(){
          this.test.assertExists(".odListGlossary", "Search result ok");
          this.clickLabel("Rennes (35000)");
      })
      .waitFor(function check() {
              return this.fetchText('h1').indexOf("Rennes") !== -1;
              }
              , function then() {
                  this.test.assertSelectorHasText("h1", "Rennes");
              }
      )
      //auto-completion
      .thenOpen(url + "/ville/", function(){
  
          //this.waitForResource(/odmainsearch/, function(){
              /*var fs = require('fs');
              fs.write("results1.html", this.getPageContent(), 'w');
              this.wait(8000, function(){
                  var fs = require('fs');
              fs.write("results2.html", this.getPageContent(), 'w');
              });*/
  
              /*casper.waitFor(function check() {
                      return this.fetchText('script').indexOf("fn.odMainsearch=function") !== -1;
                      }
                      , function then() {
                          this.echo('cc');
                          this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
                      }
              );*/
          /*** the changes ***/   
          this.waitForResource(function testResource(resource) {
              return resource.url.indexOf("http://static.ccmbg.com/www.linternaute.com/asset/js?m=odmainsearch") !== -1;
          }, function onReceived() {
              this.echo('JS for auto-completion present');
              this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
          });
          this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
              this.mouse.move('a[href="/ville/rennes/ville-35238"]');
              this.click('a[href="/ville/rennes/ville-35238"]');
          });
      })
      .waitFor(function check() {
              return this.fetchText('h1').indexOf("Rennes") !== -1;
              }
              , function then() {
                  this.test.assertSelectorHasText("h1", "Rennes");
              }
      )
      .run(function() {
              this.test.comment('--- Done ---\n');
              test.done();
      });
  }
  });
  </code></pre>
  
  <hr>
  
  <p><strong>First try :</strong></p>
  
  <p>Here an example which randomly fails :</p>
  
  <pre><code>casper.test.begin('\n********* Navigation on directories : ***********', 4,{
  setUp: function(test) {
      //setLevel("normal");
  },
  
  tearDown: function(test) {
      //getJSON();
  },
  
  test: function(test){
      "use strict";
      var url = 'http://www.linternaute.com/';
      casper.start()
     .thenOpen(url + "/ville/", function(){
          //close some ads
          if(this.exists('.ui-dialog-titlebar-close')){this.click('.ui-dialog-titlebar-close');}
          if(this.exists('#top_oas')){this.click('#top_oas &gt; .right');}
          this.test.assertExists(".jODMainSearch", "Search toolbar present");
          this.sendKeys(".jODMainSearch", "Ren");
          this.click(".submit.search");
      })
      .waitForSelector(".odListGlossary", function(){
          this.test.assertExists(".odListGlossary", "Search result ok");
          this.clickLabel("Rennes (35000)");
      })
      .waitFor(function check() {
              return this.fetchText('h1').indexOf("Rennes") !== -1;
              }
              , function then() {
                  this.test.assertSelectorHasText("h1", "Rennes");
              }
      )
      //auto-completion
      .thenOpen(url + "/ville/", function(){
          this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
          this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
              this.mouse.move('a[href="/ville/rennes/ville-35238"]');
              this.click('a[href="/ville/rennes/ville-35238"]');
          });
      })
      .waitFor(function check() {
              return this.fetchText('h1').indexOf("Rennes") !== -1;
              }
              , function then() {
                  this.test.assertSelectorHasText("h1", "Rennes");
              }
      )
      .run(function() {
              this.test.comment('--- Done ---\n');
              test.done();
      });
  }
  });
  </code></pre>
  
  <p>And if I take just the end of the script (where it randomly fails in the first example, I've commented the beginning) :</p>
  
  <pre><code>casper.test.begin('\n********* Navigation on directories : ***********', 1,{
  setUp: function(test) {
      //setLevel("normal");
  },
  
  tearDown: function(test) {
      //getJSON();
  },
  
  test: function(test){
      "use strict";
      var url = 'http://www.linternaute.com/';
      casper.start()
     /*.thenOpen(url + "/ville/", function(){
          if(this.exists('.ui-dialog-titlebar-close')){this.click('.ui-dialog-titlebar-close');}
          if(this.exists('#top_oas')){this.click('#top_oas &gt; .right');}
          this.test.assertExists(".jODMainSearch", "Search toolbar present");
          this.sendKeys(".jODMainSearch", "Ren");
          this.click(".submit.search");
      })
      .waitForSelector(".odListGlossary", function(){
          this.test.assertExists(".odListGlossary", "Search result ok");
          this.clickLabel("Rennes (35000)");
      })
      .waitFor(function check() {
              return this.fetchText('h1').indexOf("Rennes") !== -1;
              }
              , function then() {
                  this.test.assertSelectorHasText("h1", "Rennes");
              }
      )*/
      //auto-completion
      .thenOpen(url + "/ville/", function(){
          this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
          this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
              this.mouse.move('a[href="/ville/rennes/ville-35238"]');
              this.click('a[href="/ville/rennes/ville-35238"]');
          });
      })
      .waitFor(function check() {
              return this.fetchText('h1').indexOf("Rennes") !== -1;
              }
              , function then() {
                  this.test.assertSelectorHasText("h1", "Rennes");
              }
      )
      .run(function() {
              this.test.comment('--- Done ---\n');
              test.done();
      });
  }
  });
  </code></pre>
  
  <p>So what's the problem, do you have an idea? -> Two step functions in the stack : it often fails, only one : it sometimes fails... </p>