Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions docs/src/api/class-locatorassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,13 @@ await expect(page.locator('body')).toMatchAriaSnapshot({ name: 'body.aria.yml' }
Name of the snapshot to store in the snapshot folder corresponding to this test.
Generates sequential names if not specified.

### option: LocatorAssertions.toMatchAriaSnapshot#2.exact
* since: v1.57
* langs: js
- `exact` <[boolean]>

When `true`, saves the raw accessibility tree content without using regexes.

### option: LocatorAssertions.toMatchAriaSnapshot#2.timeout = %%-js-assertions-timeout-%%
* since: v1.50

Expand Down
4 changes: 3 additions & 1 deletion packages/playwright/src/matchers/toMatchAriaSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ToMatchAriaSnapshotExpected = {
name?: string;
path?: string;
timeout?: number;
exact?: boolean;
} | string;

export async function toMatchAriaSnapshot(
Expand Down Expand Up @@ -122,7 +123,8 @@ export async function toMatchAriaSnapshot(
generateMissingBaseline) {
if (expectedPath) {
await fs.promises.mkdir(path.dirname(expectedPath), { recursive: true });
await fs.promises.writeFile(expectedPath, typedReceived.regex, 'utf8');
const contentToSave = (expectedParam && typeof expectedParam !== 'string' && expectedParam.exact) ? typedReceived.raw : typedReceived.regex;
await fs.promises.writeFile(expectedPath, contentToSave, 'utf8');
const relativePath = path.relative(process.cwd(), expectedPath);
if (updateSnapshots === 'missing') {
const message = `A snapshot doesn't exist at ${relativePath}, writing actual.`;
Expand Down
5 changes: 5 additions & 0 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9536,6 +9536,11 @@ interface LocatorAssertions {
* @param options
*/
toMatchAriaSnapshot(options?: {
/**
* When `true`, saves the raw accessibility tree content without using regexes.
*/
exact?: boolean;

/**
* Name of the snapshot to store in the snapshot folder corresponding to this test. Generates sequential names if not
* specified.
Expand Down
45 changes: 45 additions & 0 deletions tests/playwright-test/aria-snapshot-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,48 @@ test('should respect config.expect.toMatchAriaSnapshot.pathTemplate', async ({ r
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});

test('should save aria snapshot in raw mode', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import fs from 'fs';
test('test', async ({ page }) => {
await page.setContent(\`<h1>hello world 123</h1>\`);
await expect(page.locator('body')).toMatchAriaSnapshot({ name: 'test.aria.yml', exact: true });
expect(fs.readFileSync('a.spec.ts-snapshots/test.aria.yml', { encoding: 'utf8' })).toEqual('- heading "hello world 123" [level=1]');
});
`
}, { 'update-snapshots': 'all' });
expect(result.exitCode).toBe(0);
});

test('should save aria snapshot in regex mode', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import fs from 'fs';
test('test', async ({ page }) => {
await page.setContent(\`<h1>hello world 123</h1>\`);
await expect(page.locator('body')).toMatchAriaSnapshot({ name: 'test.aria.yml', exact: false });
expect(fs.readFileSync('a.spec.ts-snapshots/test.aria.yml', { encoding: 'utf8' })).toEqual('- heading /hello world \\\\d+/ [level=1]');
});
`
}, { 'update-snapshots': 'all' });
expect(result.exitCode).toBe(0);
});

test('should save aria snapshot in regex mode without exact argument', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import fs from 'fs';
test('test', async ({ page }) => {
await page.setContent(\`<h1>hello world 123</h1>\`);
await expect(page.locator('body')).toMatchAriaSnapshot({ name: 'test.aria.yml' });
expect(fs.readFileSync('a.spec.ts-snapshots/test.aria.yml', { encoding: 'utf8' })).toEqual('- heading /hello world \\\\d+/ [level=1]');
});
`
}, { 'update-snapshots': 'all' });
expect(result.exitCode).toBe(0);
});