/**
* 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 { test, expect } from './pageTest';
test.describe('containment matchers print full element subtree', () => {
test('toHaveText failure includes full element subtree', async ({ page }) => {
await page.setContent(``);
const error = await expect(page.locator('#node')).toHaveText('nope', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('heading "Title"');
expect(snapshot).toContain('paragraph');
expect(snapshot).toContain('Body');
});
test('toContainText failure includes full element subtree', async ({ page }) => {
await page.setContent(``);
const error = await expect(page.locator('#node')).toContainText('nope', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('heading "Title"');
expect(snapshot).toContain('Body');
});
});
test.describe('property matchers print only the element line', () => {
test('toBeChecked failure prints just the input', async ({ page }) => {
await page.setContent(` a checkbox `);
const error = await expect(page.locator('#cb')).toBeChecked({ timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('checkbox');
});
test('toHaveAttribute failure clips descendant subtree', async ({ page }) => {
await page.setContent(`
`);
const error = await expect(page.locator('#lst')).toHaveAttribute('data-x', 'yes', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('list');
expect(snapshot).toContain('listitem');
// Property matcher caps depth at 1 — content inside the listitem (depth 2) must not appear.
expect(snapshot).not.toContain('HeadingMarker');
expect(snapshot).not.toContain('BodyMarker');
});
test('toHaveRole failure prints just the element line', async ({ page }) => {
await page.setContent(`Hinested `);
const error = await expect(page.locator('#btn')).toHaveRole('link', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('button');
});
test('toHaveValue failure prints the input element', async ({ page }) => {
await page.setContent(` `);
const error = await expect(page.locator('#inp')).toHaveValue('expected', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('textbox');
});
test('toHaveCSS failure prints the element line', async ({ page }) => {
await page.setContent(`Press `);
const error = await expect(page.locator('#btn')).toHaveCSS('color', 'rgb(0, 0, 0)', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('button');
});
});
test.describe('hidden or missing elements print full page snapshot', () => {
test('toBeVisible on hidden element prints full page snapshot', async ({ page }) => {
await page.setContent(`
secret
Page Heading
`);
const error = await expect(page.locator('#hidden')).toBeVisible({ timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
// Page-wide context, not the empty hidden element snapshot.
expect(snapshot).toContain('heading "Page Heading"');
});
test('toBeVisible on missing element prints full page snapshot', async ({ page }) => {
await page.setContent(``);
const error = await expect(page.locator('#nope')).toBeVisible({ timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('heading "Hello"');
});
test('toHaveText on missing element prints full page snapshot', async ({ page }) => {
await page.setContent(`Hello `);
const error = await expect(page.locator('#missing')).toHaveText('x', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('heading "Hello"');
});
test('toHaveTitle failure prints full page snapshot', async ({ page }) => {
await page.setContent(`Right Body Heading `);
const error = await expect(page).toHaveTitle('Wrong', { timeout: 1000 }).catch(e => e);
const snapshot = error.matcherResult.ariaSnapshot;
expect(snapshot).toContain('heading "Body Heading"');
});
});
test.describe('matchers that should not include an aria snapshot', () => {
test('toHaveCount failure has no aria snapshot', async ({ page }) => {
await page.setContent(``);
const error = await expect(page.locator('li')).toHaveCount(5, { timeout: 1000 }).catch(e => e);
expect(error.matcherResult.ariaSnapshot).toBeUndefined();
});
test('toHaveText with array has no aria snapshot', async ({ page }) => {
await page.setContent(``);
const error = await expect(page.locator('li')).toHaveText(['a', 'b', 'c'], { timeout: 1000 }).catch(e => e);
expect(error.matcherResult.ariaSnapshot).toBeUndefined();
});
test('toMatchAriaSnapshot failure has no extra aria snapshot', async ({ page }) => {
await page.setContent(`Y `);
const error = await expect(page.locator('#btn')).toMatchAriaSnapshot(`- button "X"`, { timeout: 1000 }).catch(e => e);
expect(error.matcherResult.ariaSnapshot).toBeUndefined();
});
});