/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* 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 type { Locator } from '@playwright/test';
import { test as it, expect } from './pageTest';
function unshift(snapshot: string): string {
const lines = snapshot.split('\n');
let whitespacePrefixLength = 100;
for (const line of lines) {
if (!line.trim())
continue;
const match = line.match(/^(\s*)/);
if (match && match[1].length < whitespacePrefixLength)
whitespacePrefixLength = match[1].length;
}
return lines.filter(t => t.trim()).map(line => line.substring(whitespacePrefixLength)).join('\n');
}
async function checkAndMatchSnapshot(locator: Locator, snapshot: string) {
expect.soft(await locator.ariaSnapshot()).toBe(unshift(snapshot));
await expect.soft(locator).toMatchAriaSnapshot(snapshot);
}
it('should snapshot', async ({ page }) => {
await page.setContent(`
`);
await checkAndMatchSnapshot(page.locator('body'), `
- heading "Microsoft" [level=1]
- text: Open source projects and samples from Microsoft
- list:
- listitem:
- group: Verified
- listitem:
- link "Sponsor"
`);
});
it('should support multiline text', async ({ page }) => {
await page.setContent(`
Line 1
Line 2
Line 3
`);
await checkAndMatchSnapshot(page.locator('body'), `
- paragraph: Line 1 Line 2 Line 3
`);
await expect(page.locator('body')).toMatchAriaSnapshot(`
- paragraph: |
Line 1
Line 2
Line 3
`);
});
it('should concatenate span text', async ({ page }) => {
await page.setContent(`
OneTwoThree
`);
await checkAndMatchSnapshot(page.locator('body'), `
- text: One Two Three
`);
});
it('should concatenate span text 2', async ({ page }) => {
await page.setContent(`
One Two Three
`);
await checkAndMatchSnapshot(page.locator('body'), `
- text: One Two Three
`);
});
it('should concatenate div text with spaces', async ({ page }) => {
await page.setContent(`
One
Two
Three
`);
await checkAndMatchSnapshot(page.locator('body'), `
- text: One Two Three
`);
});
it('should include pseudo in text', async ({ page }) => {
await page.setContent(`
hello
`);
await checkAndMatchSnapshot(page.locator('body'), `
- link "world hello hello bye"
`);
});
it('should work with slots', async ({ page }) => {
// Text "foo" is assigned to the slot, should not be used twice.
await page.setContent(`
`);
await checkAndMatchSnapshot(page.locator('body'), `
- button "foo"
`);
// Text "foo" is assigned to the slot, should be used instead of slot content.
await page.setContent(`
foo
`);
await checkAndMatchSnapshot(page.locator('body'), `
- button "foo"
`);
// Nothing is assigned to the slot, should use slot content.
await page.setContent(`
`);
await checkAndMatchSnapshot(page.locator('body'), `
- button "pre"
`);
});
it('should snapshot inner text', async ({ page }) => {
await page.setContent(`
`);
// - Different from Chrome DevTools which attributes ownership to the last element.
// - CDT also does not include non-owned children in accessible name.
// - Disregarding these as aria-owns can't suggest multiple parts by spec.
await checkAndMatchSnapshot(page.locator('body'), `
- link "Link 1 Value Paragraph":
- region: Link 1
- textbox: Value
- paragraph: Paragraph
- link "Link 2 Value Paragraph":
- region: Link 2
`);
});
it('should be ok with circular ownership', async ({ page }) => {
await page.setContent(`