Skip to content

Commit bf8603c

Browse files
check for bad status codes before injecting axe
1 parent 38d1844 commit bf8603c

File tree

1 file changed

+83
-55
lines changed

1 file changed

+83
-55
lines changed

cypress/e2e/a11y.cy.js

Lines changed: 83 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,108 @@
11
import { formatWCAGTag } from '../support/formatWCAGTag'
22

3-
describe('a11y tests', () => {
4-
const pages = ['/']
3+
describe('accessibility tests', () => {
4+
const pages = ['/', '/admin']
55

66
pages.forEach((page) => {
77
it(`WCAG 2.2 accessibility evaluation of ${
88
page === '/' ? 'Homepage' : page
99
}`, () => {
10-
cy.visit(page, { failOnStatusCode: false })
11-
cy.injectAxe()
10+
cy.request({
11+
url: page,
12+
failOnStatusCode: false,
13+
}).then((response) => {
14+
const statusCode = response.status
1215

13-
const safeName = page.replace(/^\//, '').toLowerCase()
14-
const reportPath = `cypress/reports/a11y-report-wcag-${
15-
safeName === '' ? 'homepage' : safeName
16-
}.json`
16+
if (statusCode >= 400) {
17+
cy.task(
18+
'log',
19+
'\n===================================================================================='
20+
)
21+
cy.task(
22+
'log',
23+
`⚠️ Skipping Accessibility Check: ${page} failed to load (Status: ${statusCode}).`
24+
)
25+
cy.task(
26+
'log',
27+
'===================================================================================='
28+
)
29+
return
30+
}
31+
cy.visit(page)
32+
cy.injectAxe()
1733

18-
cy.window()
19-
.its('axe')
20-
.then((axe) => {
21-
const options = {
22-
runOnly: {
23-
type: 'tag',
24-
values: [
25-
'wcag2a',
26-
'wcag2aa',
27-
'wcag21a',
28-
'wcag21aa',
29-
'wcag22a',
30-
'wcag22aa',
31-
],
32-
},
33-
}
34+
const safeName = page.replace(/^\//, '').toLowerCase()
35+
const reportPath = `cypress/reports/a11y-report-wcag-${
36+
safeName === '' ? 'homepage' : safeName
37+
}.json`
3438

35-
return axe.run(options).then((results) => {
36-
cy.task(
37-
'log',
38-
`\n=== WCAG 2.2 accessibility evaluation of ${
39-
page === '/' ? 'Homepage' : page
40-
} ===`
41-
)
39+
cy.window()
40+
.its('axe')
41+
.then((axe) => {
42+
const options = {
43+
runOnly: {
44+
type: 'tag',
45+
values: [
46+
'wcag2a',
47+
'wcag2aa',
48+
'wcag21a',
49+
'wcag21aa',
50+
'wcag22a',
51+
'wcag22aa',
52+
],
53+
},
54+
}
4255

43-
const categories = [
44-
{ key: 'violations', label: `❌ failed` },
45-
{ key: 'passes', label: `✅ passed` },
46-
{ key: 'incomplete', label: `⚠️ incomplete` },
47-
{ key: 'inapplicable', label: `⏭️ inapplicable` },
48-
]
49-
50-
categories.forEach(({ key, label }) => {
51-
const items = results[key] || []
56+
return axe.run(options).then((results) => {
5257
cy.task(
5358
'log',
54-
`\n=== ${label} (${items.length}) ===`
59+
`\n=== WCAG 2.2 accessibility evaluation of ${
60+
page === '/' ? 'Homepage' : page
61+
} ===`
5562
)
5663

57-
items.forEach((rule) => {
58-
const wcagRefs =
59-
(rule.tags || [])
60-
.filter((tag) => /^wcag/i.test(tag))
61-
.map((tag) => formatWCAGTag(tag))
62-
.join(', ') || 'no WCAG reference'
64+
const categories = [
65+
{ key: 'violations', label: `❌ failed` },
66+
{ key: 'passes', label: `✅ passed` },
67+
{ key: 'incomplete', label: `⚠️ incomplete` },
68+
{
69+
key: 'inapplicable',
70+
label: `⏭️ inapplicable`,
71+
},
72+
]
6373

64-
cy.task('log', `→ Rule: ${rule.id}`)
65-
cy.task('log', ` Description: ${rule.help}`)
74+
categories.forEach(({ key, label }) => {
75+
const items = results[key] || []
6676
cy.task(
6777
'log',
68-
` Impact: ${rule.impact || 'n/a'}`
78+
`\n===== ${label} (${items.length}) =====`
6979
)
70-
cy.task('log', ` WCAG: ${wcagRefs}`)
71-
cy.task('log', ` Help: ${rule.helpUrl}`)
80+
81+
items.forEach((rule) => {
82+
const wcagRefs =
83+
(rule.tags || [])
84+
.filter((tag) => /^wcag/i.test(tag))
85+
.map((tag) => formatWCAGTag(tag))
86+
.join(', ') || 'no WCAG reference'
87+
88+
cy.task('log', `→ Rule: ${rule.id}`)
89+
cy.task(
90+
'log',
91+
` Description: ${rule.help}`
92+
)
93+
cy.task(
94+
'log',
95+
` Impact: ${rule.impact || 'n/a'}`
96+
)
97+
cy.task('log', ` WCAG: ${wcagRefs}`)
98+
cy.task('log', ` Help: ${rule.helpUrl}`)
99+
})
72100
})
73-
})
74101

75-
cy.writeFile(reportPath, results)
102+
cy.writeFile(reportPath, results)
103+
})
76104
})
77-
})
105+
})
78106
})
79107
})
80108
})

0 commit comments

Comments
 (0)