diff --git a/packages/playwright-core/src/server/codegen/javascript.ts b/packages/playwright-core/src/server/codegen/javascript.ts index 428ca493f8..e5f72ce122 100644 --- a/packages/playwright-core/src/server/codegen/javascript.ts +++ b/packages/playwright-core/src/server/codegen/javascript.ts @@ -117,8 +117,10 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator { const assertion = action.value ? `toHaveValue(${quote(action.value)})` : `toBeEmpty()`; return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).${assertion};`; } - case 'assertSnapshot': - return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).toMatchAriaSnapshot(${quoteMultiline(action.snapshot)});`; + case 'assertSnapshot': { + const commentIfNeeded = this._isTest ? '' : '// '; + return `${commentIfNeeded}await expect(${subject}.${this._asLocator(action.selector)}).toMatchAriaSnapshot(${quoteMultiline(action.snapshot, `${commentIfNeeded} `)});`; + } } } diff --git a/tests/library/inspector/cli-codegen-aria.spec.ts b/tests/library/inspector/cli-codegen-aria.spec.ts index 820dab7c14..093ab0bbe5 100644 --- a/tests/library/inspector/cli-codegen-aria.spec.ts +++ b/tests/library/inspector/cli-codegen-aria.spec.ts @@ -141,4 +141,35 @@ test.describe(() => { // 3 highlighted tokens. await expect(recorder.recorderPage.locator('.source-line-error-underline')).toHaveCount(3); }); + + test('should generate valid javascript with multiline snapshot assertion', async ({ openRecorder }) => { + const { recorder } = await openRecorder(); + // set width and height to 100% to ensure click is outside of the list + await recorder.setContentAndWait(``); + + await recorder.page.click('x-pw-tool-item.snapshot'); + await recorder.page.hover('body'); + await recorder.trustedClick(); + + // playwright tests assertions are uncommented + await expect.poll(() => + recorder.text('Playwright Test')).toContain([ + ` await expect(page.locator('body')).toMatchAriaSnapshot(\``, + ` - list:`, + ` - listitem: item 1`, + ` - listitem: item 2`, + ` \`);`, + ].join('\n')); + + // non-test javascript has commented assertions + await expect.poll(() => + recorder.text('JavaScript')).toContain([ + ` // await expect(page.locator('body')).toMatchAriaSnapshot(\``, + ` // - list:`, + ` // - listitem: item 1`, + ` // - listitem: item 2`, + ` // \`);`, + ].join('\n')); + + }); });