99import chalk from 'chalk' ;
1010import testSubjectToCss from '@kbn/test-subj-selector' ;
1111
12- import { FtrProviderContext } from '../../ftr_provider_context' ;
12+ import { FtrService } from '../../ftr_provider_context' ;
1313import { AxeReport , printResult } from './axe_report' ;
1414// @ts -ignore JS that is run in browser as is
1515import { analyzeWithAxe , analyzeWithAxeWithClient } from './analyze_with_axe' ;
@@ -33,86 +33,84 @@ export const normalizeResult = (report: any) => {
3333 return report . result as false | AxeReport ;
3434} ;
3535
36- export function A11yProvider ( { getService } : FtrProviderContext ) {
37- const browser = getService ( 'browser' ) ;
38- const Wd = getService ( '__webdriver__' ) ;
39-
40- /**
41- * Accessibility testing service using the Axe (https://www.deque.com/axe/)
42- * toolset to validate a11y rules similar to ESLint. In order to test against
43- * the rules we must load up the UI and feed a full HTML snapshot into Axe.
44- */
45- return new ( class Accessibility {
46- public async testAppSnapshot ( options : TestOptions = { } ) {
47- const context = this . getAxeContext ( true , options . excludeTestSubj ) ;
48- const report = await this . captureAxeReport ( context ) ;
49- await this . testAxeReport ( report ) ;
50- }
36+ /**
37+ * Accessibility testing service using the Axe (https://www.deque.com/axe/)
38+ * toolset to validate a11y rules similar to ESLint. In order to test against
39+ * the rules we must load up the UI and feed a full HTML snapshot into Axe.
40+ */
41+ export class AccessibilityService extends FtrService {
42+ private readonly browser = this . ctx . getService ( 'browser' ) ;
43+ private readonly Wd = this . ctx . getService ( '__webdriver__' ) ;
44+
45+ public async testAppSnapshot ( options : TestOptions = { } ) {
46+ const context = this . getAxeContext ( true , options . excludeTestSubj ) ;
47+ const report = await this . captureAxeReport ( context ) ;
48+ this . assertValidAxeReport ( report ) ;
49+ }
5150
52- public async testGlobalSnapshot ( options : TestOptions = { } ) {
53- const context = this . getAxeContext ( false , options . excludeTestSubj ) ;
54- const report = await this . captureAxeReport ( context ) ;
55- await this . testAxeReport ( report ) ;
56- }
51+ public async testGlobalSnapshot ( options : TestOptions = { } ) {
52+ const context = this . getAxeContext ( false , options . excludeTestSubj ) ;
53+ const report = await this . captureAxeReport ( context ) ;
54+ this . assertValidAxeReport ( report ) ;
55+ }
5756
58- private getAxeContext ( global : boolean , excludeTestSubj ?: string | string [ ] ) : AxeContext {
59- return {
60- include : global ? undefined : [ testSubjectToCss ( 'appA11yRoot' ) ] ,
61- exclude : ( [ ] as string [ ] )
62- . concat ( excludeTestSubj || [ ] )
63- . map ( ( ts ) => [ testSubjectToCss ( ts ) ] )
64- . concat ( [ [ '[role="graphics-document"][aria-roledescription="visualization"]' ] ] ) ,
65- } ;
66- }
57+ private getAxeContext ( global : boolean , excludeTestSubj ?: string | string [ ] ) : AxeContext {
58+ return {
59+ include : global ? undefined : [ testSubjectToCss ( 'appA11yRoot' ) ] ,
60+ exclude : ( [ ] as string [ ] )
61+ . concat ( excludeTestSubj || [ ] )
62+ . map ( ( ts ) => [ testSubjectToCss ( ts ) ] )
63+ . concat ( [ [ '[role="graphics-document"][aria-roledescription="visualization"]' ] ] ) ,
64+ } ;
65+ }
6766
68- private testAxeReport ( report : AxeReport ) {
69- const errorMsgs = [ ] ;
67+ private assertValidAxeReport ( report : AxeReport ) {
68+ const errorMsgs = [ ] ;
7069
71- for ( const result of report . violations ) {
72- errorMsgs . push ( printResult ( chalk . red ( 'VIOLATION' ) , result ) ) ;
73- }
70+ for ( const result of report . violations ) {
71+ errorMsgs . push ( printResult ( chalk . red ( 'VIOLATION' ) , result ) ) ;
72+ }
7473
75- if ( errorMsgs . length ) {
76- throw new Error ( `a11y report:\n${ errorMsgs . join ( '\n' ) } ` ) ;
77- }
74+ if ( errorMsgs . length ) {
75+ throw new Error ( `a11y report:\n${ errorMsgs . join ( '\n' ) } ` ) ;
7876 }
77+ }
7978
80- private async captureAxeReport ( context : AxeContext ) : Promise < AxeReport > {
81- const axeOptions = {
82- reporter : 'v2' ,
83- runOnly : [ 'wcag2a' , 'wcag2aa' ] ,
84- rules : {
85- 'color-contrast' : {
86- enabled : false , // disabled because we have too many failures
87- } ,
88- bypass : {
89- enabled : false , // disabled because it's too flaky
90- } ,
79+ private async captureAxeReport ( context : AxeContext ) : Promise < AxeReport > {
80+ const axeOptions = {
81+ reporter : 'v2' ,
82+ runOnly : [ 'wcag2a' , 'wcag2aa' ] ,
83+ rules : {
84+ 'color-contrast' : {
85+ enabled : false , // disabled because we have too many failures
9186 } ,
92- } ;
93-
94- await ( Wd . driver . manage ( ) as any ) . setTimeouts ( {
95- ...( await ( Wd . driver . manage ( ) as any ) . getTimeouts ( ) ) ,
96- script : 600000 ,
97- } ) ;
87+ bypass : {
88+ enabled : false , // disabled because it's too flaky
89+ } ,
90+ } ,
91+ } ;
9892
99- const report = normalizeResult (
100- await browser . executeAsync ( analyzeWithAxe , context , axeOptions )
101- ) ;
93+ await this . Wd . driver . manage ( ) . setTimeouts ( {
94+ ...( await this . Wd . driver . manage ( ) . getTimeouts ( ) ) ,
95+ script : 600000 ,
96+ } ) ;
10297
103- if ( report !== false ) {
104- return report ;
105- }
98+ const report = normalizeResult (
99+ await this . browser . executeAsync ( analyzeWithAxe , context , axeOptions )
100+ ) ;
106101
107- const withClientReport = normalizeResult (
108- await browser . executeAsync ( analyzeWithAxeWithClient , context , axeOptions )
109- ) ;
102+ if ( report !== false ) {
103+ return report ;
104+ }
110105
111- if ( withClientReport === false ) {
112- throw new Error ( 'Attempted to analyze with axe but failed to load axe client' ) ;
113- }
106+ const withClientReport = normalizeResult (
107+ await this . browser . executeAsync ( analyzeWithAxeWithClient , context , axeOptions )
108+ ) ;
114109
115- return withClientReport ;
110+ if ( withClientReport === false ) {
111+ throw new Error ( 'Attempted to analyze with axe but failed to load axe client' ) ;
116112 }
117- } ) ( ) ;
113+
114+ return withClientReport ;
115+ }
118116}
0 commit comments