Skip to content

Instantly share code, notes, and snippets.

@jlouros
Created March 22, 2017 10:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jlouros/190d654850f8e2ed7b51ed6267f30400 to your computer and use it in GitHub Desktop.
Save jlouros/190d654850f8e2ed7b51ed6267f30400 to your computer and use it in GitHub Desktop.
Protractor configuration to take screenshots on test failures (using Jasmine framework)
/* eslint import/no-extraneous-dependencies: ["off"] */
/* eslint func-names: ["off"] */
/* global browser */
/**
* Jasmine reporter used to take screenshots every time a test fails
* on your 'protractor.conf.js' (Protractor configuration file)
* include a reference to this file `const ScreenshotReporter = require('./screenshotReporter.js');`
* and hook it up inside 'onPrepare()' `jasmine.getEnv().addReporter(new ScreenshotReporter('reports/e2e-failures'));`
*/
const mkdirp = require('mkdirp');
const fs = require('fs');
const path = require('path');
const ScreenshotReporter = function (dir_) {
const dir = (dir_ || '/tmp/protractorss/');
// base function to take a screenshot -- change path as needed
const screenshot = (testDescription) => {
// set file name
const fname = `${testDescription.replace(/\s/g, '_')}.png`;
// make sure the directory exists
mkdirp(dir);
// take screenshot
browser.takeScreenshot().then((png) => {
// save the taken screenshot
const stream = fs.createWriteStream(path.join(dir, fname));
stream.write(new Buffer(png, 'base64'));
stream.end();
});
};
this.specDone = (result) => {
// for each test failure
if (result.failedExpectations && result.failedExpectations.length > 0) {
screenshot(result.fullName, 'end');
}
};
};
module.exports = ScreenshotReporter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment