PhantomCSS屏幕截图在循环内不能正常工作
问题描述:
我想捕获每个页面上的屏幕截图。为了导航到不同的页面,有一个功能moveNext()
。当我检查控制台时,我能够看到它按顺序导航到所有页面。但是,它并不是在每个页面上同时进行屏幕截图,而是需要最后一页的多个屏幕截图。 casperjs是否提供回叫或等待选项?PhantomCSS屏幕截图在循环内不能正常工作
casper.then(function() {
for (var currentPage = startPage; currentPage < lastPage; currentPage++) {
this.page.evaluate(function(currentPage) {
moveNext(currentPage);
}, currentPage);
phantomcss.screenshot('html', 'screenshot');
console.log(currentPage);
}
});
答
function captureScreenshot(width, height, device, startPage, lastPage) {
casper.viewport(width, height);
casper.then(function() {
var currentPage = startPage - 1;
/* Capture screenshot of footer */
phantomcss.screenshot('footer', 'footer');
/* Capture screenshot of header */
phantomcss.screenshot('header', 'header');
/* Capture main content screenshot */
casper.repeat(lastPage, function() {
this.page.evaluate(function (currentPage) {
moveNext(currentPage);
}, ++currentPage);
this.wait(8000, function() {
phantomcss.screenshot('#NavForm > .container', 'main-content');
});
});
});
}
答
for
循环将无法工作,因为它太快。你可以使用类似:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
viewportSize:{width: 1600, height: 900}
});
var currentPage = 0,lastPage=5;
casper
.start()
.then(function to_call() {
if(currentPage<lastPage){
currentPage++;
/* this.page.evaluate(function(currentPage) {
moveNext(currentPage);
}, currentPage);
phantomcss.screenshot('html', 'screenshot');*/
this.echo("The currentPage number is: "+currentPage,"INFO");
this.wait(3000).then(to_call)
}
})
.run();