Loop of test suites in a CasperJS test file results in random failures in the shell

浪子不回头ぞ 提交于 2019-12-06 10:31:34

问题


I would like check the title of several website. So, when I want do that with the "test object", I randomly get different results. I mean, when I run the shell command "casperjs test ..." :

  • Sometimes, my shell displays all the test (its Ok!)
  • Sometimes (screenshot #1), all tests is not done
  • Sometimes (screenshot #2), I get a fail "check title" test, the previous open(url) didn't load the new url (?!)

The screenshot of my shell:

My code:

casper.options.loadImages = false;

var lines = [
  "http://www.jeuxvideo.com;JEUXVIDEO.COM - La Référence des Jeux Vidéo sur PC et Consoles !",
  "http://www.google.fr;Google",
  "http://casperjs.org/;CasperJS, a navigation scripting and testing utility for PhantomJS and SlimerJS",
  "http://en.wikipedia.org/wiki/Main_Page;Wikipedia, the free encyclopedia",
  "http://stackoverflow.com/;Stack Overflow",
  "http://9gag.com/;9GAG - Why So Serious?",
  "http://eu.blizzard.com/fr-fr/;Blizzard Entertainment",
  "http://openclassrooms.com/;OpenClassrooms, Le Site du Zéro - Les cours les plus ouverts du Web",
  "http://lesjoiesducode.fr/;Les joies du code ",
  "http://www.developpez.com/;Developpez.com, le club des développeurs et IT Pro",
];

function main(){
  this.each(lines, function(self, line){
    var tab = line.split(";");
    casper.test.begin("test : "+tab[0], 1, function suite(test){
      casper.start().then(function(){

        this.then(function(){
          this.open(tab[0]);
        });

        this.then(function (){
          this.echo(this.currentHTTPStatus);
          test.assertTitle(tab[1]);
        });

      }).run(function(){
        test.done();
      });
    });
  });
}

casper.start().then(main).run();

My versions :

casperjs version : 1.1.0-beta3
phantomjs version : 1.9.7

Why sometimes, all tests aren't done and why sometimes le new url isn't loaded ? (whereas the open(url) is in a .then)


回答1:


You should use start and run only once in your test case and never outside of casper.test.begin. You don't need the main function as a step for then. Also, you can further condense your script.

  lines.forEach(function(line){
    var tab = line.split(";");
    casper.test.begin("test : "+tab[0], 1, function suite(test){
      casper.start(tab[0]).then(function (){
        this.echo(this.currentHTTPStatus);
        test.assertTitle(tab[1]);
      }).run(function(){
        test.done();
      });
    });
  });


来源:https://stackoverflow.com/questions/27750679/loop-of-test-suites-in-a-casperjs-test-file-results-in-random-failures-in-the-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!