Last active
October 30, 2017 17:38
-
-
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
This file contains hidden or 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
// 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