问题
I am new to the protractor and would like to take screenshots of my failed test cases in browsers.
Can you please help me out by advising how should I go about it?
Thank you :)
回答1:
You can use protractor-jasmine2-screenshot-reporter
module for this, it has some good features which would serve your purpose.
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
dest: 'target/screenshots',
filename: 'my-report.html',
captureOnlyFailedSpecs: true
});
This will capture screenshots when your specs have failed, you have many more options, you can checkout this link : https://www.npmjs.com/package/protractor-jasmine2-screenshot-reporter
回答2:
Please have a look on this chunk of code. In this code we are registering screenshot function properly with jasmine reporter. Its works for me.
onPrepare: function() {
jasmine.getEnv().addReporter({
specDone: function(result) {
browser.takeScreenshot().then(function(screenShot) {
// Saving File.
// Param filePath : where you want to store screenShot
// Param screenShot : Screen shot file which you want to store.
fs.writeFile(filePath, screenShot, 'base64', function (err) {
if (err) throw err;
console.log('File saved.');
});
});
}
});
}
I hope it helps! :)
Reference link
回答3:
protractor-beautiful-reporter is capable of building nice html reports including the screenshots.
Installation:
npm install protractor-beautiful-reporter --save-dev
Configuration in protractor.conf.js
:
const HtmlReporter = require('protractor-beautiful-reporter');
exports.config = {
// your config here ...
onPrepare: function() {
// Add a screenshot reporter and store screenshots to `/tmp/screenshots`:
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: 'target/screenshots',
takeScreenShotsOnlyForFailedSpecs: true
}).getJasmine2Reporter());
}
}
来源:https://stackoverflow.com/questions/38321028/how-to-take-screenshot-in-protractor-on-failure-of-test-cases