@cronn/playwright-semantic-snapshots 0.0.0-stage → 0.35.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,155 @@
1
- # Temporary Holding Version
1
+ # Playwright Semantic Snapshots
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Semantic snapshots for Playwright.
4
+
5
+ [Read the full documentation](https://cronn.github.io/file-snapshots/playwright/semantic-snapshots/)
6
+
7
+ > [!NOTE]
8
+ > Semantic Snapshots are currently experimental. Not all HTML elements, ARIA roles and attributes are covered. Breaking changes to the snapshot format are possible.
9
+
10
+ Semantic Snapshots capture the semantic structure and accessible contents of a page, inspired by [Playwright's ARIA Snapshots](https://playwright.dev/docs/aria-snapshots). They cover additional properties like validation attributes of form inputs and are optimized for JSON serialization. They are stored as file snapshots using [`@cronn/playwright-file-snapshots`](https://www.npmjs.com/package/@cronn/playwright-file-snapshots).
11
+
12
+ ## Getting Started
13
+
14
+ ### Installation
15
+
16
+ ```shell
17
+ pnpm add -D @cronn/playwright-semantic-snapshots
18
+ ```
19
+
20
+ ```shell
21
+ npm install -D @cronn/playwright-semantic-snapshots
22
+ ```
23
+
24
+ ```shell
25
+ yarn add -D @cronn/playwright-semantic-snapshots
26
+ ```
27
+
28
+ ### Configuration
29
+
30
+ Define a reusable `expect` export (e.g. in `fixtures.ts`):
31
+
32
+ ```ts
33
+ import { defineSemanticSnapshotMatchers } from "@cronn/playwright-semantic-snapshots";
34
+
35
+ export const expect = defineSemanticSnapshotMatchers();
36
+ ```
37
+
38
+ The function accepts the same options as `defineFileSnapshotMatchers`. To combine both, define a shared configuration using `defineConfig`:
39
+
40
+ ```ts
41
+ import { mergeExpects } from "@playwright/test";
42
+
43
+ import {
44
+ defineConfig,
45
+ defineFileSnapshotMatchers,
46
+ } from "@cronn/playwright-file-snapshots";
47
+ import { defineSemanticSnapshotMatchers } from "@cronn/playwright-semantic-snapshots";
48
+
49
+ const config = defineConfig({
50
+ validationDir: "custom-data/validation",
51
+ outputDir: "custom-data/output",
52
+ });
53
+
54
+ export const expect = mergeExpects(
55
+ defineFileSnapshotMatchers(config),
56
+ defineSemanticSnapshotMatchers(config),
57
+ );
58
+ ```
59
+
60
+ ## Writing Tests
61
+
62
+ ### Semantic Snapshots
63
+
64
+ `toMatchSemanticSnapshotFile` snapshots the semantic structure of an element, including all supported roles and attributes:
65
+
66
+ ```ts
67
+ test("matches semantic snapshot", async ({ page }) => {
68
+ await page.setContent(`
69
+ <main>
70
+ <h1>List</h1>
71
+ <ul>
72
+ <li>Apple</li>
73
+ <li>Peach</li>
74
+ </ul>
75
+ </main>
76
+ `);
77
+
78
+ await expect(page.getByRole("main")).toMatchSemanticSnapshotFile();
79
+ });
80
+ ```
81
+
82
+ **Output (`matches_semantic_snapshot.json`):**
83
+
84
+ ```json
85
+ {
86
+ "main": [
87
+ {
88
+ "heading": "List",
89
+ "level": 1
90
+ },
91
+ {
92
+ "list": [
93
+ {
94
+ "listitem": "Apple"
95
+ },
96
+ {
97
+ "listitem": "Peach"
98
+ }
99
+ ]
100
+ }
101
+ ]
102
+ }
103
+ ```
104
+
105
+ Snapshots can be narrowed down using a `filter` and customized per role using `transformers`. See [JSON Snapshot](https://cronn.github.io/file-snapshots/playwright/semantic-snapshots/json-snapshot).
106
+
107
+ ### Markdown Table Snapshots
108
+
109
+ `toMatchMarkdownTableSnapshotFile` snapshots an HTML `<table>` or ARIA `grid` as a Markdown table:
110
+
111
+ ```ts
112
+ test("matches table snapshot", async ({ page }) => {
113
+ await page.setContent(`
114
+ <table>
115
+ <thead>
116
+ <tr><th>Product</th><th>Price</th></tr>
117
+ </thead>
118
+ <tbody>
119
+ <tr><td>Laptop</td><td>$999</td></tr>
120
+ <tr><td>Mouse</td><td>$25</td></tr>
121
+ </tbody>
122
+ </table>
123
+ `);
124
+
125
+ await expect(page.getByRole("table")).toMatchMarkdownTableSnapshotFile();
126
+ });
127
+ ```
128
+
129
+ **Output (`matches_table_snapshot.md`):**
130
+
131
+ ```md
132
+ | Product | Price |
133
+ | ------- | ----- |
134
+ | Laptop | $999 |
135
+ | Mouse | $25 |
136
+ ```
137
+
138
+ See [Markdown Table Snapshot](https://cronn.github.io/file-snapshots/playwright/semantic-snapshots/markdown-table-snapshot).
139
+
140
+ ### Snapshot Functions
141
+
142
+ `semanticSnapshot` and `markdownTableSnapshot` return the snapshot instead of writing it to a file. Use them to compose snapshots with the matchers of `@cronn/playwright-file-snapshots`:
143
+
144
+ ```ts
145
+ import { semanticSnapshot } from "@cronn/playwright-semantic-snapshots";
146
+
147
+ test("matches composed snapshot", async ({ page }) => {
148
+ await expect({
149
+ navigation: await semanticSnapshot(page.getByRole("navigation")),
150
+ main: await semanticSnapshot(page.getByRole("main")),
151
+ }).toMatchJsonFile();
152
+ });
153
+ ```
154
+
155
+ For working with file snapshots, e.g. adding and updating validation files, see [`@cronn/playwright-file-snapshots`](https://www.npmjs.com/package/@cronn/playwright-file-snapshots).