Skip to content

Instantly share code, notes, and snippets.

@henrahmagix
Last active October 30, 2017 17:38
Show Gist options
  • Save henrahmagix/83b71c9e01118537588e46c4b760cff2 to your computer and use it in GitHub Desktop.
Save henrahmagix/83b71c9e01118537588e46c4b760cff2 to your computer and use it in GitHub Desktop.
Take a screenshot of an element in all browsers by cropping a page screenshot
// Altered from http://stackoverflow.com/a/38311061/3150057
const fs = require('fs');
const sharp = require('sharp');
const takeElementScreenshot = function (element, name) {
return protractor.promise
.all([
element.getLocation(),
element.getSize(),
browser.takeScreenshot()
])
.then(([coords, size, pngData]) => {
return sharp(new Buffer(pngData, 'base64'))
.extract({
left: coords.x,
top: coords.y,
width: size.width,
height: size.height
})
.toBuffer()
.then((croppedPng) => fs.writeFileSync(`${name}.png`, croppedPng);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment