-
Notifications
You must be signed in to change notification settings - Fork 611
Expand file tree
/
Copy pathcreateBitmaps.js
More file actions
203 lines (170 loc) · 6.38 KB
/
createBitmaps.js
File metadata and controls
203 lines (170 loc) · 6.38 KB
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
var cloneDeep = require('lodash/cloneDeep');
var fs = require('./fs');
var each = require('./each');
var pMap = require('p-map');
var runPuppet = require('./runPuppet');
const ensureDirectoryPath = require('./ensureDirectoryPath');
var logger = require('./logger')('createBitmaps');
var CONCURRENCY_DEFAULT = 10;
function regexTest (string, search) {
var re = new RegExp(search);
return re.test(string);
}
function ensureViewportLabel (config) {
if (typeof config.viewports === 'object') {
config.viewports.forEach(function (viewport) {
if (!viewport.label) {
viewport.label = viewport.name;
}
});
}
}
function decorateConfigForCapture (config, isReference) {
var configJSON;
if (typeof config.args.config === 'object') {
configJSON = config.args.config;
} else {
configJSON = Object.assign({}, require(config.backstopConfigFileName));
}
configJSON.scenarios = configJSON.scenarios || [];
ensureViewportLabel(configJSON);
var totalScenarioCount = configJSON.scenarios.length;
function pad (number) {
var r = String(number);
if (r.length === 1) {
r = '0' + r;
}
return r;
}
var screenshotNow = new Date();
var screenshotDateTime = screenshotNow.getFullYear() + pad(screenshotNow.getMonth() + 1) + pad(screenshotNow.getDate()) + '-' + pad(screenshotNow.getHours()) + pad(screenshotNow.getMinutes()) + pad(screenshotNow.getSeconds());
screenshotDateTime = configJSON.dynamicTestId ? configJSON.dynamicTestId : screenshotDateTime;
configJSON.screenshotDateTime = screenshotDateTime;
config.screenshotDateTime = screenshotDateTime;
if (configJSON.dynamicTestId) {
console.log(`dynamicTestId '${configJSON.dynamicTestId}' found. BackstopJS will run in dynamic-test mode.`);
}
configJSON.env = cloneDeep(config);
configJSON.isReference = isReference;
configJSON.paths.tempCompareConfigFileName = config.tempCompareConfigFileName;
configJSON.defaultMisMatchThreshold = config.defaultMisMatchThreshold;
configJSON.backstopConfigFileName = config.backstopConfigFileName;
configJSON.defaultRequireSameDimensions = config.defaultRequireSameDimensions;
if (config.args.filter) {
var scenarios = [];
config.args.filter.split(',').forEach(function (filteredTest) {
each(configJSON.scenarios, function (scenario) {
if (regexTest(scenario.label, filteredTest)) {
scenarios.push(scenario);
}
});
});
configJSON.scenarios = scenarios;
}
logger.log('Selected ' + configJSON.scenarios.length + ' of ' + totalScenarioCount + ' scenarios.');
return configJSON;
}
function saveViewportIndexes (viewport, index) {
return Object.assign({}, viewport, { vIndex: index });
}
function delegateScenarios (config) {
var scenarios = [];
var scenarioViews = [];
config.viewports = config.viewports.map(saveViewportIndexes);
// casper.each(scenarios, function (casper, scenario, i) {
config.scenarios.forEach(function (scenario, i) {
// var scenarioLabelSafe = makeSafe(scenario.label);
scenario.sIndex = i;
scenario.selectors = scenario.selectors || [];
if (scenario.viewports) {
scenario.viewports = scenario.viewports.map(saveViewportIndexes);
}
scenarios.push(scenario);
if (!config.isReference && scenario.hasOwnProperty('variants')) {
scenario.variants.forEach(function (variant) {
// var variantLabelSafe = makeSafe(variant.label);
variant._parent = scenario;
scenarios.push(scenario);
});
}
});
var scenarioViewId = 0;
scenarios.forEach(function (scenario) {
var desiredViewportsForScenario = config.viewports;
if (scenario.viewports && scenario.viewports.length > 0) {
desiredViewportsForScenario = scenario.viewports;
}
desiredViewportsForScenario.forEach(function (viewport) {
scenarioViews.push({
scenario: scenario,
viewport: viewport,
config: config,
id: scenarioViewId++
});
});
});
const asyncCaptureLimit = config.asyncCaptureLimit === 0 ? 1 : config.asyncCaptureLimit || CONCURRENCY_DEFAULT;
if (config.engine.startsWith('puppet')) {
return pMap(scenarioViews, runPuppet, { concurrency: asyncCaptureLimit });
} else if (/chrom./i.test(config.engine)) {
logger.error(`Chromy is no longer supported in version 5+. Please use version 4.x.x for chromy support.`);
} else {
logger.error(`Engine "${(typeof config.engine === 'string' && config.engine) || 'undefined'}" not recognized! If you require PhantomJS or Slimer support please use backstopjs@3.8.8 or earlier.`);
}
}
function writeCompareConfigFile (comparePairsFileName, compareConfig) {
var compareConfigJSON = JSON.stringify(compareConfig, null, 2);
ensureDirectoryPath(comparePairsFileName);
return fs.writeFile(comparePairsFileName, compareConfigJSON);
}
function flatMapFailuresUrls (rawTestPairs) {
return rawTestPairs.reduce((acc, result) => {
var failureUrls = result.failureUrls;
return acc.concat(failureUrls);
}, []);
}
function flatMapTestPairs (rawTestPairs) {
return rawTestPairs.reduce((acc, result) => {
var testPairs = result.testPairs;
if (!testPairs) {
testPairs = {
diff: {
isSameDimensions: '',
dimensionDifference: {
width: '',
height: ''
},
misMatchPercentage: ''
},
reference: '',
test: '',
selector: '',
fileName: '',
label: '',
scenario: result.scenario,
viewport: result.viewport,
msg: result.msg,
error: result.originalError && result.originalError.name
};
}
return acc.concat(testPairs);
}, []);
}
module.exports = function (config, isReference) {
const promise = delegateScenarios(decorateConfigForCapture(config, isReference))
.then(rawTestPairs => {
const result = {
compareConfig: {
testPairs: flatMapTestPairs(rawTestPairs)
},
};
const failuresUrls = flatMapFailuresUrls(rawTestPairs);
if (failuresUrls.length > 0) {
var failedUrlsPath = config.bitmaps_reference + '/' + (isReference ? 'reference-' : 'test-') + config.reloadOnError.outputFile;
ensureDirectoryPath(failedUrlsPath);
fs.writeFile(failedUrlsPath, JSON.stringify(failuresUrls, null, '\t'))
}
return writeCompareConfigFile(config.tempCompareConfigFileName, result);
});
return promise;
};