Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

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

Max Schmittfdb633dc8b86
diff --git a/packages/playwright-core/src/client/locator.ts b/packages/playwright-core/src/client/locator.ts
index f06897a31e..2e4c37081a 100644
--- a/packages/playwright-core/src/client/locator.ts
+++ b/packages/playwright-core/src/client/locator.ts
@@ -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 {
diff --git a/tests/page/locator-query.spec.ts b/tests/page/locator-query.spec.ts
index fb775fc27e..51de7bb365 100644
--- a/tests/page/locator-query.spec.ts
+++ b/tests/page/locator-query.spec.ts
@@ -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"');
+});