-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
katiegoines
committed
Mar 18, 2024
1 parent
8b69a08
commit ce5c996
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { launch } from 'puppeteer'; | ||
import { getSitemapUrls } from './get-sitemap-links'; | ||
|
||
const excludedErrors = [ | ||
{ | ||
type: 'Shortbread', | ||
errorText: | ||
"Shortbread failed to set user's cookie preference because the domain name that was passed" | ||
} | ||
]; | ||
|
||
const excludedScripts = [ | ||
'prod.assets.shortbread.aws', | ||
'prod.tools.shortbread.aws', | ||
'aa0.awsstatic.com' | ||
]; | ||
|
||
const checkPage = async (url) => { | ||
const errorsFound = []; | ||
let browser = await launch({ headless: 'new' }); | ||
|
||
const page = await browser.newPage(); | ||
|
||
page | ||
.on('pageerror', (message) => { | ||
let errorText = message.message; | ||
const excluded = excludedErrors.some((excludedError) => { | ||
return errorText.includes(excludedError.errorText); | ||
}); | ||
|
||
if (!excluded) { | ||
errorsFound.push({ | ||
page: url, | ||
message: errorText | ||
}); | ||
} | ||
}) | ||
.on('console', (message) => { | ||
if (message.type().toLowerCase() === 'error') { | ||
let errorText = message.text(); | ||
let callingScript = message.location().url; | ||
const excludedFromError = excludedErrors.some((excludedError) => { | ||
return errorText.includes(excludedError.errorText); | ||
}); | ||
const excludedFromScript = excludedScripts.some((excludedScript) => { | ||
return callingScript.includes(excludedScript); | ||
}); | ||
const excluded = excludedFromError || excludedFromScript; | ||
|
||
if (!excluded) { | ||
errorsFound.push({ | ||
page: url, | ||
message: errorText | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
await page.goto(url, { waitUntil: 'domcontentloaded' }); | ||
|
||
await browser.close(); | ||
|
||
return errorsFound; | ||
}; | ||
|
||
const consoleErrors = async (domain) => { | ||
let pagesToCheck = await getSitemapUrls(domain); | ||
let errorMessage = ''; | ||
for (let i = 0; i < pagesToCheck.length; i++) { | ||
let url = pagesToCheck[i]; | ||
console.log(`checking page ${url}`); | ||
let errorsFound = await checkPage(url); | ||
errorsFound.forEach((error) => { | ||
errorMessage += `${error.message} found on ${error.page}\n`; | ||
}); | ||
} | ||
console.log(errorMessage); | ||
return errorMessage; | ||
}; | ||
|
||
export async function consoleErrors(domain = 'http://localhost:3000') { | ||
return await consoleErrors(domain); | ||
} |