-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support configure API with locator fixture via
test.use
### Global ```ts // playwright.config.ts import type { PlaywrightTestConfig } from '@playwright/test'; const config: PlaywrightTestConfig = { use: { testIdAttribute: 'data-custom-test-id', asyncUtilsTimeout: 5000, }, }; export default config; ``` ### Local ```ts import { test as baseTest } from '@playwright/test' import { locatorFixtures as fixtures, LocatorFixtures as TestingLibraryFixtures, within } from '@playwright-testing-library/test/fixture'; const test = baseTest.extend<TestingLibraryFixtures>(fixtures); const {expect} = test; // Entire test suite test.use({ testIdAttribute: 'data-custom-test-id' }); test.describe(() => { // Specific block test.use({ testIdAttribute: 'some-other-test-id', asyncUtilsTimeout: 5000, }); test('my form', async ({queries: {getByTestId}}) => { // ... }); }); ```
- Loading branch information
Showing
11 changed files
with
173 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {promises as fs} from 'fs' | ||
|
||
import {configureTestingLibraryScript} from '../../common' | ||
import {reviver} from '../helpers' | ||
import type {AllQuery, Config, FindQuery, Query, Selector, SupportedQuery} from '../types' | ||
|
||
const isAllQuery = (query: Query): query is AllQuery => query.includes('All') | ||
const isNotFindQuery = (query: Query): query is Exclude<Query, FindQuery> => | ||
!query.startsWith('find') | ||
|
||
const queryToSelector = (query: SupportedQuery) => | ||
query.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() as Selector | ||
|
||
const buildTestingLibraryScript = async ({config}: {config: Config}) => { | ||
const testingLibraryDom = await fs.readFile( | ||
require.resolve('@testing-library/dom/dist/@testing-library/dom.umd.js'), | ||
'utf8', | ||
) | ||
|
||
const configuredTestingLibraryDom = configureTestingLibraryScript(testingLibraryDom, config) | ||
|
||
return ` | ||
${configuredTestingLibraryDom} | ||
window.__testingLibraryReviver = ${reviver.toString()}; | ||
` | ||
} | ||
|
||
export {isAllQuery, isNotFindQuery, queryToSelector, buildTestingLibraryScript} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export { | ||
installTestingLibraryFixture, | ||
options, | ||
queriesFixture, | ||
registerSelectorsFixture, | ||
within, | ||
} from './fixtures' | ||
export type {Queries} from './fixtures' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as path from 'path' | ||
|
||
import * as playwright from '@playwright/test' | ||
|
||
import { | ||
LocatorFixtures as TestingLibraryFixtures, | ||
locatorFixtures as fixtures, | ||
} from '../../lib/fixture' | ||
|
||
const test = playwright.test.extend<TestingLibraryFixtures>(fixtures) | ||
|
||
const {expect} = test | ||
|
||
test.use({testIdAttribute: 'data-new-id'}) | ||
|
||
test.describe('global configuration', () => { | ||
test.beforeEach(async ({page}) => { | ||
await page.goto(`file://${path.join(__dirname, '../fixtures/page.html')}`) | ||
}) | ||
|
||
test('queries with test ID configured in module scope', async ({queries}) => { | ||
const defaultTestIdLocator = queries.queryByTestId('testid-text-input') | ||
const customTestIdLocator = queries.queryByTestId('first-level-header') | ||
|
||
await expect(defaultTestIdLocator).not.toBeVisible() | ||
await expect(customTestIdLocator).toBeVisible() | ||
}) | ||
|
||
test.describe('overridding global configuration', () => { | ||
test.use({testIdAttribute: 'data-id'}) | ||
|
||
test('overrides test ID configured in module scope', async ({queries}) => { | ||
const globalTestIdLocator = queries.queryByTestId('first-level-header') | ||
const overriddenTestIdLocator = queries.queryByTestId('second-level-header') | ||
|
||
await expect(globalTestIdLocator).not.toBeVisible() | ||
await expect(overriddenTestIdLocator).toBeVisible() | ||
}) | ||
}) | ||
|
||
test("page override doesn't modify global configuration", async ({queries}) => { | ||
const defaultTestIdLocator = queries.queryByTestId('testid-text-input') | ||
const customTestIdLocator = queries.queryByTestId('first-level-header') | ||
|
||
await expect(defaultTestIdLocator).not.toBeVisible() | ||
await expect(customTestIdLocator).toBeVisible() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters