fix: support regexp flags with locator.withText() (#10779)

This commit is contained in:
Max Schmitt 2021-12-07 19:33:04 -08:00 committed by GitHub
parent a08a41f6c9
commit fdb633dc8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -99,9 +99,9 @@ export class Locator implements api.Locator {
}
withText(text: string | RegExp): Locator {
const matcher = isRegExp(text) ? 'text-matches' : 'has-text';
const source = escapeWithQuotes(isRegExp(text) ? text.source : text, '"');
return new Locator(this._frame, this._selector + ` >> :scope:${matcher}(${source})`);
if (isRegExp(text))
return new Locator(this._frame, this._selector + ` >> :scope:text-matches(${escapeWithQuotes(text.source, '"')}, "${text.flags}")`);
return new Locator(this._frame, this._selector + ` >> :scope:has-text(${escapeWithQuotes(text, '"')})`);
}
frameLocator(selector: string): FrameLocator {

View File

@ -84,3 +84,8 @@ it('should filter by regex with quotes', async ({ page }) => {
await page.setContent(`<div>Hello "world"</div><div>Hello world</div>`);
await expect(page.locator('div').withText(/Hello "world"/)).toHaveText('Hello "world"');
});
it('should filter by regex and regexp flags', async ({ page }) => {
await page.setContent(`<div>Hello "world"</div><div>Hello world</div>`);
await expect(page.locator('div').withText(/hElLo "world"/i)).toHaveText('Hello "world"');
});