From 2ffeeb30f2f9ad23012895bcccc48c706e4c3185 Mon Sep 17 00:00:00 2001 From: Robert DeLuca Date: Wed, 3 Jul 2019 17:26:42 -0500 Subject: [PATCH] fix: Reject redirected requests for static snapshots This makes it so we no longer get redirected from the URL we visit to another one. That usually results in an error, which fails the test suite. This also adds some try/catches around parts of the snapshot that could fail with helpful error messages --- src/services/static-snapshot-service.ts | 47 +++++++++++++++++-------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/src/services/static-snapshot-service.ts b/src/services/static-snapshot-service.ts index e5f8c4d5..62bde8fc 100644 --- a/src/services/static-snapshot-service.ts +++ b/src/services/static-snapshot-service.ts @@ -44,27 +44,46 @@ export default class StaticSnapshotService { const percyAgentClientFilename = agentJsFilename() const page = await browser.newPage() + // Do not follow redirects to ensure we don't navigate to another page + await page.setRequestInterception(true) + page.on('request', (request) => { + if (request.isNavigationRequest() && request.redirectChain().length) { + logger.debug(`Skipping redirect: ${request.url()}`) + request.abort() + } else { + request.continue() + } + }) + const pageUrls = await this._buildPageUrls() for (const url of pageUrls) { logger.debug(`visiting ${url}`) - await page.goto(url, { waitUntil: 'networkidle0' }) - - await page.addScriptTag({ - path: percyAgentClientFilename, - }) - - await page.evaluate((url) => { - const percyAgentClient = new PercyAgent() - const parsedURL = new URL(url) - const snapshotName = parsedURL.pathname || url - - return percyAgentClient.snapshot(snapshotName) - }, url) + try { + await page.goto(url, { waitUntil: 'networkidle2' }) + } catch (error) { + logger.error(`Failed to navigate to ${url}, skipping. Error: ${error}`) + } + + try { + await page.addScriptTag({ + path: percyAgentClientFilename, + }) + + await page.evaluate((url) => { + const percyAgentClient = new PercyAgent() + const parsedURL = new URL(url) + const snapshotName = parsedURL.pathname || url + + return percyAgentClient.snapshot(snapshotName) + }, url) + } catch (error) { + logger.error(`Failed to inject agent JS: ${error}`) + } } - browser.close() + await browser.close() } async stop() {