-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(webdriverio): fix types between v8 and <v8 #962
Changes from all commits
f324c52
9321dad
d1ba1d9
6905997
c1b4556
c08ae90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,20 @@ import { | |
import { getFilename } from 'cross-dirname'; | ||
import { pathToFileURL } from 'url'; | ||
|
||
import type { Browser, Element } from 'webdriverio'; | ||
import type { | ||
RunOptions, | ||
AxeResults, | ||
SerialContextObject, | ||
SerialSelectorList, | ||
SerialFrameSelector | ||
} from 'axe-core'; | ||
import type { Options, CallbackFunction, PartialResults } from './types'; | ||
import type { | ||
Options, | ||
CallbackFunction, | ||
PartialResults, | ||
WdioBrowser, | ||
WdioElement | ||
} from './types'; | ||
|
||
let axeCorePath = ''; | ||
async function loadAxePath() { | ||
|
@@ -43,7 +48,7 @@ async function loadAxePath() { | |
loadAxePath(); | ||
|
||
export default class AxeBuilder { | ||
private client: Browser; | ||
private client: WdioBrowser; | ||
private axeSource: string; | ||
private includes: SerialSelectorList = []; | ||
private excludes: SerialSelectorList = []; | ||
|
@@ -199,7 +204,9 @@ export default class AxeBuilder { | |
/** | ||
* Injects `axe-core` into all frames. | ||
*/ | ||
private async inject(browsingContext: Element | null = null): Promise<void> { | ||
private async inject( | ||
browsingContext: WdioElement | null = null | ||
): Promise<void> { | ||
await this.setBrowsingContext(browsingContext); | ||
const runPartialSupported = await axeSourceInject( | ||
this.client, | ||
|
@@ -246,15 +253,15 @@ export default class AxeBuilder { | |
// ensure we fail quickly if an iframe cannot be loaded (instead of waiting | ||
// the default length of 30 seconds) | ||
const { pageLoad } = await this.client.getTimeouts(); | ||
this.client.setTimeout({ | ||
(this.client as WebdriverIO.Browser).setTimeout({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes a problem with older versions of webdriverio as in newer versions they declare the function with the first parameter as this: Webdriverio.Browser. |
||
pageLoad: FRAME_LOAD_TIMEOUT | ||
}); | ||
|
||
let partials: PartialResults | null; | ||
try { | ||
partials = await this.runPartialRecursive(context); | ||
} finally { | ||
this.client.setTimeout({ | ||
(this.client as WebdriverIO.Browser).setTimeout({ | ||
pageLoad | ||
}); | ||
} | ||
|
@@ -301,7 +308,7 @@ export default class AxeBuilder { | |
* - https://webdriver.io/docs/api/webdriver.html#switchtoframe | ||
*/ | ||
private async setBrowsingContext( | ||
id: null | Element | Browser = null | ||
id: null | WdioElement | WdioBrowser = null | ||
): Promise<void> { | ||
if (id) { | ||
await this.client.switchToFrame(id); | ||
|
@@ -317,7 +324,7 @@ export default class AxeBuilder { | |
|
||
private async runPartialRecursive( | ||
context: SerialContextObject, | ||
frameStack: Element[] = [] | ||
frameStack: WdioElement[] = [] | ||
): Promise<PartialResults> { | ||
const frameContexts = await axeGetFrameContext(this.client, context); | ||
const partials: PartialResults = [ | ||
|
@@ -362,7 +369,7 @@ export default class AxeBuilder { | |
|
||
try { | ||
await client.switchToWindow(newWindow.handle); | ||
await client.url('about:blank'); | ||
await (client as WebdriverIO.Browser).url('about:blank'); | ||
} catch (error) { | ||
throw new Error( | ||
`switchToWindow failed. Are you using updated browser drivers? \nDriver reported:\n${ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import assert from 'assert'; | ||
import type { Browser } from 'webdriverio'; | ||
import type { | ||
AxeResults, | ||
PartialResult, | ||
|
@@ -9,13 +8,14 @@ import type { | |
SerialSelectorList, | ||
SerialContextObject | ||
} from 'axe-core'; | ||
import type { WdioBrowser } from './types'; | ||
|
||
export const FRAME_LOAD_TIMEOUT = 1000; | ||
|
||
/** | ||
* Validates that the client provided is WebdriverIO v5+. | ||
*/ | ||
export const isWebdriverClient = (client: Browser): boolean => { | ||
export const isWebdriverClient = (client: WdioBrowser): boolean => { | ||
if (!client) { | ||
return false; | ||
} | ||
|
@@ -82,14 +82,14 @@ const promisify = <T>(thenable: Promise<T>): Promise<T> => { | |
}; | ||
|
||
export const axeSourceInject = async ( | ||
client: Browser, | ||
client: WdioBrowser, | ||
axeSource: string | ||
): Promise<{ runPartialSupported: boolean }> => { | ||
await assertFrameReady(client); | ||
return promisify( | ||
// Had to use executeAsync() because we could not use multiline statements in client.execute() | ||
// we were able to return a single boolean in a line but not when assigned to a variable. | ||
client.executeAsync(` | ||
(client as WebdriverIO.Browser).executeAsync(` | ||
var callback = arguments[arguments.length - 1]; | ||
${axeSource}; | ||
window.axe.configure({ | ||
|
@@ -101,7 +101,7 @@ export const axeSourceInject = async ( | |
); | ||
}; | ||
|
||
async function assertFrameReady(client: Browser): Promise<void> { | ||
async function assertFrameReady(client: WdioBrowser): Promise<void> { | ||
// Wait so that we know there is an execution context. | ||
// Assume that if we have an html node we have an execution context. | ||
try { | ||
|
@@ -119,7 +119,7 @@ async function assertFrameReady(client: Browser): Promise<void> { | |
reject(); | ||
}, FRAME_LOAD_TIMEOUT); | ||
}); | ||
const executePromise = client.execute(() => { | ||
const executePromise = (client as WebdriverIO.Browser).execute(() => { | ||
return document.readyState === 'complete'; | ||
}); | ||
const readyState = await Promise.race([timeoutPromise, executePromise]); | ||
|
@@ -130,13 +130,13 @@ async function assertFrameReady(client: Browser): Promise<void> { | |
} | ||
|
||
export const axeRunPartial = ( | ||
client: Browser, | ||
client: WdioBrowser, | ||
context?: SerialContextObject, | ||
options?: RunOptions | ||
): Promise<PartialResult> => { | ||
return promisify( | ||
client | ||
.executeAsync<string, []>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Older versions of webdriverio don't declare the type of the return so we can't declare it either. This defaults to |
||
(client as WebdriverIO.Browser) | ||
.executeAsync( | ||
` | ||
var callback = arguments[arguments.length - 1]; | ||
var context = ${JSON.stringify(context)} || document; | ||
|
@@ -145,20 +145,20 @@ export const axeRunPartial = ( | |
callback(JSON.stringify(partials)) | ||
});` | ||
) | ||
.then((r: string) => deserialize<PartialResult>(r)) | ||
.then(r => deserialize<PartialResult>(r as string)) | ||
); | ||
}; | ||
|
||
export const axeGetFrameContext = ( | ||
client: Browser, | ||
client: WdioBrowser, | ||
context: SerialContextObject | ||
// TODO: add proper types | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): Promise<any[]> => { | ||
return promisify( | ||
// Had to use executeAsync() because we could not use multiline statements in client.execute() | ||
// we were able to return a single boolean in a line but not when assigned to a variable. | ||
client.executeAsync(` | ||
(client as WebdriverIO.Browser).executeAsync(` | ||
var callback = arguments[arguments.length - 1]; | ||
var context = ${JSON.stringify(context)}; | ||
var frameContexts = window.axe.utils.getFrameContexts(context); | ||
|
@@ -168,14 +168,14 @@ export const axeGetFrameContext = ( | |
}; | ||
|
||
export const axeRunLegacy = ( | ||
client: Browser, | ||
client: WdioBrowser, | ||
context: SerialContextObject, | ||
options: RunOptions, | ||
config?: Spec | ||
): Promise<AxeResults> => { | ||
return promisify( | ||
client | ||
.executeAsync<string, []>( | ||
(client as WebdriverIO.Browser) | ||
.executeAsync( | ||
`var callback = arguments[arguments.length - 1]; | ||
var context = ${JSON.stringify(context)} || document; | ||
var options = ${JSON.stringify(options)} || {}; | ||
|
@@ -187,12 +187,12 @@ export const axeRunLegacy = ( | |
callback(JSON.stringify(axeResults)) | ||
});` | ||
) | ||
.then((r: string) => deserialize<AxeResults>(r)) | ||
.then(r => deserialize<AxeResults>(r as string)) | ||
); | ||
}; | ||
|
||
export const axeFinishRun = ( | ||
client: Browser, | ||
client: WdioBrowser, | ||
axeSource: string, | ||
partialResults: PartialResults, | ||
options: RunOptions | ||
|
@@ -207,7 +207,7 @@ export const axeFinishRun = ( | |
function chunkResults(result: string): Promise<void> { | ||
const chunk = JSON.stringify(result.substring(0, sizeLimit)); | ||
return promisify( | ||
client.execute( | ||
(client as WebdriverIO.Browser).execute( | ||
` | ||
window.partialResults ??= ''; | ||
window.partialResults += ${chunk}; | ||
|
@@ -223,7 +223,7 @@ export const axeFinishRun = ( | |
return chunkResults(partialString) | ||
.then(() => { | ||
return promisify( | ||
client.executeAsync<string, []>( | ||
(client as WebdriverIO.Browser).executeAsync( | ||
`var callback = arguments[arguments.length - 1]; | ||
${axeSource}; | ||
window.axe.configure({ | ||
|
@@ -238,12 +238,12 @@ export const axeFinishRun = ( | |
) | ||
); | ||
}) | ||
.then((r: string) => deserialize<AxeResults>(r)); | ||
.then(r => deserialize<AxeResults>(r as string)); | ||
}; | ||
|
||
export const configureAllowedOrigins = (client: Browser): Promise<void> => { | ||
export const configureAllowedOrigins = (client: WdioBrowser): Promise<void> => { | ||
return promisify( | ||
client.execute(` | ||
(client as WebdriverIO.Browser).execute(` | ||
window.axe.configure({ allowedOrigins: ['<unsafe_all_origins>'] }) | ||
`) | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node", "mocha", "@wdio/globals/types"] | ||
}, | ||
"include": ["src"], | ||
"exclude": ["test"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a temporary fix that will be fixed in a separate pr to fix the cli tests with 120 #963.