From 0ef7152775b1547b335f20ce276a2152600c5c25 Mon Sep 17 00:00:00 2001 From: Sam Treweek Date: Sun, 8 Sep 2024 13:02:32 +0100 Subject: [PATCH] chore: expose testIdAttributeName via Selector.testIdAttributeName --- docs/src/api/class-selectors.md | 5 +++++ docs/src/locators.md | 20 +++++++++++++++++++ .../playwright-core/src/client/selectors.ts | 2 ++ packages/playwright-core/types/types.d.ts | 5 +++++ tests/library/selector-generator.spec.ts | 5 +++++ 5 files changed, 37 insertions(+) diff --git a/docs/src/api/class-selectors.md b/docs/src/api/class-selectors.md index 4c3011430387a4..b64baa17ca606e 100644 --- a/docs/src/api/class-selectors.md +++ b/docs/src/api/class-selectors.md @@ -248,3 +248,8 @@ Defines custom attribute name to be used in [`method: Page.getByTestId`]. `data- - `attributeName` <[string]> Test id attribute name. + +## method: Selectors.testIdAttributeName +* since: v1.48 + +Return the current Test id attribute name. \ No newline at end of file diff --git a/docs/src/locators.md b/docs/src/locators.md index 8ade71efb7faf3..22e4e70c978a21 100644 --- a/docs/src/locators.md +++ b/docs/src/locators.md @@ -607,6 +607,26 @@ page.get_by_test_id("directions").click() await page.GetByTestId("directions").ClickAsync(); ``` +#### Get the current test id attribute name + +Return the current test id attribute name, this will default to `data-testid`, if not updated using your test config or set using [`method: Selectors.setTestIdAttribute`]. + +```java +playwright.selectors().testIdAttributeName(); +``` + +```python async +playwright.selectors.test_id_attribute_name() +``` + +```python sync +playwright.selectors.test_id_attribute_name() +``` + +```csharp +playwright.Selectors.TestIdAttributeName(); +``` + ### Locate by CSS or XPath If you absolutely must use CSS or XPath locators, you can use [`method: Page.locator`] to create a locator that takes a selector describing how to find an element in the page. Playwright supports CSS and XPath selectors, and auto-detects them if you omit `css=` or `xpath=` prefix. diff --git a/packages/playwright-core/src/client/selectors.ts b/packages/playwright-core/src/client/selectors.ts index 2739be0e8dc068..aa2222189c8945 100644 --- a/packages/playwright-core/src/client/selectors.ts +++ b/packages/playwright-core/src/client/selectors.ts @@ -39,6 +39,8 @@ export class Selectors implements api.Selectors { channel._channel.setTestIdAttributeName({ testIdAttributeName: attributeName }).catch(() => {}); } + testIdAttributeName = () => testIdAttributeName(); + _addChannel(channel: SelectorsOwner) { this._channels.add(channel); for (const params of this._registrations) { diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index 765c84c36e52b0..49c97050da581c 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -19895,6 +19895,11 @@ export interface Selectors { * @param attributeName Test id attribute name. */ setTestIdAttribute(attributeName: string): void; + + /** + * Return the current Test id attribute name. + */ + testIdAttributeName(): void; } /** diff --git a/tests/library/selector-generator.spec.ts b/tests/library/selector-generator.spec.ts index e9df1afc6e6fb9..892e40ad545027 100644 --- a/tests/library/selector-generator.spec.ts +++ b/tests/library/selector-generator.spec.ts @@ -596,4 +596,9 @@ it.describe('selector generator', () => { `span >> nth=1`, ]); }); + + it('should return the current testIdAttributeName', async ({ playwright }) => { + playwright.selectors.setTestIdAttribute('data-custom-id'); + expect(playwright.selectors.testIdAttributeName()).toEqual('data-custom-id'); + }); });