Skip to content

Commit

Permalink
chore: report highlight parse error to debug controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Dec 12, 2024
1 parent 4bcf505 commit 665bf6b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
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');
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.only('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:');
});

0 comments on commit 665bf6b

Please sign in to comment.