feat(api): allow getByTestId(regex) (#19419)

This commit is contained in:
Pavel Feldman 2022-12-13 08:43:13 -08:00 committed by GitHub
parent 7b4ae20305
commit 6cadc56ea3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 10 deletions

View File

@ -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.

View File

@ -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));
}

View File

@ -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));
}

View File

@ -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);
}

View File

@ -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)}]`;
}

View File

@ -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:

View File

@ -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>');