import * as axe from 'axe-core' ; import { defaultRuleset } from '@sa11y/preset-rules' ; import { A11yError , exceptionListFilter } from '@sa11y/format' ; import { A11yConfig , AxeResults , axeVersion , getViolations , WdioOptions , WdioBrowser } from '@sa11y/common' ; /**
 * Merge given options with default options
 */ function setDefaultOptions ( opts = { } ) { const defaultOptions = { driver : global . browser , scope : undefined , rules : defaultRuleset , exceptionList : { } , } ; return Object . assign ( Object . assign ( { } , defaultOptions ) , opts ) ; } /**
 * Return version of axe injected into browser
 */ export async function getAxeVersion ( driver ) { return driver . execute ( ( ) => { return typeof axe === 'object' ? axe . version : undefined ; } ) ; } /**
 * Load axe source into browser if it is not already loaded and return version of axe.
 * Since axe min js is large (400+kb), keep polling until given timeout in milliseconds.
 */ export async function loadAxe ( driver , timeout = 10000 , pollTime = 100 ) { await driver . execute ( axe . source ) ; await driver . waitUntil ( async ( ) => ( await getAxeVersion ( driver ) ) === axeVersion , { timeout : timeout , interval : pollTime , timeoutMsg : `Unable to load axe after ${ timeout } ms` , } ) ; } /**
 * Load and run axe in given WDIO instance and return the accessibility violations found.
 */ export async function runAxe ( options = { } ) { const { driver , scope , rules } = setDefaultOptions ( options ) ; const elemSelector = scope ? ( await scope ) . selector : undefined ; await loadAxe ( driver ) ; return ( await driver . executeAsync ( ( elemSelector , rules , done ) => { axe . run ( ( elemSelector || document ) as axe . ElementContext , rules as axe . RunOptions , ( err , results ) => { if ( err ) throw err ; done ( results . violations ) ; } ) ; } , elemSelector , rules ) ) as AxeResults ; } /**
 * Verify that the currently loaded page in the browser is accessible.
 * Throw an error with the accessibility issues found if it is not accessible.
 * Asynchronous version of  assertAccessibleSync export async function assertAccessible ( opts = { } ) { const options = setDefaultOptions ( opts ) ; const violations = await getViolations ( ( ) => runAxe ( options ) ) ; const filteredResults = exceptionListFilter ( violations , options . exceptionList ) ; A11yError . checkAndThrow ( filteredResults ) ; } /**
 * Verify that the currently loaded page in the browser is accessible.
 * Throw an error with the accessibility issues found if it is not accessible.
 * Synchronous version of  assertAccessible export function assertAccessibleSync ( opts = { } ) { const options = setDefaultOptions ( opts ) ; void options . driver . call ( async ( ) => { await assertAccessible ( options ) ; } ) ; }