|
1 | 1 | import { test, expect } from '@wordpress/e2e-test-utils-playwright'; |
2 | 2 | const AxeBuilder = require( '@axe-core/playwright' ).default; |
3 | 3 |
|
4 | | -test.describe( 'PHP Page Accessibility Tests', () => { |
5 | | - test( 'should not have any automatically detectable accessibility violations', async ( { admin, page, requestUtils } ) => { |
6 | | - |
7 | | - // await requestUtils.login(); |
8 | | - // await page.goto( 'http://localhost:8889/wp-admin/edit.php'); |
9 | | - await admin.visitAdminPage( '/edit.php' ); |
10 | | - |
11 | | - // 2. Run the axe scan on the rendered page |
12 | | - const scanResults = await new AxeBuilder({ page }).analyze(); |
13 | | - |
14 | | - // 3. Format and print violations individually if any are found |
15 | | - if (scanResults.violations.length > 0) { |
16 | | - console.log(`\n❌ Found ${scanResults.violations.length} accessibility violation(s):\n`); |
17 | | - |
18 | | - scanResults.violations.forEach((violation, index) => { |
19 | | - console.log(`--- Violation #${index + 1} ---`); |
20 | | - console.log(`Rule ID: ${violation.id}`); |
21 | | - console.log(`Impact: ${violation.impact.toUpperCase()}`); |
22 | | - console.log(`Failure: ${violation.description}`); |
23 | | - console.log(`Help Link: ${violation.helpUrl}`); |
24 | | - |
25 | | - // List every specific HTML element failing this rule |
26 | | - console.log('Failing Elements:'); |
27 | | - violation.nodes.forEach((node) => { |
28 | | - console.log(` - Target Selector: ${node.target.join(', ')}`); |
29 | | - console.log(` HTML Snippet: ${node.html}`); |
30 | | - }); |
31 | | - console.log('\n'); |
32 | | - }); |
| 4 | +test.describe( 'Page Accessibility Tests', () => { |
| 5 | + test( 'should not have any automatically detectable accessibility violations', async ( { admin, page } ) => { |
| 6 | + |
| 7 | + // The page to be scanned. |
| 8 | + await admin.visitAdminPage( '/upload.php?mode=grid' ); |
| 9 | + |
| 10 | + const scanResults = await new AxeBuilder( { page } ) |
| 11 | + .options( |
| 12 | + { |
| 13 | + runOnly: [ 'wcag2a', 'wcag2aa' ], |
| 14 | + rules: { |
| 15 | + // This is only to test how to disable a rule. |
| 16 | + 'aria-allowed-role': { enabled: false }, |
| 17 | + } |
| 18 | + } |
| 19 | + ) |
| 20 | + .analyze(); |
| 21 | + |
| 22 | + const violationsAmount = scanResults.violations.length; |
| 23 | + |
| 24 | + if ( violationsAmount > 0 ) { |
| 25 | + console.log(`\nFound ${ violationsAmount } accessibility violation(s):\n`); |
| 26 | + |
| 27 | + /* |
| 28 | + * Result Object documentation: https://github.com/dequelabs/axe-core/blob/master/doc/API.md#results-object |
| 29 | + * This object has four components: |
| 30 | + * - a `passes` array: keeps track of all the passed tests, |
| 31 | + * along with detailed information on each one. |
| 32 | + * - a `violations` array: keeps track of all the failed tests, |
| 33 | + * along with detailed information on each one. |
| 34 | + * - an `incomplete` array: indicates which nodes could neither be |
| 35 | + * determined to definitively pass or definitively |
| 36 | + * fail. They are separated out in order that |
| 37 | + * a user interface can display these to the |
| 38 | + * user for manual review |
| 39 | + * - an `inapplicable` array: lists all the rules for which no matching |
| 40 | + * elements were found on the page. |
| 41 | + */ |
| 42 | + scanResults.violations.forEach( ( violation, index ) => { |
| 43 | + console.log( `--- Violation #${ index + 1 } ---` ); |
| 44 | + console.log( `Rule ID: ${ violation.id }` ); |
| 45 | + console.log( `Impact: ${ violation.impact.toUpperCase() }` ); |
| 46 | + console.log( `Failure: ${ violation.description }` ); |
| 47 | + console.log( `Help: ${ violation.help }` ); |
| 48 | + console.log( `Help link: ${ violation.helpUrl }` ); |
| 49 | + |
| 50 | + // List every specific HTML element failing a failing rule. |
| 51 | + console.log( 'Failing elements:' ); |
| 52 | + violation.nodes.forEach( ( node ) => { |
| 53 | + console.log( ` - Target Selector: ${ node.target.join( ', ' ) }` ); |
| 54 | + console.log( ` - HTML Snippet:\n${ node.html }` ); |
| 55 | + } ); |
| 56 | + console.log('\n'); |
| 57 | + } ); |
33 | 58 | } |
34 | 59 |
|
35 | | - // 3. Assert that the violations list is empty |
36 | | - expect(scanResults.violations).toEqual([]); |
37 | | - }); |
38 | | -}); |
| 60 | + // Assert that there are no violations. |
| 61 | + expect( violationsAmount ).toEqual( 0 ); |
| 62 | + } ); |
| 63 | +} ); |
0 commit comments