Skip to content

Commit 7ecc7f7

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

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
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 });
107108
setupBrowser(this._session!);
108109
}
109110

src/browser/queries/index.ts

Lines changed: 18 additions & 9 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
}

src/browser/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { MoveCursorToCommand } from "./commands/moveCursorTo";
77
import { OpenAndWaitCommand } from "./commands/openAndWait";
88
import type { Callstack } from "./history/callstack";
99
import { Test, Hook } from "../test-reader/test-object";
10-
import { TestplaneQueries, TestplaneQueriesChainable, TestplaneQueriesSync } from "./queries";
10+
import { configure, TestplaneQueries, TestplaneQueriesChainable, TestplaneQueriesSync } from "./queries";
1111

1212
export const BrowserName = {
1313
CHROME: PuppeteerBrowser.CHROME,
@@ -154,6 +154,11 @@ declare global {
154154
switchToRepl: (this: WebdriverIO.Browser, ctx?: Record<string, unknown>) => Promise<void>;
155155

156156
clearSession: (this: WebdriverIO.Browser) => Promise<void>;
157+
158+
/**
159+
* Allows to configure DOM Testing Library
160+
*/
161+
configureDTL: typeof configure;
157162
}
158163

159164
interface 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

0 commit comments

Comments
 (0)