This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
168 lines (148 loc) · 5.4 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var util = require('./lib/util')
, mkdirp = require('mkdirp')
, path = require('path');
/** Function: defaultPathBuilder
* This function builds paths for a screenshot file. It is appended to the
* constructors base directory and gets prependend with `.png` or `.json` when
* storing a screenshot or JSON meta data file.
*
* Parameters:
* (Object) spec - The spec currently reported
* (Array) descriptions - The specs and their parent suites descriptions
* (Object) result - The result object of the current test spec.
* (Object) capabilities - WebDrivers capabilities object containing
* in-depth information about the Selenium node
* which executed the test case.
*
* Returns:
* (String) containing the built path
*/
function defaultPathBuilder(spec, descriptions, results, capabilities) {
return util.generateGuid();
}
/** Function: defaultMetaDataBuilder
* Uses passed information to generate a meta data object which can be saved
* along with a screenshot.
* You do not have to add the screenshots file path since this will be appended
* automatially.
*
* Parameters:
* (Object) spec - The spec currently reported
* (Array) descriptions - The specs and their parent suites descriptions
* (Object) result - The result object of the current test spec.
* (Object) capabilities - WebDrivers capabilities object containing
* in-depth information about the Selenium node
* which executed the test case.
*
* Returns:
* (Object) containig meta data to store along with a screenshot
*/
function defaultMetaDataBuilder(spec, descriptions, results, capabilities) {
var metaData = {
description: descriptions.reverse().join(' ')
, passed: results.passed()
, os: capabilities.caps_.platform
, browser: {
name: capabilities.caps_.browserName
, version: capabilities.caps_.version
}
};
if(results.items_.length > 0) {
var result = results.items_[0];
metaData.message = result.message;
metaData.trace = result.trace.stack;
}
return metaData;
}
/** Class: ScreenshotReporter
* Creates a new screenshot reporter using the given `options` object.
*
* For more information, please look at the README.md file.
*
* Parameters:
* (Object) options - Object with options as described below.
*
* Possible options:
* (String) baseDirectory - The path to the directory where screenshots are
* stored. If not existing, it gets created.
* Mandatory.
* (Function) pathBuilder - A function which returns a path for a screenshot
* to be stored. Optional.
* (Function) metaDataBuilder - Function which returns an object literal
* containing meta data to store along with
* the screenshot. Optional.
* (Boolean) takeScreenShotsForSkippedSpecs - Do you want to capture a
* screenshot for a skipped spec?
* Optional (default: false).
*/
function ScreenshotReporter(options) {
options = options || {};
if(!options.baseDirectory || options.baseDirectory.length === 0) {
throw new Error('Please pass a valid base directory to store the ' +
'screenshots into.');
} else {
this.baseDirectory = options.baseDirectory;
}
this.pathBuilder = options.pathBuilder || defaultPathBuilder;
this.metaDataBuilder = options.metaDataBuilder || defaultMetaDataBuilder;
this.takeScreenShotsForSkippedSpecs =
options.takeScreenShotsForSkippedSpecs || false;
this.takeScreenShotsOnlyForFailedSpecs =
options.takeScreenShotsOnlyForFailedSpecs || false;
}
/** Function: reportSpecResults
* Called by Jasmine when reporteing results for a test spec. It triggers the
* whole screenshot capture process and stores any relevant information.
*
* Parameters:
* (Object) spec - The test spec to report.
*/
ScreenshotReporter.prototype.reportSpecResults =
function reportSpecResults(spec) {
/* global browser */
var self = this
, results = spec.results()
if(!self.takeScreenShotsForSkippedSpecs && results.skipped) {
return;
}
if(self.takeScreenShotsOnlyForFailedSpecs && results.passed()) {
return;
}
browser.takeScreenshot().then(function (png) {
browser.getCapabilities().then(function (capabilities) {
var descriptions = util.gatherDescriptions(
spec.suite
, [spec.description]
)
, baseName = self.pathBuilder(
spec
, descriptions
, results
, capabilities
)
, metaData = self.metaDataBuilder(
spec
, descriptions
, results
, capabilities
)
, screenShotFile = baseName + '.png'
, metaFile = baseName + '.json'
, screenShotPath = path.join(self.baseDirectory, screenShotFile)
, metaDataPath = path.join(self.baseDirectory, metaFile)
// pathBuilder can return a subfoldered path too. So extract the
// directory path without the baseName
, directory = path.dirname(screenShotPath);
metaData.screenShotFile = screenShotFile;
mkdirp(directory, function(err) {
if(err) {
throw new Error('Could not create directory ' + directory);
} else {
util.storeScreenShot(png, screenShotPath);
util.storeMetaData(metaData, metaDataPath);
}
});
});
});
};
module.exports = ScreenshotReporter;