Skip to content

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

fix: playwright.locator.hasText RegExp flag serialisation in Console API (#11516)

Max Schmittfb139cefac19
diff --git a/packages/playwright-core/src/server/supplements/injected/consoleApi.ts b/packages/playwright-core/src/server/supplements/injected/consoleApi.ts
index ee050302ad..e5a0613bdb 100644
--- a/packages/playwright-core/src/server/supplements/injected/consoleApi.ts
+++ b/packages/playwright-core/src/server/supplements/injected/consoleApi.ts
@@ -28,9 +28,10 @@ function createLocator(injectedScript: InjectedScript, initial: string, options?
this.selector = selector;
if (options?.hasText) {
const text = options.hasText;
- const matcher = text instanceof RegExp ? 'text-matches' : 'has-text';
- const source = escapeWithQuotes(text instanceof RegExp ? text.source : text, '"');
- this.selector += ` >> :scope:${matcher}(${source})`;
+ if (text instanceof RegExp)
+ this.selector += ` >> :scope:text-matches(${escapeWithQuotes(text.source, '"')}, "${text.flags}")`;
+ else
+ this.selector += ` >> :scope:has-text(${escapeWithQuotes(text)})`;
}
const parsed = injectedScript.parseSelector(this.selector);
this.element = injectedScript.querySelector(parsed, document, false);
diff --git a/tests/inspector/console-api.spec.ts b/tests/inspector/console-api.spec.ts
index 84f39a2956..73426ac7f8 100644
--- a/tests/inspector/console-api.spec.ts
+++ b/tests/inspector/console-api.spec.ts
@@ -54,6 +54,9 @@ it('should support playwright.locator.value', async ({ page }) => {
it('should support playwright.locator.values', async ({ page }) => {
await page.setContent('<div>Hello<div>Bar</div></div>');
- const length = await page.evaluate(`playwright.locator('div', { hasText: 'Hello' }).elements.length`);
- expect(length).toBe(1);
+ expect(await page.evaluate(`playwright.locator('div', { hasText: 'Hello' }).elements.length`)).toBe(1);
+ expect(await page.evaluate(`playwright.locator('div', { hasText: 'HElLo' }).elements.length`)).toBe(1);
+ expect(await page.evaluate(`playwright.locator('div', { hasText: /ELL/ }).elements.length`)).toBe(0);
+ expect(await page.evaluate(`playwright.locator('div', { hasText: /ELL/i }).elements.length`)).toBe(1);
+ expect(await page.evaluate(`playwright.locator('div', { hasText: /Hello/ }).elements.length`)).toBe(1);
});