Skip to content

Commit

Permalink
chore: expose testIdAttributeName via Selector.testIdAttributeName
Browse files Browse the repository at this point in the history
  • Loading branch information
serialbandicoot authored and samtreweek-bjss committed Sep 8, 2024
1 parent 718bd9b commit 0ef7152
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/src/api/class-selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
20 changes: 20 additions & 0 deletions docs/src/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions packages/playwright-core/src/client/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/library/selector-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 0ef7152

Please sign in to comment.