Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created December 30, 2014 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/df2a785364e3c4a48837 to your computer and use it in GitHub Desktop.
Save adamloving/df2a785364e3c4a48837 to your computer and use it in GitHub Desktop.
An example of how to take a screenshot using phantom js.
#!/usr/bin/env node
'use strict';
var when = require('when');
var phantom = require('phantom');
exports.takeShot = function takeShot(url, outputFilePath) {
return when.promise(function(resolve, reject) {
console.log('Spawning browser for', url);
phantom.create(
'--debug=true',
'--ssl-protocol=any',
'--ignore-ssl-errors=true',
function(browser) {
console.log('Creating page for for', url);
browser.createPage(function(page) {
console.log('Opening', url);
page.open(url, function(status) {
console.log('Status: ' + status);
if (status !== 'success') {
browser.exit(1);
reject(new Error('Could not open page: ' + url));
} else {
setTimeout(function () {
console.log('Rendering', outputFilePath);
page.render(outputFilePath);
browser.exit(0);
resolve(true);
}, 2000);
}
});
}); // createPage
}
); // create
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment