本文介绍了CasperJS测试文件中的测试套件循环导致外壳中的随机失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想检查几个网站的标题。因此,当我要使用测试对象执行此操作时,我会随机获得不同的结果。我的意思是,当我运行外壳命令 casperjs test ...时: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 ..." : 有时,我的外壳会显示所有测试(正常!) 有时(屏幕截图#1),未完成所有测试 有时(屏幕截图#2),我收到了失败的检查标题 测试,以前的open(url)没有加载新的URL(?!)我的shell的屏幕截图:The screenshot of my shell:我的代码: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();我的版本:casperjs version : 1.1.0-beta3phantomjs version : 1.9.7为什么有时没有完成所有测试,为什么有时没有加载新的URL? (而open(url)在.then中)Why sometimes, all tests aren't done and why sometimes le new url isn't loaded ? (whereas the open(url) is in a .then)推荐答案您应该使用 start 和运行仅在测试案例中一次,并且永远不会在 casper.test.begin之外。您不需要 main 函数作为 then 的步骤。另外,您可以进一步压缩脚本。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(); }); }); }); 这篇关于CasperJS测试文件中的测试套件循环导致外壳中的随机失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 09:19
查看更多