自casperjs 1.0.4
的旧版和稳定版以来。如果我只拨打casper.exit()
,我就可以退出整个测试。但是在1.1 beta3
上,如果您在测试套件上部署了多个步骤,则调用exit()
将不再有效。
基本上我的测试是在失败后调用exit()
。我试图使用--fast-fail
,但它是一样的。另请注意我正在使用extend方法。
以下是示例代码,我使用Casper
扩展Google
以抓取链接。此测试应该失败并完全退出测试而不再打印链接。
var Casper = require('casper').Casper;
var utils = require('utils');
var url = "http://google.com";
function Google() {
Google.super_.apply(this, arguments);
}
utils.inherits(Google, Casper);
var links = [];
Google.prototype.getLinks = function() {
var links = document.querySelectorAll("h3.r a");
return Array.prototype.map.call(links, function(e) {
try {
// google handles redirects hrefs to some script of theirs
return (/url\?q=(.*)&sa=U/).exec(e.getAttribute("href"))[1];
} catch (err) {
return e.getAttribute("href");
}
});
};
var google = new Google();
google.test.begin("Google links test", 1, function(test){
google.start(url, function() {
// search for 'casperjs' from google form
this.fill('form[action="/search"]', { q: "casperjs" }, true);
});
google.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(this.getLinks);
// now search for 'phantomjs' by fillin the form again
this.waitForSelector('fform[action="/search"]', function(){
this.fill('form[action="/search"]', { q: "phantomjs" }, true);
test.pass("Form found")
}, function(){
test.fail('form not found');
test.abort();
this.exit();
}, 5000);
});
google.then(function() {
// aggregate results for the 'phantomjs' search
links = links.concat(this.evaluate(this.getLinks));
});
google.run(function() {
// echo results in some pretty fashion
this.echo(links.length + " links found:");
this.echo(" - " + links.join("\n - "));
this.exit();
});
});