Skip to content

Commit ffb4d44

Browse files
yury-sclaude
andcommitted
test: add basic ariaSnapshot tests
Port shouldSnapshotIntegration, shouldSupportMultilineText, shouldConcatenate*, shouldIncludePseudo*, shouldWorkWithSlots, shouldSnapshotInnerText, checkAriaHiddenText, shouldIgnorePresentationAndNoneRoles, shouldNotUseOnAsCheckboxValue, shouldNotReportTextareaTextContent, shouldNotShow*HiddenElements, shouldSnapshotPlaceholderWhenDifferentFromName from tests/page/page-aria-snapshot.spec.ts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 33d4273 commit ffb4d44

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

playwright/src/test/java/com/microsoft/playwright/TestPageAriaSnapshot.java

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,209 @@ void matchValuesBothAgainstRegexAndString(Page page) {
136136
" - /url: /auth?r=/");
137137
}
138138

139+
@Test
140+
void shouldSnapshotIntegration(Page page) {
141+
page.setContent(
142+
"<h1>Microsoft</h1>" +
143+
"<div>Open source projects and samples from Microsoft</div>" +
144+
"<ul>" +
145+
"<li><details><summary>Verified</summary><div><div>" +
146+
"<p>We've verified that the organization <strong>microsoft</strong> controls the domain:</p>" +
147+
"<ul><li class=\"mb-1\"><strong>opensource.microsoft.com</strong></li></ul>" +
148+
"<div><a href=\"about: blank\">Learn more about verified organizations</a></div>" +
149+
"</div></div></details></li>" +
150+
"<li><a href=\"about:blank\"><summary title=\"Label: GitHub Sponsor\">Sponsor</summary></a></li>" +
151+
"</ul>");
152+
checkAndMatchSnapshot(page.locator("body"),
153+
"- heading \"Microsoft\" [level=1]\n" +
154+
"- text: Open source projects and samples from Microsoft\n" +
155+
"- list:\n" +
156+
" - listitem:\n" +
157+
" - group: Verified\n" +
158+
" - listitem:\n" +
159+
" - link \"Sponsor\":\n" +
160+
" - /url: about:blank");
161+
}
162+
163+
@Test
164+
void shouldSupportMultilineText(Page page) {
165+
page.setContent("<p>\n Line 1\n Line 2\n Line 3\n </p>");
166+
checkAndMatchSnapshot(page.locator("body"), "- paragraph: Line 1 Line 2 Line 3");
167+
assertThat(page.locator("body")).matchesAriaSnapshot(
168+
" - paragraph: |\n" +
169+
" Line 1\n" +
170+
" Line 2\n" +
171+
" Line 3");
172+
}
173+
174+
@Test
175+
void shouldConcatenateSpanText(Page page) {
176+
page.setContent("<span>One</span> <span>Two</span> <span>Three</span>");
177+
checkAndMatchSnapshot(page.locator("body"), "- text: One Two Three");
178+
}
179+
180+
@Test
181+
void shouldConcatenateSpanText2(Page page) {
182+
page.setContent("<span>One </span><span>Two </span><span>Three</span>");
183+
checkAndMatchSnapshot(page.locator("body"), "- text: One Two Three");
184+
}
185+
186+
@Test
187+
void shouldConcatenateDivTextWithSpaces(Page page) {
188+
page.setContent("<div>One</div><div>Two</div><div>Three</div>");
189+
checkAndMatchSnapshot(page.locator("body"), "- text: One Two Three");
190+
}
191+
192+
@Test
193+
void shouldIncludePseudoInText(Page page) {
194+
page.setContent(
195+
"<style>span:before { content: 'world'; } div:after { content: 'bye'; }</style>" +
196+
"<a href=\"about:blank\"><span>hello</span><div>hello</div></a>");
197+
checkAndMatchSnapshot(page.locator("body"),
198+
"- link \"worldhello hellobye\":\n" +
199+
" - /url: about:blank");
200+
}
201+
202+
@Test
203+
void shouldNotIncludeHiddenPseudoInText(Page page) {
204+
page.setContent(
205+
"<style>span:before { content: 'world'; display: none; } div:after { content: 'bye'; visibility: hidden; }</style>" +
206+
"<a href=\"about:blank\"><span>hello</span><div>hello</div></a>");
207+
checkAndMatchSnapshot(page.locator("body"),
208+
"- link \"hello hello\":\n" +
209+
" - /url: about:blank");
210+
}
211+
212+
@Test
213+
void shouldIncludeNewLineForBlockPseudo(Page page) {
214+
page.setContent(
215+
"<style>span:before { content: 'world'; display: block; } div:after { content: 'bye'; display: block; }</style>" +
216+
"<a href=\"about:blank\"><span>hello</span><div>hello</div></a>");
217+
checkAndMatchSnapshot(page.locator("body"),
218+
"- link \"world hello hello bye\":\n" +
219+
" - /url: about:blank");
220+
}
221+
222+
@Test
223+
void shouldWorkWithSlots(Page page) {
224+
// Text "foo" is assigned to the slot, should not be used twice.
225+
page.setContent(
226+
"<button><div>foo</div></button>" +
227+
"<script>(() => {" +
228+
" const container = document.querySelector('div');" +
229+
" const shadow = container.attachShadow({ mode: 'open' });" +
230+
" const slot = document.createElement('slot');" +
231+
" shadow.appendChild(slot);" +
232+
"})()</script>");
233+
checkAndMatchSnapshot(page.locator("body"), "- button \"foo\"");
234+
235+
// Text "foo" is assigned to the slot, should be used instead of slot content.
236+
page.setContent(
237+
"<div>foo</div>" +
238+
"<script>(() => {" +
239+
" const container = document.querySelector('div');" +
240+
" const shadow = container.attachShadow({ mode: 'open' });" +
241+
" const button = document.createElement('button');" +
242+
" shadow.appendChild(button);" +
243+
" const slot = document.createElement('slot');" +
244+
" button.appendChild(slot);" +
245+
" const span = document.createElement('span');" +
246+
" span.textContent = 'pre';" +
247+
" slot.appendChild(span);" +
248+
"})()</script>");
249+
checkAndMatchSnapshot(page.locator("body"), "- button \"foo\"");
250+
251+
// Nothing is assigned to the slot, should use slot content.
252+
page.setContent(
253+
"<div></div>" +
254+
"<script>(() => {" +
255+
" const container = document.querySelector('div');" +
256+
" const shadow = container.attachShadow({ mode: 'open' });" +
257+
" const button = document.createElement('button');" +
258+
" shadow.appendChild(button);" +
259+
" const slot = document.createElement('slot');" +
260+
" button.appendChild(slot);" +
261+
" const span = document.createElement('span');" +
262+
" span.textContent = 'pre';" +
263+
" slot.appendChild(span);" +
264+
"})()</script>");
265+
checkAndMatchSnapshot(page.locator("body"), "- button \"pre\"");
266+
}
267+
268+
@Test
269+
void shouldSnapshotInnerText(Page page) {
270+
page.setContent(
271+
"<div role=\"listitem\"><div><div><span title=\"a.test.ts\">a.test.ts</span></div>" +
272+
"<div><button title=\"Run\"></button><button title=\"Show source\"></button><button title=\"Watch\"></button></div></div></div>" +
273+
"<div role=\"listitem\"><div><div><span title=\"snapshot\">snapshot</span></div>" +
274+
"<div class=\"ui-mode-list-item-time\">30ms</div>" +
275+
"<div><button title=\"Run\"></button><button title=\"Show source\"></button><button title=\"Watch\"></button></div></div></div>");
276+
checkAndMatchSnapshot(page.locator("body"),
277+
" - listitem:\n" +
278+
" - text: a.test.ts\n" +
279+
" - button \"Run\"\n" +
280+
" - button \"Show source\"\n" +
281+
" - button \"Watch\"\n" +
282+
" - listitem:\n" +
283+
" - text: snapshot 30ms\n" +
284+
" - button \"Run\"\n" +
285+
" - button \"Show source\"\n" +
286+
" - button \"Watch\"");
287+
}
288+
289+
@Test
290+
void checkAriaHiddenText(Page page) {
291+
page.setContent("<p><span>hello</span><span aria-hidden=\"true\">world</span></p>");
292+
checkAndMatchSnapshot(page.locator("body"), "- paragraph: hello");
293+
}
294+
295+
@Test
296+
void shouldIgnorePresentationAndNoneRoles(Page page) {
297+
page.setContent("<ul><li role=\"presentation\">hello</li><li role=\"none\">world</li></ul>");
298+
checkAndMatchSnapshot(page.locator("body"), "- list: hello world");
299+
}
300+
301+
@Test
302+
void shouldNotUseOnAsCheckboxValue(Page page) {
303+
page.setContent("<input type=\"checkbox\"><input type=\"radio\">");
304+
checkAndMatchSnapshot(page.locator("body"), "- checkbox\n- radio");
305+
}
306+
307+
@Test
308+
void shouldNotReportTextareaTextContent(Page page) {
309+
page.setContent("<textarea>Before</textarea>");
310+
checkAndMatchSnapshot(page.locator("body"), "- textbox: Before");
311+
page.evaluate("document.querySelector('textarea').value = 'After'");
312+
checkAndMatchSnapshot(page.locator("body"), "- textbox: After");
313+
}
314+
315+
@Test
316+
void shouldNotShowVisibleChildrenOfHiddenElements(Page page) {
317+
page.setContent(
318+
"<div style=\"visibility: hidden;\">" +
319+
"<div style=\"visibility: visible;\"><button>Button</button></div>" +
320+
"</div>");
321+
assertEquals("", page.locator("body").ariaSnapshot());
322+
}
323+
324+
@Test
325+
void shouldNotShowUnhiddenChildrenOfAriaHiddenElements(Page page) {
326+
page.setContent(
327+
"<div aria-hidden=\"true\">" +
328+
"<div aria-hidden=\"false\"><button>Button</button></div>" +
329+
"</div>");
330+
assertEquals("", page.locator("body").ariaSnapshot());
331+
}
332+
333+
@Test
334+
void shouldSnapshotPlaceholderWhenDifferentFromName(Page page) {
335+
page.setContent("<input placeholder=\"Placeholder\">");
336+
assertThat(page.locator("body")).matchesAriaSnapshot("- textbox \"Placeholder\"");
337+
338+
page.setContent("<input placeholder=\"Placeholder\" aria-label=\"Label\">");
339+
assertThat(page.locator("body")).matchesAriaSnapshot(
340+
"- textbox \"Label\":\n" +
341+
" - /placeholder: Placeholder");
342+
}
343+
139344
}

0 commit comments

Comments
 (0)