Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: make sure asLocatorDescription() tolerates invalid selectors
  • Loading branch information
dgozman committed Jul 4, 2025
commit 92979acd626de660ceefe3aae6ebaeaf63ada028
18 changes: 13 additions & 5 deletions packages/playwright-core/src/utils/isomorphic/locatorGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ export interface LocatorFactory {
}

export function asLocatorDescription(lang: Language, selector: string): string | undefined {
const parsed = parseSelector(selector);
const lastPart = parsed.parts[parsed.parts.length - 1];
if (lastPart?.name === 'internal:describe')
return JSON.parse(lastPart.body as string);
return asLocator(lang, selector);
try {
const parsed = parseSelector(selector);
const lastPart = parsed.parts[parsed.parts.length - 1];
if (lastPart?.name === 'internal:describe') {
const description = JSON.parse(lastPart.body as string);
if (typeof description === 'string')
return description;
}
return innerAsLocators(new generators[lang](), parsed, false, 1)[0];
} catch (e) {
// Tolerate invalid input.
return selector;
}
}

export function asLocator(lang: Language, selector: string, isFrameLocator: boolean = false): string {
Expand Down
9 changes: 8 additions & 1 deletion tests/library/locator-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { contextTest as it, expect } from '../config/browserTest';
import { asLocator, asLocators } from '../../packages/playwright-core/lib/utils/isomorphic/locatorGenerators';
import { asLocator, asLocators, asLocatorDescription } from '../../packages/playwright-core/lib/utils/isomorphic/locatorGenerators';
import { locatorOrSelectorAsSelector as parseLocator } from '../../packages/playwright-core/lib/utils/isomorphic/locatorParser';
import type { Page, Frame, Locator, FrameLocator } from 'playwright-core';

Expand Down Expand Up @@ -654,3 +654,10 @@ it('should not oom in locator parser', async ({ page }) => {
const error = await locator.count().catch(e => e);
expect(error.message).toContain('Frame locators are not allowed inside composite locators');
});

it('asLocatorDescription invalid input', async () => {
expect.soft(asLocatorDescription('javascript', `body >> internal:describe="desc"`)).toBe(`desc`);
expect.soft(asLocatorDescription('javascript', `body >> internal:describe=12`)).toBe(`locator('body')`);
expect.soft(asLocatorDescription('javascript', `following-sibling::*[1]`)).toBe(`following-sibling::*[1]`);
expect.soft(asLocatorDescription('javascript', `body >> internal:describe="desc" >> div`)).toBe(`locator('body').locator('div')`);
});
Loading