Javascript 如何在casperjs中将参数传递给函数?
我已将所有图像url存储在一个数组中,并尝试测试图像是否已正确加载。如果你看到下面的代码,我不得不一次又一次地重复几行。我怎样才能把它写成通用的Javascript 如何在casperjs中将参数传递给函数?,javascript,casperjs,Javascript,Casperjs,我已将所有图像url存储在一个数组中,并尝试测试图像是否已正确加载。如果你看到下面的代码,我不得不一次又一次地重复几行。我怎样才能把它写成通用的 casper.start() var imagesArray = []; imagesArray = ['https://www.google.co.in/images/srpr/logo11w.png', 'https://www.google.co.in/images/srpr/logo1w.png']; casper.thenOpen(ima
casper.start()
var imagesArray = [];
imagesArray = ['https://www.google.co.in/images/srpr/logo11w.png',
'https://www.google.co.in/images/srpr/logo1w.png'];
casper.thenOpen(imagesArray[0], function () {
if (this.currentHTTPStatus === 404) {
this.warn(imagesArray[0] + ' is missing (HTTP 404)');
} else if (this.currentHTTPStatus === 500) {
this.warn(imagesArray[0] + ' is broken (HTTP 500)');
} else {
this.echo(' is okay (HTTP %s)');
}
});
casper.thenOpen(imagesArray[1], function () {
if (this.currentHTTPStatus === 404) {
this.warn(imagesArray[0] + ' is missing (HTTP 404)');
} else if (this.currentHTTPStatus === 500) {
this.warn(imagesArray[0] + ' is broken (HTTP 500)');
} else {
this.echo(' is okay (HTTP %s)');
}
});
casper.run(function() {
this.echo('Image loading test finished');
this.exit();
});
我尝试了下面的方法,调用了一个函数,但它抛出了解析器错误,我做错了什么,或者如何继续
function checkImages(item){
if (this.currentHTTPStatus === 404) {
this.warn(item + ' is missing (HTTP 404)');
} else if (this.currentHTTPStatus === 500) {
this.warn(item + ' is broken (HTTP 500)');
} else {
this.echo(' is okay (HTTP %s)');
}
}
casper.thenOpen(imagesArray[0], function () {
this.evaluate(checkImages(imagesArray[0]));
});
casper.thenOpen(imagesArray[1], function () {
this.evaluate(checkImages(imagesArray[1]));
});
提前感谢。作为提醒,请将evaluate()方法视为CasperJS环境和您打开的页面之间的一道大门;每次向evaluate()传递闭包时,都会进入页面并执行代码,就像使用浏览器控制台一样。因此,您不能在
checkImages
上使用evaluate
像这样使用echo
:
casper.thenOpen(imagesArray[0], function () {
this.echo(checkImages(imagesArray[0]));
});
casper.thenOpen(imagesArray[1], function () {
this.echo(checkImages(imagesArray[1]));
});
在这个测试用例中,您不需要
然后打开,因为您只想验证响应代码。你可以这样做,但这是难以置信的浪费时间/资源。这就是我实现相同目标的方式:
casper.test.begin('link tester', 73, function(test) {
casper.start(url);
getLinks = function(){
links = this.evaluate(function(){
var links = document.getElementsByTagName('a');
links = Array.prototype.map.call(links,function(link){
return link.getAttribute('href');
});
return links;
});
}
casper.then(getLinks);
casper.then(function(response) {
for (q = 0; q < links.length; q++) {
if (response == undefined || response.status >= 400) {
this.test.fail("URL " + links[q] + " failed with response code " + (response.status));
} else {
this.test.pass((response.status + " ---- " + links[q]));
}
}
});
由于所有then*
函数都是将步骤插入队列的异步步骤函数,因此可以在循环中调用它们。由于imagesArray
是本机数组,因此可以使用PhantomJS支持的array.prototype.forEach
对其进行迭代:
var imagesArray = [
'https://www.google.co.in/images/srpr/logo11w.png',
'https://www.google.co.in/images/srpr/logo1w.png'
];
casper.start();
imagesArray.forEach(function(imageUrl){
casper.thenOpen(imageUrl, function () {
if (this.currentHTTPStatus === 404) {
this.warn(imageUrl + ' is missing (HTTP 404)');
} else if (this.currentHTTPStatus === 500) {
this.warn(imageUrl + ' is broken (HTTP 500)');
} else {
this.echo(' is okay (HTTP %s)');
}
});
});
casper.run();
一个简单的for循环就足够了,但随后您会遇到imagesArray[i]
内部和open
的问题。i
变量永远不会更改,因为每个步骤都是在循环完成执行后执行的。因此,每个imagesArray[i]
都会显示最后一个url。因为JavaScript具有函数级作用域,url绑定到每个迭代,并且在迭代之后从不更改。PhantomJS支持Array.prototype.forEach
,但它可以通过在for循环体内部使用IIFE来解决。我想这看起来更好。我不明白你为什么认为这能解决问题。你能详细说明一下吗?也许你忘了什么。首先从页面中检索所有链接。然后迭代所有链接,但仍然在同一页面上,并且没有打开要检查的页面。因此,如果起始页未能加载每个链接,则应该失败,但由于起始页失败,因此没有要检查的链接,并且您将获得73个可疑的测试用例。如果我在casper中获取图像,这会发生什么变化。然后
,在这种情况下,casper.thenOpen似乎总是与最后一个元素一起调用?@srrvnn不会。这段代码和您尝试的代码之间可能有一点不同。如果调试后仍有问题,可以提出新问题。请注意我答案的最后一段(您是否使用for循环?),您是对的。读了你的最后一段后,我尝试了forEach,效果很好。非常感谢。
var imagesArray = [
'https://www.google.co.in/images/srpr/logo11w.png',
'https://www.google.co.in/images/srpr/logo1w.png'
];
casper.start();
imagesArray.forEach(function(imageUrl){
casper.thenOpen(imageUrl, function () {
if (this.currentHTTPStatus === 404) {
this.warn(imageUrl + ' is missing (HTTP 404)');
} else if (this.currentHTTPStatus === 500) {
this.warn(imageUrl + ' is broken (HTTP 500)');
} else {
this.echo(' is okay (HTTP %s)');
}
});
});
casper.run();