Tuesday, July 4, 2017

Not able to take screenshot for a specific element in protractor

Leave a Comment

I want to take the snapshot of an element using protractor and protractor supports the element.takeScreeshot(). However when i am using it throws the Some session error(below)

 element(by.model('model.username')).takeScreenshot().then(ab=>{  )} 

Error

**- Failed: GET /session/5d58e1ca-f55d-4b51-aee8-1d518498cb35/element/0/screenshot   Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'   System info: host: 'INBEN10174', ip: '157.237.220.180', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'   Driver info: driver.version: unknown** 

2 Answers

Answers 1

You can screenshot the entire page and then crop the image to the element you want:

const fs = require('fs'); const PNG = require('pngjs').PNG;  var elem = element(by.model('model.username'));  promise.all([     elem.getLocation(),     elem.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)); }); 

This will output a .png image of the selected element. As mentioned below, you will need to make sure the element is on the screen prior to this; which is achievable like so:

var elem = element(by.model('model.username')); browser.actions().mouseMove(elem).perform(); 

Answers 2

As said by @suresh-salloju, it's a new feature and even on my chromedriver 2.30 and selenium 3.4.0 is throws the same answer.

If you want to be able to take a screenshot of an element you can maybe use protractor-image-comparison. The methods saveElement or checkElement can help with testing. Only be sure that you scroll the element in the viewport.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment