Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created December 30, 2014 00:48
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/8ce8ca2fbd2899df0450 to your computer and use it in GitHub Desktop.
Save adamloving/8ce8ca2fbd2899df0450 to your computer and use it in GitHub Desktop.
example of taking a screenshot of a web page using selenium-webdriver
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var when = require('when');
var webdriver = require('selenium-webdriver');
exports.takeShot = function(url, outputFilePath) {
return when.promise(function(resolve, reject) {
var driver = new webdriver.Builder()
.withCapabilities({'browserName': 'chrome'}).build();
new webdriver.WebDriver.Window(driver).setSize(1024, 865);
driver.get(url);
driver.wait(function() {
if (driver.isElementPresent({id: 'url2png-cheese'})) {
return true;
} else {
reject(new Error('Cheese not found'));
return false;
}
}, 1000, 'Timed out waiting for page content to load.')
.then(function() {
setTimeout(function() {
driver.takeScreenshot()
.then(function(data) {
fs.writeFileSync(outputFilePath, data, 'base64');
driver.quit()
.then(function(){
resolve(true);
});
});
}, 5000); // ugh: still have to wait for background image ... why?
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment