Skip to content

Commit

Permalink
cherry-pick(#17893): fix(locators): make regex escape work when multi…
Browse files Browse the repository at this point in the history
…ple spaces are present (#17894)
  • Loading branch information
dgozman authored Oct 7, 2022
1 parent 923a583 commit 3f0af1e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
40 changes: 21 additions & 19 deletions tests/page/selectors-get-by.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
6 changes: 3 additions & 3 deletions tests/playwright-test/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 3f0af1e

Please sign in to comment.