Capture image using specific element locator using protractor tool
Here is an example to crop the logo from a screenshot:
const fs = require('fs');const PNG = require('pngjs').PNG;it("should crop logo", function() { browser.get("http://stackoverflow.com/"); var element = $("#hlogo"); protractor.promise.all([ element.getLocation(), element.getSize(), browser.takeScreenshot() ]).then(function(result) { var src = PNG.sync.read(Buffer.from(result[2], 'base64')); var dst = new PNG({width: result[1].width, height: result[1].height}); PNG.bitblt(src, dst, result[0].x, result[0].y, dst.width, dst.height, 0, 0); fs.writeFileSync('out.png', PNG.sync.write(dst)); });});