Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save multiple captures from same site with one casper.open #64

Open
giko45 opened this issue Apr 19, 2018 · 0 comments
Open

save multiple captures from same site with one casper.open #64

giko45 opened this issue Apr 19, 2018 · 0 comments

Comments

@giko45
Copy link

giko45 commented Apr 19, 2018

I needed to save multiple captures from same site with one casper.open.

i solved it by modifying webshot.js as below incase anyone is intrested

`// This must be executed with phantomjs
// Take a screenshot of a URL and saves it to a .png file
// phantomjs webshot.js
//
// 'optsList' is a JSON array containing configurations for each screenshot that has
// to be taken. Each configuration object needs to contain at least properties
// "url" and "file". For instance:
// [{"url":"http://rstudio.github.io/leaflet/","file":"webshot.png"}]

var utils = require('./utils');
var system = require('system');

phantom.casperPath = phantom.libraryPath + '/casperjs';
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');

var opt_defaults = {
delay: 0.2,
vwidth: 992,
vheight: 744,
zoom: 1
};

// =====================================================================
// Command line arguments
// =====================================================================
var args = system.args;

if (args.length < 2) {
console.log(
'Usage:\n' +
' phantomjs webshot.js \n' +
'\n' +
'optsList is a JSON array containing configuration for each screenshot.\n' +
'For instance:\n' +
''[{"url":"url1.html","file":"file1.png"},{"url":"url2.html","file":"fil2.png","zoom":2}]'');
phantom.exit(1);
}

var optsList = JSON.parse(args[1]);

// Options passed to CasperJS
var casperOpts = {};
if (optsList[0].options) {
casperOpts = JSON.parse(optsList[0].options);
}

// debug is a special option. The value from the first element in the
// optsList array is applied globally.
if (optsList[0].debug) {
casperOpts.verbose = true;
casperOpts.logLevel = 'debug';
}
delete optsList.debug;

var casper = require('casper').create(casperOpts);

// =====================================================================
// Screenshot
// =====================================================================

casper.start();
casper.options.onLoadError = function(c, url) {
console.log("Could not load ", url);
phantom.exit(1);
};

//////// ADDED FOR ONLY REQUESTING FIRST URL, REST IS IGNORED //////////////////
casper.then(function() {
var opts = optsList[1];

// Prepare options
opts = utils.fillMissing(opts, opt_defaults);

// Go to url and perform the desired screenshot
this.zoom(opts.zoom)
.viewport(opts.zoom * opts.vwidth, opts.zoom * opts.vheight)
.thenOpen(opts.url)
.wait(opts.delay * 1000)
});
//////// ADDED FOR ONLY REQUESTING FIRST URL, REST IS IGNORED //////////////////

casper.eachThen(optsList, function(response) {
var opts = response.data;

// Prepare options
opts = utils.fillMissing(opts, opt_defaults);

// This should be four numbers separated by ","
if (opts.cliprect) {
opts.cliprect = opts.cliprect.split(",");
opts.cliprect = opts.cliprect.map(function(x) { return +x; });
}

// Can be 1 or 4 numbers separated by ","
if (opts.expand) {
opts.expand = opts.expand.split(",");
opts.expand = opts.expand.map(function(x) { return +x; });
if (opts.expand.length !== 1 && opts.expand.length !== 4) {
console.log("'expand' must have either 1 or 4 values.");
phantom.exit(1);
}
}

// Can have multiple selectors
if (opts.selector) {
opts.selector = opts.selector.split(",");
}

//////// FIRST THREE CALLS ARE ALREADY DONE, ONLY SAVE selector //////////////////
// Go to url and perform the desired screenshot
// this.zoom(opts.zoom)
// .viewport(opts.zoom * opts.vwidth, opts.zoom * opts.vheight)
// .thenOpen(opts.url)
// .wait(opts.delay * 1000)
this.then(function() {
if (opts.eval) {
eval(opts.eval);
}
})
.then(function() {
var cr = findClipRect(opts, this);
this.capture(opts.file, cr);
});
});

casper.run();

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant