Skip to content

Instantly share code, notes, and snippets.

@mjul
Last active January 10, 2018 08:23
Show Gist options
  • Save mjul/ecdd196bf03c2c4dd09ade14d596361c to your computer and use it in GitHub Desktop.
Save mjul/ecdd196bf03c2c4dd09ade14d596361c to your computer and use it in GitHub Desktop.
Take screenshot with Selenium JavaScript driver
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until,
logging = webdriver.logging;
const fs = require('fs');
const path = require('path');
// Take a screenshot.
// state contains the Selenium driver instance and the config with the default path.
// name is the filename (relative)
function takeScreenshot(state, name) {
return state
.driver.takeScreenshot()
.then(base64PngImage => {
return state.driver.getCurrentUrl()
.then(u => {
let fname = path.join(state.config.screenshotPath, name);
console.info("Writing screenshot: " + fname);
fs.writeFileSync(fname, base64PngImage, 'base64');
});
})
.then(_ => state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment