-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshotReporter.js
40 lines (35 loc) · 1.22 KB
/
screenshotReporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var mkdirp = require('mkdirp');
var fs = require('fs');
var path = require('path');
var ScreenshotReporter = function(dir_) {
var dir = (dir_ ? dir_ : "/tmp/protractorss/");
dir = path.join(dir, new Date().toISOString());
var index = 0;
// base function to take a screenshot -- change path as needed
var screenshot = function(testDescription, id) {
var fname = testDescription.replace(/\s/g, "_") + "_" + id + ".png";
mkdirp(dir);
browser.takeScreenshot().then(function(png) {
var stream = fs.createWriteStream(path.join(dir, fname));
stream.write(new Buffer(png, 'base64'));
stream.end();
});
}
// takes screenshot on each failed expect
var originalAddMatcherResult = jasmine.Spec.prototype.addMatcherResult;
jasmine.Spec.prototype.addMatcherResult = function() {
++index;
if (!arguments[0].passed()) {
screenshot(this.description, index);
}
return originalAddMatcherResult.apply(this, arguments);
};
// takes screenshot on each failed spec (including timeout)
this.reportSpecResults = function(spec) {
if (!spec.results().passed()) {
screenshot(spec.description, "end");
}
index = 0;
};
};
module.exports = ScreenshotReporter;