Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: report highlight parse error to debug controller #33984

Merged
merged 1 commit into from
Dec 14, 2024
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
15 changes: 13 additions & 2 deletions packages/playwright-core/src/server/debugController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { Recorder } from './recorder';
import { EmptyRecorderApp } from './recorder/recorderApp';
import { asLocator, type Language } from '../utils';
import { parseYamlForAriaSnapshot } from './ariaSnapshot';
import type { ParsedYaml } from '../utils/isomorphic/ariaSnapshot';
import { parseYamlTemplate } from '../utils/isomorphic/ariaSnapshot';
import { unsafeLocatorOrSelectorAsSelector } from '../utils/isomorphic/locatorParser';

const internalMetadata = serverSideCallMetadata();

Expand Down Expand Up @@ -144,9 +147,17 @@ export class DebugController extends SdkObject {
}

async highlight(params: { selector?: string, ariaTemplate?: string }) {
// Assert parameters validity.
if (params.selector)
unsafeLocatorOrSelectorAsSelector(this._sdkLanguage, params.selector, 'data-testid');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who catches the error here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debugControllerDispatcher will try catch and return the protocol error.

let parsedYaml: ParsedYaml | undefined;
if (params.ariaTemplate) {
parsedYaml = parseYamlForAriaSnapshot(params.ariaTemplate);
parseYamlTemplate(parsedYaml);
}
for (const recorder of await this._allRecorders()) {
if (params.ariaTemplate)
recorder.setHighlightedAriaTemplate(parseYamlForAriaSnapshot(params.ariaTemplate));
if (parsedYaml)
recorder.setHighlightedAriaTemplate(parsedYaml);
else if (params.selector)
recorder.setHighlightedSelector(this._sdkLanguage, params.selector);
}
Expand Down
19 changes: 12 additions & 7 deletions packages/playwright-core/src/utils/isomorphic/locatorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,23 @@ function transform(template: string, params: TemplateParams, testIdAttributeName

export function locatorOrSelectorAsSelector(language: Language, locator: string, testIdAttributeName: string): string {
try {
parseSelector(locator);
return locator;
return unsafeLocatorOrSelectorAsSelector(language, locator, testIdAttributeName);
} catch (e) {
return '';
}
}

export function unsafeLocatorOrSelectorAsSelector(language: Language, locator: string, testIdAttributeName: string): string {
try {
const { selector, preferredQuote } = parseLocator(locator, testIdAttributeName);
const locators = asLocators(language, selector, undefined, undefined, preferredQuote);
const digest = digestForComparison(language, locator);
if (locators.some(candidate => digestForComparison(language, candidate) === digest))
return selector;
parseSelector(locator);
return locator;
} catch (e) {
}
const { selector, preferredQuote } = parseLocator(locator, testIdAttributeName);
const locators = asLocators(language, selector, undefined, undefined, preferredQuote);
const digest = digestForComparison(language, locator);
if (locators.some(candidate => digestForComparison(language, candidate) === digest))
return selector;
return '';
}

Expand Down
6 changes: 6 additions & 0 deletions tests/library/debug-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,9 @@ test('should highlight aria template', async ({ backend, connectedBrowser }, tes
const box2 = roundBox(await highlight.boundingBox());
expect(box1).toEqual(box2);
});

test('should report error in aria template', async ({ backend }) => {
await backend.navigate({ url: `data:text/html,<button>Submit</button>` });
const error = await backend.highlight({ ariaTemplate: `- button "Submit` }).catch(e => e);
expect(error.message).toContain('Unterminated string:');
});
Loading