I am very new to Protractor and Javascript / Node.js. I had a requirement when I need to take a screenshot of a certain element and show the same in the jasmine report (note that the screenshot contained in the report should not contain the entire web page, it should only contain the web element binding trying to find on the page.)
Here is an example of the code I found in a stack overflow. but I could not accept the same, because it takes a screenshot of the entire page.
testpage.takesnap = function (elementLocator, screenshotName) {
var test1 = element(by.xpath(elementLocator)).getSize().then(function(size){
element(by.xpath(elementLocator)).getLocation().then(function(location) {
browser.takeScreenshot().then(function(data) {
var base64Data = data.replace(/^data:image\/png;base64,/, "");
fs.writeFile(screenshotFilePath+screenshotName, base64Data, 'base64', function(err) {
if (err) {
console.log(err);
}
else {
test.cropInFile(size, location, screenshotFilePath+screenshotName);
}
doneCallback();
});
});
});
});
console.log('Completed taking screenshot');
};
testpage.cropInFile = function(size, location, filePath) {
easyimg.crop({
src: filePath,
dst: filePath,
cropwidth: size.width,
cropheight: size.height,
x: location.x,
y: location.y,
gravity: 'North-West'
},
function(err, stdout, stderr) {
if (err) throw err;
});
};
I didn’t get any errors, but it still requires a complete link to the entire web page.
.
java . , .
, .
Advance.