chore: do not use relative xpath when querying from document (#24405)
Fixes https://github.com/microsoft/playwright/issues/24399
This commit is contained in:
Родитель
d81b35b836
Коммит
33d62d9a97
|
@ -18,7 +18,7 @@ import type { SelectorEngine, SelectorRoot } from './selectorEngine';
|
|||
|
||||
export const XPathEngine: SelectorEngine = {
|
||||
queryAll(root: SelectorRoot, selector: string): Element[] {
|
||||
if (selector.startsWith('/'))
|
||||
if (selector.startsWith('/') && root.nodeType !== Node.DOCUMENT_NODE)
|
||||
selector = '.' + selector;
|
||||
const result: Element[] = [];
|
||||
const document = root.ownerDocument || root;
|
||||
|
|
|
@ -494,6 +494,14 @@ it('asLocator internal:chain', async () => {
|
|||
expect.soft(asLocator('csharp', 'div >> internal:chain="span >> article"', false)).toBe(`Locator("div").Locator(Locator("span").Locator("article"))`);
|
||||
});
|
||||
|
||||
it('asLocator xpath', async () => {
|
||||
const selector = `//*[contains(normalizer-text(), 'foo']`;
|
||||
expect.soft(asLocator('javascript', selector, false)).toBe(`locator('xpath=//*[contains(normalizer-text(), \\'foo\\']')`);
|
||||
expect.soft(asLocator('python', selector, false)).toBe(`locator(\"xpath=//*[contains(normalizer-text(), 'foo']\")`);
|
||||
expect.soft(asLocator('java', selector, false)).toBe(`locator(\"xpath=//*[contains(normalizer-text(), 'foo']\")`);
|
||||
expect.soft(asLocator('csharp', selector, false)).toBe(`Locator(\"xpath=//*[contains(normalizer-text(), 'foo']\")`);
|
||||
});
|
||||
|
||||
it('parse locators strictly', () => {
|
||||
const selector = 'div >> internal:has-text=\"Goodbye world\"i >> span';
|
||||
|
||||
|
|
|
@ -318,6 +318,12 @@ it('should work with pipe in xpath', async ({ page, server }) => {
|
|||
await page.click(`//code|//span[@id="t2"]`);
|
||||
});
|
||||
|
||||
it('should print original xpath in error', async ({ page, browserName }) => {
|
||||
const error = await page.locator(`//*[contains(@Class, 'foo']`).isVisible().catch(e => e);
|
||||
expect(error.message).toContain('//*[contains(@Class, \\\'foo\\\']');
|
||||
expect(error.message).not.toContain('.//*[contains(@Class, \'foo\']');
|
||||
});
|
||||
|
||||
it('data-testid on the handle should be relative', async ({ page }) => {
|
||||
await page.setContent(`
|
||||
<span data-testid="find-me" id=target1>1</span>
|
||||
|
|
Загрузка…
Ссылка в новой задаче