From 33f9c8279c4375d2324957e00688bf750f90e43d Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Tue, 12 Nov 2024 02:26:54 -0800 Subject: [PATCH] chore: more aria snapshot fixes (#33534) --- .../src/server/injected/ariaSnapshot.ts | 3 ++- .../src/matchers/toMatchAriaSnapshot.ts | 2 +- tests/page/page-aria-snapshot.spec.ts | 10 +++++----- tests/page/to-match-aria-snapshot.spec.ts | 11 ----------- .../update-aria-snapshot.spec.ts | 19 +++++++++++++++++++ 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/packages/playwright-core/src/server/injected/ariaSnapshot.ts b/packages/playwright-core/src/server/injected/ariaSnapshot.ts index 06de76c820..b2352a1b9c 100644 --- a/packages/playwright-core/src/server/injected/ariaSnapshot.ts +++ b/packages/playwright-core/src/server/injected/ariaSnapshot.ts @@ -300,7 +300,8 @@ export function renderAriaTree(ariaNode: AriaNode, options?: { mode?: 'raw' | 'r } let key = ariaNode.role; - if (ariaNode.name) { + // Yaml has a limit of 1024 characters per key, and we leave some space for role and attributes. + if (ariaNode.name && ariaNode.name.length <= 900) { const name = renderString(ariaNode.name); if (name) { const stringifiedName = name.startsWith('/') && name.endsWith('/') ? name : JSON.stringify(name); diff --git a/packages/playwright/src/matchers/toMatchAriaSnapshot.ts b/packages/playwright/src/matchers/toMatchAriaSnapshot.ts index 47e9f70441..f3e7d47a6e 100644 --- a/packages/playwright/src/matchers/toMatchAriaSnapshot.ts +++ b/packages/playwright/src/matchers/toMatchAriaSnapshot.ts @@ -79,7 +79,7 @@ export async function toMatchAriaSnapshot( if (notFound) { return { pass: this.isNot, - message: () => messagePrefix + `Expected: ${this.utils.printExpected(expected)}\nReceived: ${EXPECTED_COLOR('not found')}` + callLogText(log), + message: () => messagePrefix + `Expected: ${this.utils.printExpected(expected)}\nReceived: ${EXPECTED_COLOR('')}` + callLogText(log), name: 'toMatchAriaSnapshot', expected, }; diff --git a/tests/page/page-aria-snapshot.spec.ts b/tests/page/page-aria-snapshot.spec.ts index 22ae280b4d..a7937e9be5 100644 --- a/tests/page/page-aria-snapshot.spec.ts +++ b/tests/page/page-aria-snapshot.spec.ts @@ -479,16 +479,16 @@ it('should escape yaml text in text nodes', async ({ page }) => { `); }); -it.fixme('should handle long strings', async ({ page }) => { +it('should handle long strings', async ({ page }) => { + const s = 'a'.repeat(10000); await page.setContent(` -
${'a'.repeat(100000)}
+
${s}
`); - const trimmed = 'a'.repeat(1000); await checkAndMatchSnapshot(page.locator('body'), ` - - link "${trimmed}": - - region: "${trimmed}" + - link: + - region: ${s} `); }); diff --git a/tests/page/to-match-aria-snapshot.spec.ts b/tests/page/to-match-aria-snapshot.spec.ts index 40709909ce..05a02f369b 100644 --- a/tests/page/to-match-aria-snapshot.spec.ts +++ b/tests/page/to-match-aria-snapshot.spec.ts @@ -640,17 +640,6 @@ test('call log should contain actual snapshot', async ({ page }) => { expect(stripAnsi(error.message)).toContain(`- unexpected value "- heading "todos" [level=1]"`); }); -test.fixme('should normalize whitespace when matching accessible name', async ({ page }) => { - await page.setContent(` - - `); - await expect(page.locator('body')).toMatchAriaSnapshot(` - - | - button "hello - world" - `); -}); - test('should parse attributes', async ({ page }) => { { await page.setContent(` diff --git a/tests/playwright-test/update-aria-snapshot.spec.ts b/tests/playwright-test/update-aria-snapshot.spec.ts index e6d76da282..2a831521a7 100644 --- a/tests/playwright-test/update-aria-snapshot.spec.ts +++ b/tests/playwright-test/update-aria-snapshot.spec.ts @@ -377,3 +377,22 @@ test('should generate baseline for input values', async ({ runInlineTest }, test const result2 = await runInlineTest({}); expect(result2.exitCode).toBe(0); }); + +test('should not update snapshots when locator did not match', async ({ runInlineTest }, testInfo) => { + const result = await runInlineTest({ + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('test', async ({ page }) => { + await page.setContent('

hello

'); + await expect(page.locator('div')).toMatchAriaSnapshot('- heading', { timeout: 3000 }); + }); + `, + }, { 'update-snapshots': true }); + + expect(result.exitCode).toBe(1); + const patchPath = testInfo.outputPath('test-results/rebaselines.patch'); + expect(fs.existsSync(patchPath)).toBe(false); + expect(result.output).not.toContain('New baselines created'); + expect(result.output).toContain('Expected: "- heading"'); + expect(result.output).toContain('Received: '); +});