Skip to content

Commit 186b449

Browse files
authored
fix: error running with wdio v8 (#47)
Closes issue #46. When using WebdriverIO v8 we encounter `TypeError: container.parent.execute is not a function`. This is due to the execute function being moved off of Elements. Traverse up through the Element's parents until we find one with `execute` and use that to inject DTL and to execute the query. Using the parent to execute does not change the behaviour of `within` since the passed Element is still used as the container for the query.
1 parent 0c1d179 commit 186b449

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/index.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '@testing-library/dom'
1111
import 'simmerjs'
1212

13-
import {BrowserBase, ElementBase} from './wdio-types'
13+
import {BaseWithExecute, BrowserBase, ElementBase} from './wdio-types'
1414
import {
1515
QueryArg,
1616
Config,
@@ -46,16 +46,29 @@ const SIMMERJS = fs
4646

4747
let _config: Partial<Config>
4848

49+
function isContainerWithExecute(container: ElementBase | BaseWithExecute): container is BaseWithExecute {
50+
return (container as { execute?: unknown }).execute != null;
51+
}
52+
53+
function findContainerWithExecute(container: ElementBase): BaseWithExecute {
54+
let curContainer: ElementBase | BaseWithExecute = container.parent;
55+
while (!isContainerWithExecute(curContainer)) {
56+
curContainer = curContainer.parent;
57+
}
58+
return curContainer;
59+
}
60+
4961
async function injectDOMTestingLibrary(container: ElementBase) {
50-
const shouldInject = await container.parent.execute(function () {
62+
const containerWithExecute = findContainerWithExecute(container);
63+
const shouldInject = await containerWithExecute.execute(function () {
5164
return {
5265
domTestingLibrary: !window.TestingLibraryDom,
5366
simmer: !window.Simmer,
5467
}
5568
})
5669

5770
if (shouldInject.domTestingLibrary) {
58-
await container.parent.execute(function (library: string) {
71+
await containerWithExecute.execute(function (library: string) {
5972
// add DOM Testing Library to page as a script tag to support Firefox
6073
if (navigator.userAgent.includes('Firefox')) {
6174
const script = document.createElement('script')
@@ -69,10 +82,10 @@ async function injectDOMTestingLibrary(container: ElementBase) {
6982
}
7083

7184
if (shouldInject.simmer) {
72-
await container.parent.execute(SIMMERJS)
85+
await containerWithExecute.execute(SIMMERJS)
7386
}
7487

75-
await container.parent.execute(function (config: Config) {
88+
await containerWithExecute.execute(function (config: Config) {
7689
window.TestingLibraryDom.configure(config)
7790
}, _config)
7891
}
@@ -187,7 +200,7 @@ function createQuery(container: ElementBase, queryName: QueryName) {
187200
await injectDOMTestingLibrary(container)
188201

189202
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
190-
const result: SerializedQueryResult = await container.parent.executeAsync(
203+
const result: SerializedQueryResult = await findContainerWithExecute(container).executeAsync(
191204
executeQuery,
192205
queryName,
193206
container,

src/wdio-types.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ export type SelectorsBase = {
3737
$$: $$
3838
}
3939

40-
export type ElementBase = SelectorsBase & {
41-
parent: {
42-
execute<T>(
40+
export type BaseWithExecute = {
41+
execute<T>(
4342
script: string | ((...args: any[]) => T),
4443
...args: any[]
45-
): Promise<T>
46-
47-
execute<T>(script: string | ((...args: any[]) => T), ...args: any[]): T
48-
49-
executeAsync(script: string | ((...args: any[]) => void), ...args: any[]): any
50-
}
44+
): Promise<T>
45+
46+
execute<T>(script: string | ((...args: any[]) => T), ...args: any[]): T
47+
48+
executeAsync(script: string | ((...args: any[]) => void), ...args: any[]): any
49+
}
50+
51+
export type ElementBase = SelectorsBase & {
52+
parent: ElementBase | BaseWithExecute
5153
}
5254

5355
export type BrowserBase = SelectorsBase & {

0 commit comments

Comments
 (0)