mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-03 07:51:12 +03:00
feat(api): allow getByTestId(regex) (#19419)
This commit is contained in:
parent
7b4ae20305
commit
6cadc56ea3
@ -1087,7 +1087,7 @@ When set to `"hide"`, screenshot will hide text caret. When set to `"initial"`,
|
||||
|
||||
## locator-get-by-test-id-test-id
|
||||
* since: v1.27
|
||||
- `testId` <[string]>
|
||||
- `testId` <[string]|[RegExp]>
|
||||
|
||||
Id to locate the element by.
|
||||
|
||||
|
@ -309,7 +309,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel> implements api.Fr
|
||||
return new Locator(this, selector, options);
|
||||
}
|
||||
|
||||
getByTestId(testId: string): Locator {
|
||||
getByTestId(testId: string | RegExp): Locator {
|
||||
return this.locator(getByTestIdSelector(testIdAttributeName(), testId));
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ export class Locator implements api.Locator {
|
||||
return new Locator(this._frame, this._selector + ' >> ' + selector, options);
|
||||
}
|
||||
|
||||
getByTestId(testId: string): Locator {
|
||||
getByTestId(testId: string | RegExp): Locator {
|
||||
return this.locator(getByTestIdSelector(testIdAttributeName(), testId));
|
||||
}
|
||||
|
||||
@ -339,7 +339,7 @@ export class FrameLocator implements api.FrameLocator {
|
||||
return new Locator(this._frame, this._frameSelector + ' >> internal:control=enter-frame >> ' + selector, options);
|
||||
}
|
||||
|
||||
getByTestId(testId: string): Locator {
|
||||
getByTestId(testId: string | RegExp): Locator {
|
||||
return this.locator(getByTestIdSelector(testIdAttributeName(), testId));
|
||||
}
|
||||
|
||||
|
@ -572,7 +572,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
|
||||
return this.mainFrame().locator(selector, options);
|
||||
}
|
||||
|
||||
getByTestId(testId: string): Locator {
|
||||
getByTestId(testId: string | RegExp): Locator {
|
||||
return this.mainFrame().getByTestId(testId);
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,9 @@ function getByAttributeTextSelector(attrName: string, text: string | RegExp, opt
|
||||
return `internal:attr=[${attrName}=${escapeForAttributeSelector(text, options?.exact || false)}]`;
|
||||
}
|
||||
|
||||
export function getByTestIdSelector(testIdAttributeName: string, testId: string): string {
|
||||
export function getByTestIdSelector(testIdAttributeName: string, testId: string | RegExp): string {
|
||||
if (!isString(testId))
|
||||
return `internal:testid=[${testIdAttributeName}=${testId}]`;
|
||||
return `internal:testid=[${testIdAttributeName}=${escapeForAttributeSelector(testId, true)}]`;
|
||||
}
|
||||
|
||||
|
8
packages/playwright-core/types/types.d.ts
vendored
8
packages/playwright-core/types/types.d.ts
vendored
@ -2597,7 +2597,7 @@ export interface Page {
|
||||
*
|
||||
* @param testId Id to locate the element by.
|
||||
*/
|
||||
getByTestId(testId: string): Locator;
|
||||
getByTestId(testId: string|RegExp): Locator;
|
||||
|
||||
/**
|
||||
* Allows locating elements that contain given text. Consider the following DOM structure:
|
||||
@ -5849,7 +5849,7 @@ export interface Frame {
|
||||
*
|
||||
* @param testId Id to locate the element by.
|
||||
*/
|
||||
getByTestId(testId: string): Locator;
|
||||
getByTestId(testId: string|RegExp): Locator;
|
||||
|
||||
/**
|
||||
* Allows locating elements that contain given text. Consider the following DOM structure:
|
||||
@ -10546,7 +10546,7 @@ export interface Locator {
|
||||
*
|
||||
* @param testId Id to locate the element by.
|
||||
*/
|
||||
getByTestId(testId: string): Locator;
|
||||
getByTestId(testId: string|RegExp): Locator;
|
||||
|
||||
/**
|
||||
* Allows locating elements that contain given text. Consider the following DOM structure:
|
||||
@ -16114,7 +16114,7 @@ export interface FrameLocator {
|
||||
*
|
||||
* @param testId Id to locate the element by.
|
||||
*/
|
||||
getByTestId(testId: string): Locator;
|
||||
getByTestId(testId: string|RegExp): Locator;
|
||||
|
||||
/**
|
||||
* Allows locating elements that contain given text. Consider the following DOM structure:
|
||||
|
@ -36,6 +36,13 @@ it('getByTestId should escape id', async ({ page }) => {
|
||||
await expect(page.getByTestId('He"llo')).toHaveText('Hello world');
|
||||
});
|
||||
|
||||
it('getByTestId should work for regex', async ({ page }) => {
|
||||
await page.setContent('<div><div data-testid="Hello">Hello world</div></div>');
|
||||
await expect(page.getByTestId(/He[l]*o/)).toHaveText('Hello world');
|
||||
await expect(page.mainFrame().getByTestId('Hello')).toHaveText('Hello world');
|
||||
await expect(page.locator('div').getByTestId('Hello')).toHaveText('Hello world');
|
||||
});
|
||||
|
||||
it('getByText should work', async ({ page }) => {
|
||||
await page.setContent(`<div>yo</div><div>ya</div><div>\nye </div>`);
|
||||
expect(await page.getByText('ye').evaluate(e => e.outerHTML)).toContain('>\nye </div>');
|
||||
|
Loading…
Reference in New Issue
Block a user