Created
December 30, 2014 00:44
-
-
Save adamloving/df2a785364e3c4a48837 to your computer and use it in GitHub Desktop.
An example of how to take a screenshot using phantom js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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