-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
332 lines (204 loc) · 7.88 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict';
var $q = require('q');
var $image = require('./lib/image');
var $source = require('./lib/source');
var $config = require('./lib/config');
var $utils = require('./lib/utils');
var $reporter = require('./lib/jasmineReporter');
var $log = require('./lib/log');
module.exports = new ProtractorSnapshot();
module.exports.saveImage = $image.save;
module.exports.saveSource = $source.save;
module.exports.defaultConfig = $config.defaultConfig;
module.exports.clearTarget = $utils.clearTarget;
function ProtractorSnapshot () {
var initialized = false;
var configured = false;
var self = this;
self.report = {};
self.jasmineVersion = 1;
self.setConfig = setConfig;
self.cycle = cycle;
self.image = image;
self.source = source;
self.utils = $utils;
self.getSuiteName = getSuiteName;
self.getSuiteId = getSuiteId;
self.getSpecName = getSpecName;
self.getSpecId = getSpecId;
self.getResolution = getResolution;
self.addReporter = addReporter;
self.log = log;
self.resizeBrowser = resizeBrowser;
self.config = undefined;
self.state = {};
function init () {
if (initialized === false && typeof browser !== 'undefined') {
initialized = true;
if (configured === false) {
self.setConfig();
}
onInit();
}
}
function setConfig (config) {
configured = true;
config = config || browser.getProcessedConfig().value_.protractorSnapshotOpts;
self.config = $config.extract(config);
}
function onInit () {
if (typeof self.config.onInit !== 'undefined') {
if (typeof self.config.onInit === 'function') {
self.config.onInit(self);
}
else if (Array.isArray(self.config.onInit)) {
self.config.onInit.forEach(function (onInitFn) {
if (typeof onInitFn !== 'function') {
throw 'config.onInit should be a function or array of functions';
}
onInitFn(self);
});
}
}
}
function cycle (resolutions, callback) {
init();
var deferreds = [];
// cycle over provided or configured resolutions
if (typeof resolutions === 'function') {
callback = resolutions;
resolutions = self.config.resolutions;
}
resolutions.forEach(function (resolution) {
deferreds.push(resizeBrowser(resolution[0], resolution[1], resolution[2])
.then(function () {
self.state.resolution = [resolution[0], resolution[1]];
// perform callback and provide current resolution as argument
return callback(resolution);
}));
});
return browser.wait($q.allSettled(deferreds)
.then(function (promises) {
// reset window size to default
return resizeBrowser(self.config.defaultResolution[0], self.config.defaultResolution[1], self.config.defaultResolution[2])
.then(function () {
self.state.resolution = [self.config.defaultResolution[0], self.config.defaultResolution[1]];
return promises;
});
}));
}
function image (customConfig) {
init();
return browser.wait($image(self, self.config.image.callbacks, customConfig));
}
function source (customConfig) {
init();
return browser.wait($source(self, self.config.source.callbacks, customConfig));
}
function getResolution () {
return self.state.resolution[0] + 'x' + self.state.resolution[1];
}
function getSuiteName () {
var suite = _getSpec().suite || {};
return suite.description || 'unknown-suite-name';
}
function getSuiteId () {
var suite = _getSpec().suite || {};
var suiteId = 'unknown-suite-id';
if (typeof suite.id !== 'undefined') {
suiteId = suite.id;
// jasmine v1 has an int as suiteId, v2 has a string
if (self.jasmineVersion === 1) {
// only jasmine v1 has a 0-based suiteId
suiteId++;
}
else {
suiteId = suiteId.replace(/[^\d]+/g, '');
suiteId = parseInt(suiteId, 10);
}
}
return suiteId;
}
function getSpecName () {
return _getSpec().description || 'unknown-spec-name';
}
function getSpecId () {
var specId = 'unknown-spec-id';
if (typeof _getSpec().id !== 'undefined') {
specId = _getSpec().id;
if (self.jasmineVersion > 1) {
specId = specId.replace(/[^\d]+/g, '');
specId = parseInt(specId, 10);
}
// for some reason both jasmine v1 and v2 have a 0-based specId. Consistent much?
specId++;
}
return specId;
}
function addReporter () {
// property is not present in jasmine v1 so use it as indication of v2+
if (typeof jasmine.version !== 'undefined') {
self.jasmineVersion = jasmine.version.split('.').shift();
jasmine.getEnv().addReporter(new $reporter(self));
}
}
function log (filename) {
$log.log(self, filename);
}
function _getSpec () {
return jasmine.getEnv().currentSpec || {};
}
function resizeBrowser (width, height, type) {
var promise;
if (typeof height === 'string' && height.toLowerCase() === 'full') {
type = 'viewport';
// first resize the browser to the current height with the new width, to prevent responsiveness errors
promise = browser.driver.manage().window().getSize()
.then(function (value) {
return browser.driver.manage().window().setSize(width, value.height);
});
promise = promise.then(function () {
return browser.driver.executeScript(function () {
var body = document.body;
var html = document.documentElement;
return Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
}).then(function (viewPortHeight) {
height = viewPortHeight;
return browser.driver.manage().window().setSize(width, viewPortHeight);
});
});
}
else {
promise = browser.manage().window().setSize(width, height);
}
if (type === 'viewport') {
// calculate offset
promise = promise.then(function () {
return browser.driver.executeScript(function () {
return {
height: Math.max(
document.documentElement.clientHeight,
window.innerHeight || 0
),
width: Math.max(
document.documentElement.clientWidth,
window.innerWidth || 0
)
};
}).then(function (value) {
// fix the values, but make sure we're not ending up with smaller values
width = Math.max(value.width, width - value.width + width, width);
height = Math.max(value.height, height - value.height + height, height);
return resizeBrowser(width, height, 'window');
});
});
}
return promise;
}
}