Skip to content

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

fix: text-is() should ignore comments (#33980)

Yury Semikhatsky76d46d478f65
diff --git a/packages/playwright-core/src/server/injected/selectorUtils.ts b/packages/playwright-core/src/server/injected/selectorUtils.ts
index 746e12af9d..da0f869fa0 100644
--- a/packages/playwright-core/src/server/injected/selectorUtils.ts
+++ b/packages/playwright-core/src/server/injected/selectorUtils.ts
@@ -73,6 +73,8 @@ export function elementText(cache: Map<Element | ShadowRoot, ElementText>, root:
if (child.nodeType === Node.TEXT_NODE) {
value.full += child.nodeValue || '';
currentImmediate += child.nodeValue || '';
+ } else if (child.nodeType === Node.COMMENT_NODE) {
+ continue;
} else {
if (currentImmediate)
value.immediate.push(currentImmediate);
diff --git a/tests/page/selectors-text.spec.ts b/tests/page/selectors-text.spec.ts
index af9a2d2730..3793cdac5c 100644
--- a/tests/page/selectors-text.spec.ts
+++ b/tests/page/selectors-text.spec.ts
@@ -183,6 +183,17 @@ it('should work across nodes', async ({ page }) => {
expect(await page.$$eval(`text=/world/`, els => els.length)).toBe(1);
});
+it('text-is() should ignore comments', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33976' }
+}, async ({ page }) => {
+ await page.setContent(`<div id=me>hel<!-- comment -->lo
+ <!-- comment -->
+ world</div>`);
+ expect(await page.$eval(`:text-is("hello world")`, e => e.id)).toBe('me');
+ expect(await page.locator('div', { hasText: 'hello world' }).getAttribute('id')).toBe('me');
+ expect(await page.getByText('hello world', { exact: true }).getAttribute('id')).toBe('me');
+});
+
it('should work with text nodes in quoted mode', async ({ page }) => {
await page.setContent(`<div id=target1>Hello<span id=target2>wo rld </span> Hi again </div>`);
expect(await page.$eval(`text="Hello"`, e => e.id)).toBe('target1');