Skip to content

Commit 658dc52

Browse files
committed
feat: add support of @testing-library/dom queries
1 parent 246b649 commit 658dc52

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

src/browser/browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { AsyncEmitter } from "../events";
1313
import { BrowserConfig } from "../config/browser-config";
1414
import type { Callstack } from "./history/callstack";
1515
import type { WdProcess, WebdriverPool } from "../browser-pool/webdriver-pool";
16-
import { setupBrowser } from "./queries";
16+
import { configure, setupBrowser } from "./queries";
1717

1818
const CUSTOM_SESSION_OPTS = [
1919
"outputDir",
@@ -104,6 +104,7 @@ export class Browser {
104104
}
105105

106106
protected _addQueries(): void {
107+
configure({ testIdAttribute: this._config.testIdAttribute, asyncUtilTimeout: this._config.waitTimeout });
107108
setupBrowser(this._session!);
108109
}
109110

src/browser/queries/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,35 @@ function findContainerWithExecute(container: ElementBase): WebdriverIO.Browser {
6161
}
6262

6363
async function injectDOMTestingLibrary(container: ElementBase) {
64-
const containerWithExecute = findContainerWithExecute(container);
65-
const shouldInjectDTL = await containerWithExecute.execute(function () {
64+
const browser = findContainerWithExecute(container);
65+
const shouldInjectDTL = await browser.execute(function () {
6666
return !window.TestingLibraryDom;
6767
});
6868

6969
if (shouldInjectDTL) {
70-
await containerWithExecute.execute(function (library) {
71-
// add DOM Testing Library to page as a script tag to support Firefox
70+
await browser.execute(function (library) {
7271
if (navigator.userAgent.indexOf("Firefox") !== -1) {
73-
const script = window.document.createElement("script");
74-
script.textContent = library;
75-
window.document.head.append(script);
76-
window.eval(library);
72+
try {
73+
// Inject via inline-script
74+
const script = window.document.createElement("script");
75+
script.textContent = library;
76+
window.document.head.append(script);
77+
if (!window.TestingLibraryDom) {
78+
// Inject via eval
79+
window.eval(library);
80+
}
81+
} catch (error) {
82+
throw new Error(
83+
`The DOM Testing Library cannot be injected on certain domains, particularly "${window.location.host}", due to restrictions imposed by the Content-Security-Policy (CSP) header.`,
84+
);
85+
}
7786
} else {
7887
eval(library);
7988
}
8089
}, DOM_TESTING_LIBRARY_UMD);
8190
}
8291

83-
await containerWithExecute.execute(function (config: Partial<Config>) {
92+
await browser.execute(function (config: Partial<Config>) {
8493
window.TestingLibraryDom.configure(config);
8594
}, _config);
8695
}
@@ -109,7 +118,6 @@ function serializeArg(arg: QueryArg): SerializedArg {
109118
type SerializedQueryResult = { selector: string }[] | string | { selector: string } | null;
110119

111120
async function executeQuery(query: QueryName, container: HTMLElement, ...args: SerializedArg[]) {
112-
// const done = args.pop() as unknown as (result: SerializedQueryResult) => void;
113121
return new Promise((done: (result: SerializedQueryResult) => void) => {
114122
function deserializeObject(object: SerializedObject) {
115123
return Object.entries(object)
@@ -154,7 +162,7 @@ async function executeQuery(query: QueryName, container: HTMLElement, ...args: S
154162
}
155163

156164
function makeSelectorResult(element: HTMLElement): { selector: string } {
157-
const elementIdAttributeName = "data-wdio-testing-lib-element-id";
165+
const elementIdAttributeName = "data-testplane-element-id";
158166
let elementId = element.getAttribute(elementIdAttributeName);
159167

160168
// if id doesn't already exist create one and add it to element

src/config/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ module.exports = {
118118
},
119119
passive: false,
120120
timeTravel: TimeTravelMode.Off,
121+
testIdAttribute: "data-testid",
121122
};
122123

123124
module.exports.configPaths = [

src/config/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ export interface CommonConfig {
277277
sessionsPerBrowser: number;
278278
testsPerSession: number;
279279
retry: number;
280+
280281
shouldRetry(testInfo: { ctx: Test; retriesLeft: number }): boolean | null;
282+
281283
httpTimeout: number;
282284
urlHttpTimeout: number | null;
283285
pageLoadTimeout: number | null;
@@ -293,9 +295,13 @@ export interface CommonConfig {
293295
};
294296
takeScreenshotOnFailsTimeout: number | null;
295297
takeScreenshotOnFailsMode: "fullpage" | "viewport";
298+
296299
prepareBrowser(browser: WebdriverIO.Browser): void | null;
300+
297301
screenshotPath: string | null;
302+
298303
screenshotsDir(test: Test): string;
304+
299305
calibrate: boolean;
300306
compositeImage: boolean;
301307
strictTestsOrder: boolean;
@@ -350,6 +356,8 @@ export interface CommonConfig {
350356
};
351357

352358
timeTravel: TimeTravelConfig;
359+
360+
testIdAttribute?: string;
353361
}
354362

355363
export interface SetsConfig {
@@ -386,6 +394,7 @@ export interface ConfigParsed extends CommonConfig {
386394

387395
export interface RuntimeConfig {
388396
extend: (data: unknown) => this;
397+
389398
[key: string]: unknown;
390399
}
391400

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export { Key } from "@testplane/webdriverio";
1414
export * from "./mock";
1515

1616
export * as unstable from "./unstable";
17-
export * as queries from "./browser/queries";
1817

1918
export type {
2019
WdioBrowser,

0 commit comments

Comments
 (0)