feat: treat selectors with leading '(//' as xpath (#821)

This starts treating the following selectors as xpath:
- `page.$('//div')`
- `page.$('(//div)[1]')`
- `page.$('((((//div))))[1]')`

(and generally, any number of leading openting parenthesis)

Fixes #817
This commit is contained in:
Andrey Lushnikov 2020-02-03 14:52:18 -08:00 committed by GitHub
parent cea036ab7e
commit b82bc5fbd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -511,7 +511,9 @@ function normalizeSelector(selector: string): string {
const eqIndex = selector.indexOf('=');
if (eqIndex !== -1 && selector.substring(0, eqIndex).trim().match(/^[a-zA-Z_0-9-]+$/))
return selector;
if (selector.startsWith('//'))
// If selector starts with '//' or '//' prefixed with multiple opening
// parenthesis, consider xpath. @see https://github.com/microsoft/playwright/issues/817
if (/^\(*\/\//.test(selector))
return 'xpath=' + selector;
if (selector.startsWith('"'))
return 'text=' + selector;

View File

@ -177,6 +177,11 @@ module.exports.describe = function({testRunner, expect, selectors, FFOX, CHROMIU
const element = await page.$('//html/body/section');
expect(element).toBeTruthy();
});
it('should auto-detect xpath selector with starting parenthesis', async({page, server}) => {
await page.setContent('<section>test</section>');
const element = await page.$('(//section)[1]');
expect(element).toBeTruthy();
});
it('should auto-detect text selector', async({page, server}) => {
await page.setContent('<section>test</section>');
const element = await page.$('"test"');