-
Notifications
You must be signed in to change notification settings - Fork 601
/
runPuppet.js
481 lines (425 loc) · 15.9 KB
/
runPuppet.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
const puppeteer = require('puppeteer');
const mergeImg = require('merge-img');
const fs = require('./fs');
const path = require('path');
const chalk = require('chalk');
const ensureDirectoryPath = require('./ensureDirectoryPath');
const injectBackstopTools = require('../../capture/backstopTools.js');
const engineTools = require('./engineTools');
const MIN_CHROME_VERSION = 62;
const TEST_TIMEOUT = 60000;
const DEFAULT_FILENAME_TEMPLATE = '{configId}_{scenarioLabel}_{selectorIndex}_{selectorLabel}_{viewportIndex}_{viewportLabel}';
const DEFAULT_BITMAPS_TEST_DIR = 'bitmaps_test';
const DEFAULT_BITMAPS_REFERENCE_DIR = 'bitmaps_reference';
const SELECTOR_NOT_FOUND_PATH = '/capture/resources/notFound.png';
const HIDDEN_SELECTOR_PATH = '/capture/resources/notVisible.png';
const ERROR_SELECTOR_PATH = '/capture/resources/unexpectedErrorSm.png';
const BODY_SELECTOR = 'body';
const DOCUMENT_SELECTOR = 'document';
const NOCLIP_SELECTOR = 'body:noclip';
const VIEWPORT_SELECTOR = 'viewport';
module.exports = function (args) {
const scenario = args.scenario;
const viewport = args.viewport;
const config = args.config;
const scenarioLabelSafe = engineTools.makeSafe(scenario.label);
const variantOrScenarioLabelSafe = scenario._parent ? engineTools.makeSafe(scenario._parent.label) : scenarioLabelSafe;
config._bitmapsTestPath = config.paths.bitmaps_test || DEFAULT_BITMAPS_TEST_DIR;
config._bitmapsReferencePath = config.paths.bitmaps_reference || DEFAULT_BITMAPS_REFERENCE_DIR;
config._fileNameTemplate = config.fileNameTemplate || DEFAULT_FILENAME_TEMPLATE;
config._outputFileFormatSuffix = '.' + ((config.outputFormat && config.outputFormat.match(/jpg|jpeg/)) || 'png');
config._configId = config.id || engineTools.genHash(config.backstopConfigFileName);
return processScenarioView(scenario, variantOrScenarioLabelSafe, scenarioLabelSafe, viewport, config);
};
async function processScenarioView (scenario, variantOrScenarioLabelSafe, scenarioLabelSafe, viewport, config) {
if (!config.paths) {
config.paths = {};
}
if (typeof viewport.label !== 'string') {
viewport.label = viewport.name || '';
}
const engineScriptsPath = config.env.engine_scripts || config.env.engine_scripts_default;
const isReference = config.isReference;
const VP_W = viewport.width || viewport.viewport.width;
const VP_H = viewport.height || viewport.viewport.height;
const puppeteerArgs = Object.assign(
{},
{
ignoreHTTPSErrors: true,
headless: !config.debugWindow
},
config.engineOptions
);
const browser = await puppeteer.launch(puppeteerArgs);
const page = await browser.newPage();
await page.setViewport({ width: VP_W, height: VP_H });
page.setDefaultNavigationTimeout(engineTools.getEngineOption(config, 'waitTimeout', TEST_TIMEOUT));
if (isReference) {
console.log(chalk.blue('CREATING NEW REFERENCE FILE'));
}
// --- set up console output and ready event ---
const readyEvent = scenario.readyEvent || config.readyEvent;
let readyResolve, readyPromise;
if (readyEvent) {
readyPromise = new Promise(resolve => {
readyResolve = resolve;
});
}
page.on('console', msg => {
for (let i = 0; i < msg.args().length; ++i) {
const line = msg.args()[i];
console.log(`Browser Console Log ${i}: ${line}`);
if (readyEvent && new RegExp(readyEvent).test(line)) {
readyResolve();
}
}
});
let chromeVersion = await page.evaluate(_ => {
let v = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return v ? parseInt(v[2], 10) : 0;
});
if (chromeVersion < MIN_CHROME_VERSION) {
console.warn(`***WARNING! CHROME VERSION ${MIN_CHROME_VERSION} OR GREATER IS REQUIRED. PLEASE UPDATE YOUR CHROME APP!***`);
}
let result;
const puppetCommands = async () => {
// --- BEFORE SCRIPT ---
var onBeforeScript = scenario.onBeforeScript || config.onBeforeScript;
if (onBeforeScript) {
var beforeScriptPath = path.resolve(engineScriptsPath, onBeforeScript);
if (fs.existsSync(beforeScriptPath)) {
await require(beforeScriptPath)(page, scenario, viewport, isReference, browser, config);
} else {
console.warn('WARNING: script not found: ' + beforeScriptPath);
}
}
// --- OPEN URL ---
var url = scenario.url;
if (isReference && scenario.referenceUrl) {
url = scenario.referenceUrl;
}
await page.goto(translateUrl(url));
await injectBackstopTools(page);
// --- WAIT FOR READY EVENT ---
if (readyEvent) {
await page.evaluate(`window._readyEvent = '${readyEvent}'`);
await readyPromise;
await page.evaluate(_ => console.info('readyEvent ok'));
}
// --- WAIT FOR SELECTOR ---
if (scenario.readySelector) {
await page.waitForTimeout(scenario.readySelector);
}
//
// --- DELAY ---
if (scenario.delay > 0) {
await page.waitForTimeout(scenario.delay);
}
// --- REMOVE SELECTORS ---
if (scenario.hasOwnProperty('removeSelectors')) {
const removeSelectors = async () => {
return Promise.all(
scenario.removeSelectors.map(async (selector) => {
await page
.evaluate((sel) => {
document.querySelectorAll(sel).forEach(s => {
s.style.cssText = 'display: none !important;';
s.classList.add('__86d');
});
}, selector);
})
);
};
await removeSelectors();
}
// --- ON READY SCRIPT ---
var onReadyScript = scenario.onReadyScript || config.onReadyScript;
if (onReadyScript) {
var readyScriptPath = path.resolve(engineScriptsPath, onReadyScript);
if (fs.existsSync(readyScriptPath)) {
await require(readyScriptPath)(page, scenario, viewport, isReference, browser, config);
} else {
console.warn('WARNING: script not found: ' + readyScriptPath);
}
}
// reinstall tools in case onReadyScript has loaded a new URL.
await injectBackstopTools(page);
// --- HIDE SELECTORS ---
if (scenario.hasOwnProperty('hideSelectors')) {
const hideSelectors = async () => {
return Promise.all(
scenario.hideSelectors.map(async (selector) => {
await page
.evaluate((sel) => {
document.querySelectorAll(sel).forEach(s => {
s.style.visibility = 'hidden';
});
}, selector);
})
);
};
await hideSelectors();
}
// --- HANDLE NO-SELECTORS ---
if (!scenario.hasOwnProperty('selectors') || !scenario.selectors.length) {
scenario.selectors = [DOCUMENT_SELECTOR];
}
await page.evaluate(`window._selectorExpansion = '${scenario.selectorExpansion}'`);
await page.evaluate(`window._backstopSelectors = '${scenario.selectors}'`);
result = await page.evaluate(() => {
if (window._selectorExpansion.toString() === 'true') {
window._backstopSelectorsExp = window._backstopTools.expandSelectors(window._backstopSelectors);
} else {
window._backstopSelectorsExp = window._backstopSelectors;
}
if (!Array.isArray(window._backstopSelectorsExp)) {
window._backstopSelectorsExp = window._backstopSelectorsExp.split(',');
}
window._backstopSelectorsExpMap = window._backstopSelectorsExp.reduce((acc, selector) => {
acc[selector] = {
exists: window._backstopTools.exists(selector),
isVisible: window._backstopTools.isVisible(selector)
};
return acc;
}, {});
return {
backstopSelectorsExp: window._backstopSelectorsExp,
backstopSelectorsExpMap: window._backstopSelectorsExpMap
};
});
};
let error;
await puppetCommands().catch(e => {
console.log(chalk.red(`Puppeteer encountered an error while running scenario "${scenario.label}"`));
console.log(chalk.red(e));
error = e;
});
let compareConfig;
if (!error) {
try {
compareConfig = await delegateSelectors(
page,
browser,
scenario,
viewport,
variantOrScenarioLabelSafe,
scenarioLabelSafe,
config,
result.backstopSelectorsExp,
result.backstopSelectorsExpMap
);
} catch (e) {
error = e;
}
} else {
await browser.close();
}
if (error) {
const testPair = engineTools.generateTestPair(config, scenario, viewport, variantOrScenarioLabelSafe, scenarioLabelSafe, 0, `${scenario.selectors.join('__')}`);
const filePath = config.isReference ? testPair.reference : testPair.test;
testPair.engineErrorMsg = error.message;
compareConfig = {
testPairs: [ testPair ]
};
fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);
}
return Promise.resolve(compareConfig);
}
// TODO: Should be in engineTools
async function delegateSelectors (
page,
browser,
scenario,
viewport,
variantOrScenarioLabelSafe,
scenarioLabelSafe,
config,
selectors,
selectorMap
) {
let compareConfig = { testPairs: [] };
let captureDocument = false;
let captureViewport = false;
let captureList = [];
let captureJobs = [];
selectors.forEach(function (selector, selectorIndex) {
const testPair = engineTools.generateTestPair(config, scenario, viewport, variantOrScenarioLabelSafe, scenarioLabelSafe, selectorIndex, selector);
const filePath = config.isReference ? testPair.reference : testPair.test;
if (!config.isReference) {
compareConfig.testPairs.push(testPair);
}
selectorMap[selector].filePath = filePath;
if (selector === BODY_SELECTOR || selector === DOCUMENT_SELECTOR) {
captureDocument = selector;
} else if (selector === VIEWPORT_SELECTOR) {
captureViewport = selector;
} else {
captureList.push(selector);
}
});
if (captureDocument) {
captureJobs.push(function () { return captureScreenshot(page, browser, captureDocument, selectorMap, config, [], viewport); });
}
// TODO: push captureViewport into captureList (instead of calling captureScreenshot()) to improve perf.
if (captureViewport) {
captureJobs.push(function () { return captureScreenshot(page, browser, captureViewport, selectorMap, config, [], viewport); });
}
if (captureList.length) {
captureJobs.push(function () { return captureScreenshot(page, browser, null, selectorMap, config, captureList, viewport); });
}
return new Promise(function (resolve, reject) {
var job = null;
var errors = [];
var next = function () {
if (captureJobs.length === 0) {
if (errors.length === 0) {
resolve();
} else {
reject(errors);
}
return;
}
job = captureJobs.shift();
job().catch(function (e) {
console.log(e);
errors.push(e);
}).then(function () {
next();
});
};
next();
}).then(async () => {
console.log(chalk.green('x Close Browser'));
await browser.close();
}).catch(async (err) => {
console.log(chalk.red(err));
await browser.close();
}).then(_ => compareConfig);
}
async function captureScreenshot (page, browser, selector, selectorMap, config, selectors, viewport) {
let filePath;
let fullPage = (selector === NOCLIP_SELECTOR || selector === DOCUMENT_SELECTOR);
if (selector) {
filePath = selectorMap[selector].filePath;
ensureDirectoryPath(filePath);
/*
Setting config.mergeImgHack == true will use an alternate screen grab method which
takes multple screenshots at periodic scroll positions then stitches them all togther
into one single screenshot.
*/
if (fullPage && config.mergeImgHack) {
// Safer version of `document` selector
// see https://github.com/garris/BackstopJS/issues/820
try {
const screenHeight = typeof config.mergeImgHack === 'number' ? config.mergeImgHack : viewport.height;
const bodyHandle = await page.$('body');
const { width, height: totalHeight } = await bodyHandle.boundingBox();
const screens = [];
let cumHeight = 0;
for (let i = 0; i * screenHeight < totalHeight; i++) {
cumHeight += screenHeight;
console.log(`screenshot part ${i} (${Math.min(cumHeight, totalHeight)} / ${totalHeight})`);
await page.evaluate((i, screenHeight) => {
window.scrollTo(0, i * screenHeight);
}, i, screenHeight);
const screen = await page.screenshot({
path: totalHeight > screenHeight ? undefined : filePath, // if only 1 screen is needed with save immediately
fullPage: false,
clip: {
x: 0,
y: i * screenHeight,
width,
height:
cumHeight > totalHeight
? Math.ceil(screenHeight - (cumHeight - totalHeight))
: screenHeight
}
});
screens.push(screen);
}
// if there was only 1 screen we already saved the file during screenshot
if (screens.length > 1) {
const img = await mergeImg(screens, {
direction: true
});
// Note: mergeImg relies on an old version of Jimp without writeAsync support
// See https://github.com/preco21/merge-img/issues/6
await new Promise((resolve, reject) => {
img.write(filePath, err => {
if (err) return reject(err);
resolve();
});
});
}
await bodyHandle.dispose();
} catch (e) {
console.log(chalk.red(`Error capturing..`), e);
return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);
}
} else {
try {
await page.screenshot({
path: filePath,
fullPage: fullPage
});
} catch (e) {
console.log(chalk.red(`Error capturing..`), e);
return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);
}
}
} else {
// OTHER-SELECTOR screenshot
const selectorShot = async (s, path) => {
const el = await page.$(s);
if (el) {
const box = await el.boundingBox();
if (box) {
// Resize the viewport to screenshot elements outside of the viewport
if (config.useBoundingBoxViewportForSelectors !== false) {
const bodyHandle = await page.$('body');
const boundingBox = await bodyHandle.boundingBox();
await page.setViewport({
width: Math.max(viewport.width, Math.ceil(boundingBox.width)),
height: Math.max(viewport.height, Math.ceil(boundingBox.height))
});
}
var type = config.puppeteerOffscreenCaptureFix ? page : el;
var params = config.puppeteerOffscreenCaptureFix ? { path: path, clip: box } : { path: path };
await type.screenshot(params);
} else {
console.log(chalk.yellow(`Element not visible for capturing: ${s}`));
return fs.copy(config.env.backstop + HIDDEN_SELECTOR_PATH, path);
}
} else {
console.log(chalk.magenta(`Element not found for capturing: ${s}`));
return fs.copy(config.env.backstop + SELECTOR_NOT_FOUND_PATH, path);
}
};
const selectorsShot = async () => {
return Promise.all(
selectors.map(async selector => {
filePath = selectorMap[selector].filePath;
ensureDirectoryPath(filePath);
try {
await selectorShot(selector, filePath);
} catch (e) {
console.log(chalk.red(`Error capturing Element ${selector}`), e);
return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);
}
})
);
};
await selectorsShot();
}
}
// handle relative file name
function translateUrl (url) {
const RE = new RegExp('^[./]');
if (RE.test(url)) {
const fileUrl = 'file://' + path.join(process.cwd(), url);
console.log('Relative filename detected -- translating to ' + fileUrl);
return fileUrl;
} else {
return url;
}
}