MCPcopy
hub / github.com/ampproject/amphtml / snapshotWebpages

Function snapshotWebpages

build-system/tasks/visual-diff/index.js:349–553  ·  view source on GitHub ↗

* Generates Percy snapshots for a set of given webpages. * * @param {!puppeteer.Browser} browser a Puppeteer controlled browser. * @param {!Array<!WebpageDef>} webpages an array of JSON objects containing * details about the webpages to snapshot. * @return {Promise } true if all tes

(browser, webpages)

Source from the content-addressed store, hash-verified

347 * indicate whether the tests passed on Percy).
348 */
349async function snapshotWebpages(browser, webpages) {
350 const availablePages = [];
351 const allPages = [];
352
353 log('verbose', 'Preallocating', cyan(MAX_PARALLEL_TABS), 'tabs...');
354 for (let i = 0; i < MAX_PARALLEL_TABS; i++) {
355 const page = await newPage(browser);
356 availablePages.push(page);
357 allPages.push(page);
358 }
359
360 const pagePromises = [];
361 const testErrors = [];
362 let testNumber = 0;
363 for (const webpage of webpages) {
364 const {name: pageName, viewport} = webpage;
365 let hasWarnings = false;
366 for (const [testName, testFunction] of Object.entries(webpage.tests_)) {
367 // Chrome supports redirecting <anything>.localhost to localhost, while
368 // respecting domain name boundaries. This allows each test to be
369 // sandboxed from other tests, with respect to things like cookies and
370 // localStorage. Since Puppeteer only ever executes on Chrome, this is
371 // fine.
372 const fullUrl = `http://${testNumber++}.${HOST}:${PORT}/${webpage.url}`;
373 while (availablePages.length == 0) {
374 await sleep(WAIT_FOR_TABS_MS);
375 }
376 const [page] = availablePages;
377 availablePages.shift();
378
379 const name = testName ? `${pageName} (${testName})` : pageName;
380 log(
381 'info',
382 drawBoxes(allPages, availablePages, page, yellow('▄')),
383 'Starting test',
384 yellow(name)
385 );
386
387 await resetPage(page, viewport);
388
389 const consoleMessages = [];
390 const consoleLogger = (consoleMessage) => {
391 consoleMessages.push(consoleMessage);
392 };
393 page.on('console', consoleLogger);
394
395 const pagePromise = (async () => {
396 try {
397 log('verbose', 'Navigating to page', yellow(webpage.url));
398 await page.goto(fullUrl, {waitUntil: 'networkidle0'});
399
400 log(
401 'verbose',
402 'Page navigation of test',
403 yellow(name),
404 'is done, verifying page'
405 );
406 } catch (navigationError) {

Callers 1

runVisualTestsFunction · 0.85

Calls 15

cyanFunction · 0.85
newPageFunction · 0.85
drawBoxesFunction · 0.85
yellowFunction · 0.85
resetPageFunction · 0.85
addTestErrorFunction · 0.85
waitForPageLoadFunction · 0.85
verifySelectorsInvisibleFunction · 0.85
verifySelectorsVisibleFunction · 0.85
isCiBuildFunction · 0.85
setContentMethod · 0.80
offMethod · 0.80

Tested by

no test coverage detected