|
| 1 | +import { BasePage, test } from '@test/e2e/helpers' |
| 2 | + |
| 3 | +const pages = [ |
| 4 | + '/', |
| 5 | + '/about', |
| 6 | + '/articles', |
| 7 | + '/articles/demo', |
| 8 | + '/case-studies', |
| 9 | + '/consent', |
| 10 | + '/contact', |
| 11 | + '/my-data', |
| 12 | + '/newsletter', |
| 13 | + '/newsletter/confirm/error-token', |
| 14 | + '/newsletter/confirm/expired-token', |
| 15 | + '/newsletter/confirm/success', |
| 16 | + '/offline', |
| 17 | + '/privacy', |
| 18 | + '/search', |
| 19 | + '/services', |
| 20 | + '/terms', |
| 21 | + '/404', |
| 22 | +] |
| 23 | + |
| 24 | +const pagesWithForms = [ |
| 25 | + '/', |
| 26 | + '/articles/demo', |
| 27 | + '/consent', |
| 28 | + '/contact', |
| 29 | + '/privacy/my-data', |
| 30 | + '/newsletter', |
| 31 | + '/search', |
| 32 | +] |
| 33 | + |
| 34 | +interface PageLoopDiagnostics { |
| 35 | + currentUrl: string |
| 36 | + pathname: string |
| 37 | + readyState: string |
| 38 | + title: string |
| 39 | +} |
| 40 | + |
| 41 | +const getPageLoopDiagnostics = async (page: BasePage): Promise<PageLoopDiagnostics> => { |
| 42 | + if (page.page.isClosed()) { |
| 43 | + return { |
| 44 | + currentUrl: 'page-closed', |
| 45 | + pathname: 'unavailable', |
| 46 | + readyState: 'unavailable', |
| 47 | + title: 'unavailable', |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const currentUrl = page.page.url() |
| 52 | + |
| 53 | + const metadata = await page.evaluate(() => { |
| 54 | + return { |
| 55 | + pathname: window.location.pathname, |
| 56 | + readyState: document.readyState, |
| 57 | + title: document.title || '', |
| 58 | + } |
| 59 | + }).catch(() => { |
| 60 | + return { |
| 61 | + pathname: 'unavailable', |
| 62 | + readyState: 'unavailable', |
| 63 | + title: 'unavailable', |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + return { |
| 68 | + currentUrl, |
| 69 | + pathname: metadata.pathname, |
| 70 | + readyState: metadata.readyState, |
| 71 | + title: metadata.title, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const wrapPageLoopError = async ( |
| 76 | + page: BasePage, |
| 77 | + url: string, |
| 78 | + error: unknown, |
| 79 | + formPagesOnly = false |
| 80 | +) => { |
| 81 | + const diagnostics = await getPageLoopDiagnostics(page) |
| 82 | + const message = error instanceof Error ? error.message : String(error) |
| 83 | + |
| 84 | + throw new Error( |
| 85 | + [ |
| 86 | + `Looped accessibility check failed on ${url}.`, |
| 87 | + `pageSet=${formPagesOnly ? 'pagesWithForms' : 'pages'}`, |
| 88 | + `currentUrl=${diagnostics.currentUrl}`, |
| 89 | + `pathname=${diagnostics.pathname}`, |
| 90 | + `title="${diagnostics.title}"`, |
| 91 | + `readyState=${diagnostics.readyState}`, |
| 92 | + `originalError=${message}`, |
| 93 | + ].join(' '), |
| 94 | + { cause: error instanceof Error ? error : undefined } |
| 95 | + ) |
| 96 | +} |
| 97 | + |
| 98 | +export const runAcrossPages = async ( |
| 99 | + page: BasePage, |
| 100 | + label: string, |
| 101 | + callback: (_url: string) => Promise<void>, |
| 102 | + formPagesOnly = false |
| 103 | +) => { |
| 104 | + const loopPages = formPagesOnly ? pagesWithForms : pages |
| 105 | + |
| 106 | + for (const url of loopPages) { |
| 107 | + await test.step(`${label} on ${url}`, async () => { |
| 108 | + try { |
| 109 | + await callback(url) |
| 110 | + } catch (error) { |
| 111 | + await wrapPageLoopError(page, url, error, formPagesOnly) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments