如果页面打开,Casperjs将测试 [英] Casperjs test if page opens
本文介绍了如果页面打开,Casperjs将测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以使用这样的纯幻像:
It can be done with pure phantomjs like this:
var page = require('webpage').create();
var address = 'http://google.com/';
page.open(address, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
console.log('SUCCESS');
}
phantom.exit();
});
但我希望它支持 casperjs test
命令。我想出的最好的是:
but I'd like it to support casperjs test
command. The best I came up with is:
casper.test.begin("Hello, Test!", 1, function(test) {
var page = require('webpage').create();
var address = 'http://google_doesnotexist.com/';
page.open(address, function(status) {
test.assert(status == 'success');
//phantom.exit();
test.done();
});
});
如果页面确实打开,它可以正常工作,但如果页面没有打开,脚本根本不会停止打开。
it works fine if page really opens, but the script does not stop at all if the page does not open.
推荐答案
除了自己的答案,您可以通过 <显式检查状态code>状态功能:
Apart from your own answer, you can explicitly check the status through the status
function:
casper.start("http://www.example.com/", function() {
test.assert(this.status().currentHTTPStatus == 200);
});
甚至更容易使用测试器模块,因为你已经这样做了:
or even easier by using the tester module as you already do this:
casper.start("http://www.example.com/", function() {
test.assertHttpStatus(200);
});
这篇关于如果页面打开,Casperjs将测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文