-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
index.js
865 lines (792 loc) · 25.5 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const JSON5 = require('json5');
const path = require('path');
const {
createCtrlcHandler,
exitCtrlcHandler,
} = require('../../common/ctrlcHandler');
const {
escapeHtml,
log,
waitForPageLoad,
verifySelectorsInvisible,
verifySelectorsVisible,
sleep,
} = require('./helpers');
const {
gitBranchName,
gitCommitterEmail,
gitCiMasterBaseline,
shortSha,
} = require('../../common/git');
const {buildRuntime, installPackages} = require('../../common/utils');
const {cyan, yellow} = require('kleur/colors');
const {execScriptAsync} = require('../../common/exec');
const {isCiBuild} = require('../../common/ci');
const {startServer, stopServer} = require('../serve');
const {waitUntilUsed} = require('tcp-port-used');
// optional dependencies for local development (outside of visual diff tests)
let puppeteer;
let percySnapshot;
// CSS injected in every page tested.
// Normally, as in https://docs.percy.io/docs/percy-specific-css
// Otherwise, as a <style> in an iframe, see snippets/iframe-wrapper.js
const percyCss = [
// Loader animation may otherwise be captured in slightly different points,
// causing the test to flake.
'.i-amphtml-new-loader * { animation: none !important; }',
].join('\n');
// REPEATING TODO(@ampproject/wg-infra): Update this whenever the Percy backend
// starts using a new version of Chrome to render DOM snapshots.
//
// Steps:
// 1. Open a recent Percy build, and click the “ⓘ” icon
// 2. Note the Chrome major version at the bottom
// 3. Look up the full version at https://en.wikipedia.org/wiki/Google_Chrome_version_history
// 4. Open https://omahaproxy.appspot.com in a browser
// 5. Go to "Tools" -> "Version information"
// 6. Paste the full version in the "Version" field and click "Lookup"
// 7. Copy the value next to "Branch Base Position" and update the line below
const PUPPETEER_CHROMIUM_REVISION = '827102'; // 88.0.4324.0
const SNAPSHOT_SINGLE_BUILD_OPTIONS = {
widths: [375],
};
const VIEWPORT_WIDTH = 1400;
const VIEWPORT_HEIGHT = 100000;
const HOST = 'localhost';
const PORT = 8000;
const PERCY_AGENT_PORT = 5338;
const PERCY_AGENT_RETRY_MS = 100;
const PERCY_AGENT_TIMEOUT_MS = 5000;
const NAVIGATE_TIMEOUT_MS = 30000;
const MAX_PARALLEL_TABS = 5;
const WAIT_FOR_TABS_MS = 1000;
const ROOT_DIR = path.resolve(__dirname, '../../../');
// JavaScript snippets that execute inside the page.
const WRAP_IN_IFRAME_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/iframe-wrapper.js'),
'utf8'
);
const REMOVE_AMP_SCRIPTS_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/remove-amp-scripts.js'),
'utf8'
);
const FREEZE_FORM_VALUE_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/freeze-form-values.js'),
'utf8'
);
const FREEZE_CANVAS_IMAGE_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/freeze-canvas-image.js'),
'utf8'
);
// HTML snippet to create an error page snapshot.
const SNAPSHOT_ERROR_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/snapshot-error.html'),
'utf8'
);
let browser_;
let percyAgentProcess_;
/**
* Override PERCY_* environment variables if passed via gulp task parameters.
*/
function maybeOverridePercyEnvironmentVariables() {
['percy_token', 'percy_branch'].forEach((variable) => {
if (variable in argv) {
process.env[variable.toUpperCase()] = argv[variable];
}
});
}
/**
* Disambiguates branch names by decorating them with the commit author name.
* We do this for all non-push builds in order to prevent them from being used
* as baselines for future builds.
*/
function setPercyBranch() {
if (!process.env['PERCY_BRANCH'] && (!argv.master || !isCiBuild())) {
const userName = gitCommitterEmail();
const branchName = gitBranchName();
process.env['PERCY_BRANCH'] = userName + '-' + branchName;
}
}
/**
* Set the branching point's SHA to an env variable.
*
* This will let Percy determine which build to use as the baseline for this new
* build.
*
* Only does something during CI, and for non-master branches, since master
* builds are always built on top of the previous commit (we use the squash and
* merge method for pull requests.)
*/
function setPercyTargetCommit() {
if (isCiBuild() && !argv.master) {
process.env['PERCY_TARGET_COMMIT'] = gitCiMasterBaseline();
}
}
/**
* Launches a @percy/agent instance.
*/
async function launchPercyAgent() {
if (argv.percy_disabled) {
return;
}
const env = argv.percy_agent_debug ? {LOG_LEVEL: 'debug'} : {};
percyAgentProcess_ = execScriptAsync(
`npx percy start --port ${PERCY_AGENT_PORT}`,
{
cwd: __dirname,
env: Object.assign(env, process.env),
stdio: ['ignore', process.stdout, process.stderr],
}
);
await waitUntilUsed(
PERCY_AGENT_PORT,
PERCY_AGENT_RETRY_MS,
PERCY_AGENT_TIMEOUT_MS
);
log('info', 'Percy agent is reachable on port', PERCY_AGENT_PORT);
}
/**
* Launches an AMP webserver for minified js.
*/
async function launchWebServer() {
await startServer(
{host: HOST, port: PORT},
{quiet: !argv.webserver_debug},
{compiled: true}
);
}
/**
* Launches a Puppeteer controlled browser.
*
* Waits until the browser is up and reachable, and ties its lifecycle to this
* process's lifecycle.
*
* @return {!puppeteer.Browser} a Puppeteer controlled browser.
*/
async function launchBrowser() {
const browserOptions = {
args: ['--no-sandbox', '--disable-extensions', '--disable-gpu'],
dumpio: argv.chrome_debug,
headless: true,
};
try {
browser_ = await puppeteer.launch(browserOptions);
} catch (error) {
log('fatal', error);
}
return browser_;
}
/**
* Opens a new browser tab, resizes its viewport, and returns a Page handler.
*
* @param {!puppeteer.Browser} browser a Puppeteer controlled browser.
* @param {JsonObject} viewport optional viewport size object with numeric
* fields `width` and `height`.
* @return {!Promise<!Puppeteer.Page>}
*/
async function newPage(browser, viewport = null) {
log('verbose', 'Creating new tab');
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
await page.setJavaScriptEnabled(true);
await page.setRequestInterception(true);
page.on('request', (interceptedRequest) => {
const requestUrl = new URL(interceptedRequest.url());
const mockedFilepath = path.join(
path.dirname(__filename),
'network-mocks',
requestUrl.hostname,
encodeURIComponent(
`${requestUrl.pathname.substr(1)}${requestUrl.search}`
).replace(/%2F/g, '/')
);
if (
requestUrl.protocol === 'data:' ||
requestUrl.hostname === HOST ||
requestUrl.hostname.endsWith(`.${HOST}`)
) {
return interceptedRequest.continue();
} else if (fs.existsSync(mockedFilepath)) {
log(
'verbose',
'Mocked network request for',
yellow(requestUrl.href),
'with file',
cyan(mockedFilepath)
);
return interceptedRequest.respond(fs.readFileSync(mockedFilepath));
} else {
log(
'verbose',
'Blocked external network request for',
yellow(requestUrl.href)
);
return interceptedRequest.abort('blockedbyclient');
}
});
await resetPage(page, viewport);
return page;
}
/**
* Resets the size of a tab and loads about:blank.
*
* @param {!puppeteer.Page} page a Puppeteer control browser tab/page.
* @param {JsonObject} viewport optional viewport size object with numeric
* fields `width` and `height`.
*/
async function resetPage(page, viewport = null) {
const width = viewport ? viewport.width : VIEWPORT_WIDTH;
const height = viewport ? viewport.height : VIEWPORT_HEIGHT;
log(
'verbose',
'Resetting tab to',
yellow('about:blank'),
'with size',
yellow(`${width}×${height}`)
);
await page.goto('about:blank');
await page.setViewport({width, height});
}
/**
* Adds a test error and logs it if running locally (not as part of CI).
*
* @param {!Array<!JsonObject>} testErrors array of testError objects.
* @param {string} name full name of the test.
* @param {string} message extra information about the failure.
* @param {Error} error error object with stack trace.
* @param {!Array<puppeteer.ConsoleMessage>} consoleMessages array of console
* messages printed so far.
*/
function addTestError(testErrors, name, message, error, consoleMessages) {
const testError = {name, message, error, consoleMessages};
if (!isCiBuild()) {
logTestError(testError);
}
testErrors.push(testError);
}
/**
* Logs a test error (regardless of where it's running).
*
* @param {!JsonObject} testError object as created by addTestError.
*/
function logTestError(testError) {
log(
'error',
'Error in test',
yellow(testError.name),
'\n ',
testError.message,
'\n ',
testError.error
);
if (testError.consoleMessages.length > 0) {
log(
'error',
cyan(testError.consoleMessages.length),
'Console messages in the browser so far:'
);
for (const message of testError.consoleMessages) {
log('error', cyan(`[console.${message.type()}]`), message.text());
}
}
}
/**
* Runs the visual tests.
*
* @param {!Array<JsonObject>} webpages an array of JSON objects containing
* details about the pages to snapshot.
*/
async function runVisualTests(webpages) {
// Create a Percy client and start a build.
if (process.env['PERCY_TARGET_COMMIT']) {
log(
'info',
'The Percy build is baselined on top of commit',
cyan(shortSha(process.env['PERCY_TARGET_COMMIT']))
);
}
// Take the snapshots.
await generateSnapshots(webpages);
}
/**
* Sets the AMP config, launches a server, and generates Percy snapshots for a
* set of given webpages.
*
* @param {!Array<JsonObject>} webpages an array of JSON objects containing
* details about the pages to snapshot.
*/
async function generateSnapshots(webpages) {
const numUnfilteredPages = webpages.length;
webpages = webpages.filter((webpage) => !webpage.flaky);
if (numUnfilteredPages != webpages.length) {
log(
'info',
'Skipping',
cyan(numUnfilteredPages - webpages.length),
'flaky pages'
);
}
if (argv.grep) {
webpages = webpages.filter((webpage) => argv.grep.test(webpage.name));
log(
'info',
cyan(`--grep ${argv.grep}`),
'matched',
cyan(webpages.length),
'pages'
);
}
// Expand all the interactive tests. Every test should have a base test with
// no interactions, and each test that has in interactive tests file should
// load those tests here.
for (const webpage of webpages) {
webpage.tests_ = {};
if (!webpage.no_base_test) {
webpage.tests_[''] = async () => {};
}
if (webpage.interactive_tests) {
try {
Object.assign(
webpage.tests_,
require(path.resolve(ROOT_DIR, webpage.interactive_tests))
);
} catch (error) {
log(
'fatal',
'Failed to load interactive test',
cyan(webpage.interactive_tests),
'for test',
cyan(webpage.name),
'\nError:',
error
);
}
}
}
const totalTests = webpages.reduce(
(numTests, webpage) => numTests + Object.keys(webpage.tests_).length,
0
);
if (!totalTests) {
log('fatal', 'No pages left to test!');
} else {
log(
'info',
'Executing',
cyan(totalTests),
'visual diff tests on',
cyan(webpages.length),
'pages'
);
}
const browser = await launchBrowser();
if (argv.master) {
const page = await newPage(browser);
await page.goto(
`http://${HOST}:${PORT}/examples/visual-tests/blank-page/blank.html`
);
await percySnapshot(page, 'Blank page', SNAPSHOT_SINGLE_BUILD_OPTIONS);
}
log('info', 'Generating snapshots...');
if (!(await snapshotWebpages(browser, webpages))) {
log('fatal', 'Some tests have failed locally.');
}
}
/**
* Generates Percy snapshots for a set of given webpages.
*
* @param {!puppeteer.Browser} browser a Puppeteer controlled browser.
* @param {!Array<!JsonObject>} webpages an array of JSON objects containing
* details about the webpages to snapshot.
* @return {boolean} true if all tests passed locally (does not indicate whether
* the tests passed on Percy).
*/
async function snapshotWebpages(browser, webpages) {
const availablePages = [];
const allPages = [];
log('verbose', 'Preallocating', cyan(MAX_PARALLEL_TABS), 'tabs...');
for (let i = 0; i < MAX_PARALLEL_TABS; i++) {
const page = await newPage(browser);
availablePages.push(page);
allPages.push(page);
}
const pagePromises = [];
const testErrors = [];
let testNumber = 0;
for (const webpage of webpages) {
const {viewport, name: pageName} = webpage;
let hasWarnings = false;
for (const [testName, testFunction] of Object.entries(webpage.tests_)) {
// Chrome supports redirecting <anything>.localhost to localhost, while
// respecting domain name boundaries. This allows each test to be
// sandboxed from other tests, with respect to things like cookies and
// localStorage. Since Puppeteer only ever executes on Chrome, this is
// fine.
const fullUrl = `http://${testNumber++}.${HOST}:${PORT}/${webpage.url}`;
while (availablePages.length == 0) {
await sleep(WAIT_FOR_TABS_MS);
}
const page = availablePages.shift();
const name = testName ? `${pageName} (${testName})` : pageName;
log(
'info',
'Starting test',
yellow(name),
'on tab',
yellow(`#${allPages.indexOf(page) + 1}`)
);
await resetPage(page, viewport);
const consoleMessages = [];
const consoleLogger = (consoleMessage) => {
consoleMessages.push(consoleMessage);
};
page.on('console', consoleLogger);
// Puppeteer is flaky when it comes to catching navigation requests, so
// retry the page navigation up to NAVIGATE_RETRIES times and eventually
// ignore a final timeout. If this ends up being a real non-loading page
// error, this will be caught in the resulting Percy build. Also attempt
// to wait until there are no more network requests. This method is flaky
// since Puppeteer doesn't always understand Chrome's network activity, so
// ignore timeouts again.
const pagePromise = (async () => {
try {
const responseWatcher = new Promise((resolve, reject) => {
const responseTimeout = setTimeout(() => {
reject(
new puppeteer.TimeoutError(
`Response was not received in test ${testName} for page ` +
`${webpage.url} after ${NAVIGATE_TIMEOUT_MS}ms`
)
);
}, NAVIGATE_TIMEOUT_MS);
page.once('response', (response) => {
log(
'verbose',
'Response for url',
yellow(response.url()),
'with status',
cyan(response.status()),
cyan(response.statusText())
);
clearTimeout(responseTimeout);
resolve();
});
});
log('verbose', 'Navigating to page', yellow(webpage.url));
await Promise.all([
responseWatcher,
page.goto(fullUrl, {waitUntil: 'networkidle2'}),
]);
log(
'verbose',
'Page navigation of test',
yellow(name),
'is done, verifying page'
);
} catch (navigationError) {
hasWarnings = true;
addTestError(
testErrors,
name,
'The browser test runner failed to complete the navigation to the test page',
navigationError,
consoleMessages
);
log('warning', 'Continuing to verify page regardless...');
}
let performSnapshot = true;
try {
// Perform visibility checks: wait for all AMP built-in loader dots
// to disappear (i.e., all visible components are finished being
// layed out and external resources such as images are loaded and
// displayed), then, depending on the test configurations, wait for
// invisibility/visibility of specific elements that match the
// configured CSS selectors.
await waitForPageLoad(page, name);
if (webpage.loading_incomplete_selectors) {
await verifySelectorsInvisible(
page,
name,
webpage.loading_incomplete_selectors
);
}
if (webpage.loading_complete_selectors) {
await verifySelectorsVisible(
page,
name,
webpage.loading_complete_selectors
);
}
// Based on test configuration, wait for a specific amount of time.
if (webpage.loading_complete_delay_ms) {
log(
'verbose',
'Waiting',
cyan(`${webpage.loading_complete_delay_ms}ms`),
'for loading to complete'
);
await sleep(webpage.loading_complete_delay_ms);
}
// Run any other custom code located in the test's interactive_tests
// file. If there is no interactive test, this defaults to an empty
// function.
await testFunction(page, name);
} catch (testError) {
performSnapshot = false;
addTestError(
testErrors,
name,
'Test page failed',
testError,
consoleMessages
);
let htmlSnapshot;
try {
htmlSnapshot = await page.content();
} catch (e) {
htmlSnapshot = e.message;
}
await page.setContent(
SNAPSHOT_ERROR_SNIPPET.replace('__TEST_NAME__', name)
.replace('__TEST_ERROR__', testError)
.replace('__HTML_SNAPSHOT__', escapeHtml(htmlSnapshot))
);
await percySnapshot(page, name, SNAPSHOT_SINGLE_BUILD_OPTIONS);
}
if (performSnapshot) {
try {
// Execute post-scripts that clean up the page's HTML and send
// prepare it for snapshotting on Percy. See comments inside the
// snippet files for description of each.
await page.evaluate(REMOVE_AMP_SCRIPTS_SNIPPET);
await page.evaluate(FREEZE_FORM_VALUE_SNIPPET);
await page.evaluate(FREEZE_CANVAS_IMAGE_SNIPPET);
// Create a default set of snapshot options for Percy and modify
// them based on the test's configuration.
const snapshotOptions = {};
if (webpage.enable_percy_javascript) {
snapshotOptions.enableJavaScript = true;
}
if (viewport) {
snapshotOptions.widths = [viewport.width];
log('verbose', 'Wrapping viewport-constrained page in an iframe');
await page.evaluate(
WRAP_IN_IFRAME_SNIPPET.replace(/__WIDTH__/g, viewport.width)
.replace(/__HEIGHT__/g, viewport.height)
.replace(/__PERCY_CSS__/g, percyCss)
);
} else {
snapshotOptions.percyCss = percyCss;
}
// Finally, send the snapshot to percy.
await percySnapshot(page, name, snapshotOptions);
} catch (snapshotError) {
addTestError(
testErrors,
name,
'Failed to set up or take the Percy snapshot',
snapshotError,
consoleMessages
);
throw snapshotError;
}
}
log(
hasWarnings ? 'warning' : 'info',
'Finished test',
yellow(name),
hasWarnings ? 'with warnings' : ''
);
page.removeListener('console', consoleLogger);
availablePages.push(page);
})();
pagePromises.push(pagePromise);
}
}
await Promise.all(pagePromises);
if (isCiBuild() && testErrors.length > 0) {
testErrors.sort((a, b) => a.name.localeCompare(b.name));
log('info', yellow('Tests warnings and errors:'));
testErrors.forEach(logTestError);
return false;
}
return true;
}
/**
* Enables debugging if requested via command line.
*/
function setDebuggingLevel() {
if (argv.debug) {
argv['chrome_debug'] = true;
argv['webserver_debug'] = true;
argv['percy_agent_debug'] = true;
}
}
/**
* Creates a Percy build with only a blank page for comparison.
*
* Enables us to require percy checks on GitHub, and yet, not have to do a full
* build for every PR.
*/
async function createEmptyBuild() {
log('info', 'Skipping visual diff tests and generating a blank Percy build');
const browser = await launchBrowser();
const page = await newPage(browser);
try {
await page.goto(
`http://${HOST}:${PORT}/examples/visual-tests/blank-page/blank.html`
);
} catch {
// Ignore failures
}
await percySnapshot(page, 'Blank page', SNAPSHOT_SINGLE_BUILD_OPTIONS);
}
/**
* Runs the AMP visual diff tests.
* @return {!Promise}
*/
async function visualDiff() {
const handlerProcess = createCtrlcHandler('visual-diff');
await ensureOrBuildAmpRuntimeInTestMode_();
installPercy_();
setupCleanup_();
maybeOverridePercyEnvironmentVariables();
setPercyBranch();
setPercyTargetCommit();
if (argv.grep) {
argv.grep = RegExp(argv.grep);
}
await performVisualTests();
await cleanup_();
exitCtrlcHandler(handlerProcess);
}
/**
* Runs the AMP visual diff tests.
*/
async function performVisualTests() {
setDebuggingLevel();
if (!argv.percy_disabled && !process.env.PERCY_TOKEN) {
log('fatal', 'Could not find', cyan('PERCY_TOKEN'), 'environment variable');
} else {
try {
await launchPercyAgent();
} catch (reason) {
log('fatal', `Failed to start the Percy agent: ${reason}`);
}
}
// Launch a local web server.
try {
await launchWebServer();
} catch (reason) {
log('fatal', `Failed to start a web server: ${reason}`);
}
if (argv.empty) {
await createEmptyBuild();
} else {
// Load and parse the config. Use JSON5 due to JSON comments in file.
const visualTestsConfig = JSON5.parse(
fs.readFileSync(
path.resolve(__dirname, '../../../test/visual-diff/visual-tests'),
'utf8'
)
);
await runVisualTests(visualTestsConfig.webpages);
}
}
async function ensureOrBuildAmpRuntimeInTestMode_() {
if (argv.empty) {
return;
}
if (argv.nobuild) {
const isInTestMode = /AMP_CONFIG=\{(?:.+,)?"test":true\b/.test(
fs.readFileSync('dist/v0.js', 'utf8')
);
if (!isInTestMode) {
log(
'fatal',
'The AMP runtime was not built in test mode. Run',
cyan('gulp dist --fortesting'),
'or remove the',
cyan('--nobuild'),
'option from this command'
);
}
} else {
await buildRuntime(/* opt_compiled */ true);
}
}
function installPercy_() {
process.env['PUPPETEER_CHROMIUM_REVISION'] = PUPPETEER_CHROMIUM_REVISION;
if (!argv.noinstall) {
installPackages(__dirname);
}
puppeteer = require('puppeteer');
percySnapshot = require('@percy/puppeteer').percySnapshot;
}
function setupCleanup_() {
process.on('exit', cleanup_);
process.on('SIGINT', cleanup_);
process.on('uncaughtException', cleanup_);
process.on('unhandledRejection', cleanup_);
}
async function exitPercyAgent_() {
if (percyAgentProcess_ && !percyAgentProcess_.killed) {
let resolver;
const percyAgentExited_ = new Promise((resolverIn) => {
resolver = resolverIn;
});
percyAgentProcess_.on('exit', () => {
resolver();
});
// Explicitly exit the process by "Ctrl+C"-ing it.
await percyAgentProcess_.kill('SIGINT');
await percyAgentExited_;
}
}
async function cleanup_() {
if (browser_) {
await browser_.close();
}
await stopServer();
await exitPercyAgent_();
}
module.exports = {
visualDiff,
};
visualDiff.description = 'Runs the AMP visual diff tests.';
visualDiff.flags = {
'master': ' Includes a blank snapshot (baseline for skipped builds)',
'empty': ' Creates a dummy Percy build with only a blank snapshot',
'config':
' Sets the runtime\'s AMP_CONFIG to one of "prod" (default) or "canary"',
'chrome_debug': ' Prints debug info from Chrome',
'webserver_debug': ' Prints debug info from the local gulp webserver',
'percy_agent_debug': ' Prints debug info from the @percy/agent instance',
'debug': ' Sets all debugging flags',
'verbose': ' Prints verbose log statements',
'grep': ' Runs tests that match the pattern',
'percy_token': ' Override the PERCY_TOKEN environment variable',
'percy_branch': ' Override the PERCY_BRANCH environment variable',
'percy_disabled':
' Disables Percy integration (for testing local changes only)',
'nobuild': ' Skip build',
'noinstall': ' Skip installing npm dependencies',
};