Skip to content

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

fix(selectors): max hasText regex against nested nodes (#15638)

Yury Semikhatskycb81d23ad69d
diff --git a/packages/playwright-core/src/client/locator.ts b/packages/playwright-core/src/client/locator.ts
index 9d805b3cfa..a77fd56f34 100644
--- a/packages/playwright-core/src/client/locator.ts
+++ b/packages/playwright-core/src/client/locator.ts
@@ -42,7 +42,7 @@ export class Locator implements api.Locator {
if (options?.hasText) {
const text = options.hasText;
if (isRegExp(text))
- this._selector += ` >> :scope:text-matches(${escapeWithQuotes(text.source, '"')}, "${text.flags}")`;
+ this._selector += ` >> has=${JSON.stringify('text=' + text.toString())}`;
else
this._selector += ` >> :scope:has-text(${escapeWithQuotes(text, '"')})`;
}
diff --git a/tests/page/locator-query.spec.ts b/tests/page/locator-query.spec.ts
index fc5194b97d..29742c1531 100644
--- a/tests/page/locator-query.spec.ts
+++ b/tests/page/locator-query.spec.ts
@@ -92,11 +92,22 @@ it('should filter by regex and regexp flags', async ({ page }) => {
it('should filter by case-insensitive regex in a child', async ({ page }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/15348' });
- it.fixme();
await page.setContent(`<div class="test"><h5>Title Text</h5></div>`);
await expect(page.locator('div', { hasText: /^title text$/i })).toHaveText('Title Text');
});
+it('should filter by case-insensitive regex in multiple children', async ({ page }) => {
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/15348' });
+ await page.setContent(`<div class="test"><h5>Title</h5> <h2><i>Text</i></h2></div>`);
+ await expect(page.locator('div', { hasText: /^title text$/i })).toHaveClass('test');
+});
+
+it('should filter by regex with special symbols', async ({ page }) => {
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/15348' });
+ await page.setContent(`<div class="test"><h5>First/"and"</h5><h2><i>Second\\</i></h2></div>`);
+ await expect(page.locator('div', { hasText: /^first\/".*"second\\$/si })).toHaveClass('test');
+});
+
it('should support has:locator', async ({ page, trace }) => {
it.skip(trace === 'on');