Skip to content

Commit

Permalink
enable use of custom data-test-id (#706)
Browse files Browse the repository at this point in the history
* use custom data-test

* Update __tests__/core/gatherer.test.ts

* Update __tests__/core/gatherer.test.ts
  • Loading branch information
shahzad31 authored Feb 23, 2023
1 parent cbf4c4e commit 46be5eb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
35 changes: 35 additions & 0 deletions __tests__/core/gatherer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,41 @@ describe('Gatherer', () => {
});
});

describe('Set Test ID Attribute', () => {
it('uses default when not set', async () => {
const driver = await Gatherer.setupDriver({
wsEndpoint,
});
const { page } = driver;
await page.goto(server.TEST_PAGE);
await page.setContent(
'<a target=_blank rel=noopener href="/popup.html" data-testid="username-button">Click me</a>'
);
expect(await page.getByTestId('username-button').isVisible()).toEqual(
true
);
await Gatherer.dispose(driver);
await Gatherer.stop();
});

it('uses custom when provided', async () => {
const driver = await Gatherer.setupDriver({
wsEndpoint,
playwrightOptions: { testIdAttribute: 'data-test-subj' },
});
const { page } = driver;
await page.goto(server.TEST_PAGE);
await page.setContent(
'<a target=_blank rel=noopener href="/popup.html" data-test-subj="username-button">Click me</a>'
);
expect(await page.getByTestId('username-button').isVisible()).toEqual(
true
);
await Gatherer.dispose(driver);
await Gatherer.stop();
});
});

describe('Network emulation', () => {
const networkConditions = {
downloadThroughput: megabitsToBytes(3),
Expand Down
3 changes: 2 additions & 1 deletion src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ export type ProjectSettings = {
space: string;
};

export type PlaywrightOptions = LaunchOptions & BrowserContextOptions;
export type PlaywrightOptions = LaunchOptions &
BrowserContextOptions & { testIdAttribute?: string };
export type SyntheticsConfig = {
params?: Params;
playwrightOptions?: PlaywrightOptions;
Expand Down
5 changes: 5 additions & 0 deletions src/core/gatherer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ChromiumBrowser,
BrowserContext,
request as apiRequest,
selectors,
} from 'playwright-chromium';
import { PluginManager } from '../plugins';
import { log } from './logger';
Expand Down Expand Up @@ -64,6 +65,10 @@ export class Gatherer {
});
Gatherer.setNetworkConditions(context, networkConditions);

if (playwrightOptions?.testIdAttribute) {
selectors.setTestIdAttribute(playwrightOptions.testIdAttribute);
}

const page = await context.newPage();
const client = await context.newCDPSession(page);
const request = await apiRequest.newContext({ ...playwrightOptions });
Expand Down

0 comments on commit 46be5eb

Please sign in to comment.