Skip to content

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

fix(locators): make regex escape work when multiple spaces are present (#17893)

Dmitry Gozmanb64457d8ce99
diff --git a/packages/playwright-core/src/utils/isomorphic/stringUtils.ts b/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
index f060eeb658..f642e5cc8c 100644
--- a/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
+++ b/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
@@ -68,7 +68,7 @@ export function escapeForTextSelector(text: string | RegExp, exact: boolean, cas
if (exact)
return '"' + text.replace(/["]/g, '\\"') + '"';
if (text.includes('"') || text.includes('>>') || text[0] === '/')
- return `/${escapeForRegex(text).replace(/\s+/, '\\s+')}/` + (caseSensitive ? '' : 'i');
+ return `/${escapeForRegex(text).replace(/\s+/g, '\\s+')}/` + (caseSensitive ? '' : 'i');
return text;
}
diff --git a/tests/page/selectors-get-by.spec.ts b/tests/page/selectors-get-by.spec.ts
index 63510c1007..dba7978592 100644
--- a/tests/page/selectors-get-by.spec.ts
+++ b/tests/page/selectors-get-by.spec.ts
@@ -108,29 +108,31 @@ it('getByTitle should work', async ({ page }) => {
});
it('getBy escaping', async ({ page }) => {
- await page.setContent(`<label id=label for=control>Hello
+ await page.setContent(`<label id=label for=control>Hello my
wo"rld</label><input id=control />`);
await page.$eval('input', input => {
- input.setAttribute('placeholder', 'hello\nwo"rld');
- input.setAttribute('title', 'hello\nwo"rld');
- input.setAttribute('alt', 'hello\nwo"rld');
+ input.setAttribute('placeholder', 'hello my\nwo"rld');
+ input.setAttribute('title', 'hello my\nwo"rld');
+ input.setAttribute('alt', 'hello my\nwo"rld');
});
- await expect(page.getByText('hello\nwo"rld')).toHaveAttribute('id', 'label');
- await expect(page.getByLabel('hello\nwo"rld')).toHaveAttribute('id', 'control');
- await expect(page.getByPlaceholder('hello\nwo"rld')).toHaveAttribute('id', 'control');
- await expect(page.getByAltText('hello\nwo"rld')).toHaveAttribute('id', 'control');
- await expect(page.getByTitle('hello\nwo"rld')).toHaveAttribute('id', 'control');
-
- await page.setContent(`<label id=label for=control>Hello
+ await expect(page.getByText('hello my\nwo"rld')).toHaveAttribute('id', 'label');
+ await expect(page.getByText('hello my wo"rld')).toHaveAttribute('id', 'label');
+ await expect(page.getByLabel('hello my\nwo"rld')).toHaveAttribute('id', 'control');
+ await expect(page.getByPlaceholder('hello my\nwo"rld')).toHaveAttribute('id', 'control');
+ await expect(page.getByAltText('hello my\nwo"rld')).toHaveAttribute('id', 'control');
+ await expect(page.getByTitle('hello my\nwo"rld')).toHaveAttribute('id', 'control');
+
+ await page.setContent(`<label id=label for=control>Hello my
world</label><input id=control />`);
await page.$eval('input', input => {
- input.setAttribute('placeholder', 'hello\nworld');
- input.setAttribute('title', 'hello\nworld');
- input.setAttribute('alt', 'hello\nworld');
+ input.setAttribute('placeholder', 'hello my\nworld');
+ input.setAttribute('title', 'hello my\nworld');
+ input.setAttribute('alt', 'hello my\nworld');
});
- await expect(page.getByText('hello\nworld')).toHaveAttribute('id', 'label');
- await expect(page.getByLabel('hello\nworld')).toHaveAttribute('id', 'control');
- await expect(page.getByPlaceholder('hello\nworld')).toHaveAttribute('id', 'control');
- await expect(page.getByAltText('hello\nworld')).toHaveAttribute('id', 'control');
- await expect(page.getByTitle('hello\nworld')).toHaveAttribute('id', 'control');
+ await expect(page.getByText('hello my\nworld')).toHaveAttribute('id', 'label');
+ await expect(page.getByText('hello my world')).toHaveAttribute('id', 'label');
+ await expect(page.getByLabel('hello my\nworld')).toHaveAttribute('id', 'control');
+ await expect(page.getByPlaceholder('hello my\nworld')).toHaveAttribute('id', 'control');
+ await expect(page.getByAltText('hello my\nworld')).toHaveAttribute('id', 'control');
+ await expect(page.getByTitle('hello my\nworld')).toHaveAttribute('id', 'control');
});
diff --git a/tests/playwright-test/hooks.spec.ts b/tests/playwright-test/hooks.spec.ts
index 24d1fda0c8..035b5c4eb3 100644
--- a/tests/playwright-test/hooks.spec.ts
+++ b/tests/playwright-test/hooks.spec.ts
@@ -740,11 +740,11 @@ test('test.setTimeout should work separately in afterAll', async ({ runInlineTes
});
test.afterAll(async () => {
console.log('\\n%%afterAll');
- test.setTimeout(1000);
- await new Promise(f => setTimeout(f, 800));
+ test.setTimeout(3000);
+ await new Promise(f => setTimeout(f, 2000));
});
`,
- }, { timeout: '100' });
+ }, { timeout: '1000' });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
expect(result.output.split('\n').filter(line => line.startsWith('%%'))).toEqual([