11import assert from 'assert' ;
2- import type { Browser } from 'webdriverio' ;
32import type {
43 AxeResults ,
54 PartialResult ,
@@ -9,13 +8,14 @@ import type {
98 SerialSelectorList ,
109 SerialContextObject
1110} from 'axe-core' ;
11+ import type { WdioBrowser } from './types' ;
1212
1313export const FRAME_LOAD_TIMEOUT = 1000 ;
1414
1515/**
1616 * Validates that the client provided is WebdriverIO v5+.
1717 */
18- export const isWebdriverClient = ( client : Browser ) : boolean => {
18+ export const isWebdriverClient = ( client : WdioBrowser ) : boolean => {
1919 if ( ! client ) {
2020 return false ;
2121 }
@@ -82,14 +82,14 @@ const promisify = <T>(thenable: Promise<T>): Promise<T> => {
8282} ;
8383
8484export const axeSourceInject = async (
85- client : Browser ,
85+ client : WdioBrowser ,
8686 axeSource : string
8787) : Promise < { runPartialSupported : boolean } > => {
8888 await assertFrameReady ( client ) ;
8989 return promisify (
9090 // Had to use executeAsync() because we could not use multiline statements in client.execute()
9191 // we were able to return a single boolean in a line but not when assigned to a variable.
92- client . executeAsync ( `
92+ ( client as WebdriverIO . Browser ) . executeAsync ( `
9393 var callback = arguments[arguments.length - 1];
9494 ${ axeSource } ;
9595 window.axe.configure({
@@ -101,7 +101,7 @@ export const axeSourceInject = async (
101101 ) ;
102102} ;
103103
104- async function assertFrameReady ( client : Browser ) : Promise < void > {
104+ async function assertFrameReady ( client : WdioBrowser ) : Promise < void > {
105105 // Wait so that we know there is an execution context.
106106 // Assume that if we have an html node we have an execution context.
107107 try {
@@ -119,7 +119,7 @@ async function assertFrameReady(client: Browser): Promise<void> {
119119 reject ( ) ;
120120 } , FRAME_LOAD_TIMEOUT ) ;
121121 } ) ;
122- const executePromise = client . execute ( ( ) => {
122+ const executePromise = ( client as WebdriverIO . Browser ) . execute ( ( ) => {
123123 return document . readyState === 'complete' ;
124124 } ) ;
125125 const readyState = await Promise . race ( [ timeoutPromise , executePromise ] ) ;
@@ -130,13 +130,13 @@ async function assertFrameReady(client: Browser): Promise<void> {
130130}
131131
132132export const axeRunPartial = (
133- client : Browser ,
133+ client : WdioBrowser ,
134134 context ?: SerialContextObject ,
135135 options ?: RunOptions
136136) : Promise < PartialResult > => {
137137 return promisify (
138- client
139- . executeAsync < string , [ ] > (
138+ ( client as WebdriverIO . Browser )
139+ . executeAsync (
140140 `
141141 var callback = arguments[arguments.length - 1];
142142 var context = ${ JSON . stringify ( context ) } || document;
@@ -145,20 +145,20 @@ export const axeRunPartial = (
145145 callback(JSON.stringify(partials))
146146 });`
147147 )
148- . then ( ( r : string ) => deserialize < PartialResult > ( r ) )
148+ . then ( r => deserialize < PartialResult > ( r as string ) )
149149 ) ;
150150} ;
151151
152152export const axeGetFrameContext = (
153- client : Browser ,
153+ client : WdioBrowser ,
154154 context : SerialContextObject
155155 // TODO: add proper types
156156 // eslint-disable-next-line @typescript-eslint/no-explicit-any
157157) : Promise < any [ ] > => {
158158 return promisify (
159159 // Had to use executeAsync() because we could not use multiline statements in client.execute()
160160 // we were able to return a single boolean in a line but not when assigned to a variable.
161- client . executeAsync ( `
161+ ( client as WebdriverIO . Browser ) . executeAsync ( `
162162 var callback = arguments[arguments.length - 1];
163163 var context = ${ JSON . stringify ( context ) } ;
164164 var frameContexts = window.axe.utils.getFrameContexts(context);
@@ -168,14 +168,14 @@ export const axeGetFrameContext = (
168168} ;
169169
170170export const axeRunLegacy = (
171- client : Browser ,
171+ client : WdioBrowser ,
172172 context : SerialContextObject ,
173173 options : RunOptions ,
174174 config ?: Spec
175175) : Promise < AxeResults > => {
176176 return promisify (
177- client
178- . executeAsync < string , [ ] > (
177+ ( client as WebdriverIO . Browser )
178+ . executeAsync (
179179 `var callback = arguments[arguments.length - 1];
180180 var context = ${ JSON . stringify ( context ) } || document;
181181 var options = ${ JSON . stringify ( options ) } || {};
@@ -187,12 +187,12 @@ export const axeRunLegacy = (
187187 callback(JSON.stringify(axeResults))
188188 });`
189189 )
190- . then ( ( r : string ) => deserialize < AxeResults > ( r ) )
190+ . then ( r => deserialize < AxeResults > ( r as string ) )
191191 ) ;
192192} ;
193193
194194export const axeFinishRun = (
195- client : Browser ,
195+ client : WdioBrowser ,
196196 axeSource : string ,
197197 partialResults : PartialResults ,
198198 options : RunOptions
@@ -207,7 +207,7 @@ export const axeFinishRun = (
207207 function chunkResults ( result : string ) : Promise < void > {
208208 const chunk = JSON . stringify ( result . substring ( 0 , sizeLimit ) ) ;
209209 return promisify (
210- client . execute (
210+ ( client as WebdriverIO . Browser ) . execute (
211211 `
212212 window.partialResults ??= '';
213213 window.partialResults += ${ chunk } ;
@@ -223,7 +223,7 @@ export const axeFinishRun = (
223223 return chunkResults ( partialString )
224224 . then ( ( ) => {
225225 return promisify (
226- client . executeAsync < string , [ ] > (
226+ ( client as WebdriverIO . Browser ) . executeAsync (
227227 `var callback = arguments[arguments.length - 1];
228228 ${ axeSource } ;
229229 window.axe.configure({
@@ -238,12 +238,12 @@ export const axeFinishRun = (
238238 )
239239 ) ;
240240 } )
241- . then ( ( r : string ) => deserialize < AxeResults > ( r ) ) ;
241+ . then ( r => deserialize < AxeResults > ( r as string ) ) ;
242242} ;
243243
244- export const configureAllowedOrigins = ( client : Browser ) : Promise < void > => {
244+ export const configureAllowedOrigins = ( client : WdioBrowser ) : Promise < void > => {
245245 return promisify (
246- client . execute ( `
246+ ( client as WebdriverIO . Browser ) . execute ( `
247247 window.axe.configure({ allowedOrigins: ['<unsafe_all_origins>'] })
248248 ` )
249249 ) ;
0 commit comments