playwright/tests/page/to-match-aria-snapshot.spec.ts

192 lines
4.8 KiB
TypeScript

/**
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { stripAnsi } from 'tests/config/utils';
import { test, expect } from './pageTest';
test('should match', async ({ page }) => {
await page.setContent(`<h1>title</h1>`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "title"
`);
});
test('should match in list', async ({ page }) => {
await page.setContent(`
<h1>title</h1>
<h1>title 2</h1>
`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "title"
`);
});
test('should match list with accessible name', async ({ page }) => {
await page.setContent(`
<ul aria-label="my list">
<li>one</li>
<li>two</li>
</ul>
`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- list "my list":
- listitem: one
- listitem: two
`);
});
test('should match deep item', async ({ page }) => {
await page.setContent(`
<div>
<h1>title</h1>
<h1>title 2</h1>
</div>
`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "title"
`);
});
test('should match complex', async ({ page }) => {
await page.setContent(`
<ul>
<li>
<a href='about:blank'>link</a>
</li>
</ul>
`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- list:
- listitem:
- link "link"
`);
});
test('should match regex', async ({ page }) => {
await page.setContent(`<h1>Issues 12</h1>`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading /Issues \\d+/
`);
});
test('should allow text nodes', async ({ page }) => {
await page.setContent(`
<h1>Microsoft</h1>
<div>Open source projects and samples from Microsoft</div>
`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "Microsoft"
- text: Open source projects and samples from Microsoft
`);
});
test('details visibility', async ({ page, browserName }) => {
await page.setContent(`
<details>
<summary>Summary</summary>
<div>Details</div>
</details>
`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- group: Summary
`);
});
test('integration test', async ({ page, browserName }) => {
await page.setContent(`
<h1>Microsoft</h1>
<div>Open source projects and samples from Microsoft</div>
<ul>
<li>
<details>
<summary>
Verified
</summary>
<div>
<div>
<p>
We've verified that the organization <strong>microsoft</strong> controls the domain:
</p>
<ul>
<li class="mb-1">
<strong>opensource.microsoft.com</strong>
</li>
</ul>
<div>
<a href="about: blank">Learn more about verified organizations</a>
</div>
</div>
</div>
</details>
</li>
<li>
<a href="about:blank">
<summary title="Label: GitHub Sponsor">Sponsor</summary>
</a>
</li>
</ul>`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "Microsoft"
- text: Open source projects and samples from Microsoft
- list:
- listitem:
- group: Verified
- listitem:
- link "Sponsor"
`);
});
test('integration test 2', async ({ page }) => {
await page.setContent(`
<div>
<header>
<h1>todos</h1>
<input placeholder="What needs to be done?">
</header>
</div>`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "todos"
- textbox "What needs to be done?"
`);
});
test('expected formatter', async ({ page }) => {
await page.setContent(`
<div>
<header>
<h1>todos</h1>
<input placeholder="What needs to be done?">
</header>
</div>`);
const error = await expect(page.locator('body')).toMatchAriaSnapshot(`
- heading "todos"
- textbox "Wrong text"
`, { timeout: 1 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`- Expected - 3
+ Received string + 3
-
+ - :
+ - banner:
- heading "todos"
- - textbox "Wrong text"
-
+ - textbox "What needs to be done?"`);
});