99
1010import type { Page } from '@playwright/test' ;
1111import AxeBuilder from '@axe-core/playwright' ;
12- import { AXE_OPTIONS } from '@kbn/axe-config' ;
12+ import { AXE_OPTIONS , AXE_IMPACT_LEVELS } from '@kbn/axe-config' ;
1313
1414import type { KibanaUrl } from '../../..' ;
1515
@@ -30,8 +30,6 @@ export interface A11yViolation {
3030export interface RunA11yScanOptions {
3131 /** Optional CSS selectors to exclude from scan */
3232 exclude ?: string [ ] ;
33- /** Optional result impact levels to include (e.g. \['critical','serious'\]) */
34- impactLevels ?: Array < 'minor' | 'moderate' | 'serious' | 'critical' > ;
3533 /** Timeout in ms for the scan (defaults 10000) */
3634 timeoutMs ?: number ;
3735}
@@ -42,13 +40,15 @@ export interface RunA11yScanResult {
4240
4341export const runA11yScan = async (
4442 page : Page ,
45- { exclude = [ ] , impactLevels , timeoutMs = 10000 } : RunA11yScanOptions = { }
43+ { exclude = [ ] , timeoutMs = 10000 } : RunA11yScanOptions = { }
4644) : Promise < RunA11yScanResult > => {
4745 const builder = new AxeBuilder ( { page } ) ;
4846 builder . options ( AXE_OPTIONS ) ;
4947
50- for ( const selector of exclude ) {
51- builder . exclude ( selector ) ;
48+ if ( exclude ) {
49+ for ( const selector of exclude ) {
50+ builder . exclude ( selector ) ;
51+ }
5252 }
5353
5454 const analysisPromise = builder . analyze ( ) ;
@@ -70,10 +70,10 @@ export const runA11yScan = async (
7070
7171 let violations = ( result . violations as A11yViolation [ ] ) || [ ] ;
7272
73- if ( impactLevels && impactLevels . length ) {
74- const allowed = new Set ( impactLevels ) ;
73+ if ( AXE_IMPACT_LEVELS ? .length ) {
74+ const allowed = new Set ( AXE_IMPACT_LEVELS ) ;
7575 violations = violations . filter (
76- ( v ) => v . impact && allowed . has ( v . impact as ( typeof impactLevels ) [ number ] )
76+ ( v ) => v . impact && allowed . has ( v . impact as ( typeof AXE_IMPACT_LEVELS ) [ number ] )
7777 ) ;
7878 }
7979
@@ -83,13 +83,19 @@ export const runA11yScan = async (
8383/**
8484 * Assert helper usable inside tests.
8585 */
86- export const checkA11y = async ( page : Page , kbnUrl ?: KibanaUrl ) => {
87- const { violations } = await runA11yScan ( page ) ;
86+ export const checkA11y = async ( page : Page , kbnUrl ?: KibanaUrl , options ?: RunA11yScanOptions ) => {
87+ const { violations } = await runA11yScan ( page , options ) ;
8888
8989 return {
90- violations : violations . map ( ( v ) => ( {
91- ...v ,
92- url : kbnUrl ?. toString ( ) ,
93- } ) ) ,
90+ violations : violations . map (
91+ ( v ) =>
92+ `Accessibility violation: ${ v . id } (${ v . impact } ): \n
93+ ${ v . helpUrl } \n
94+ ${ v . description } \n
95+ ${ kbnUrl } \n
96+ Nodes:\n${ v . nodes
97+ . map ( ( n ) => ` ${ n . target . join ( ', ' ) } -> ${ n . failureSummary } ` )
98+ . join ( '\n' ) } `
99+ ) ,
94100 } ;
95101} ;
0 commit comments