Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

6.5 KB
Raw Blame
/**
 * 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('EX-313 toHaveText failure includes full element subtree', async ({ page }) => {
    await page.setContent(`<section id=node><h1>Title</h1><p>Body</p></section>`);
    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('EX-314 toContainText failure includes full element subtree', async ({ page }) => {
    await page.setContent(`<section id=node><h1>Title</h1><p>Body</p></section>`);
    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('EX-315 toBeChecked failure prints just the input', async ({ page }) => {
    await page.setContent(`<label><input id=cb type=checkbox> a checkbox</label>`);
    const error = await expect(page.locator('#cb')).toBeChecked({ timeout: 1000 }).catch(e => e);
    const snapshot = error.matcherResult.ariaSnapshot;
    expect(snapshot).toContain('checkbox');
  });

  test('EX-316 toHaveAttribute failure clips descendant subtree', async ({ page }) => {
    await page.setContent(`<ul id=lst><li><h2>HeadingMarker</h2><p>BodyMarker</p></li></ul>`);
    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('EX-317 toHaveRole failure prints just the element line', async ({ page }) => {
    await page.setContent(`<button id=btn>Hi<span>nested</span></button>`);
    const error = await expect(page.locator('#btn')).toHaveRole('link', { timeout: 1000 }).catch(e => e);
    const snapshot = error.matcherResult.ariaSnapshot;
    expect(snapshot).toContain('button');
  });

  test('EX-318 toHaveValue failure prints the input element', async ({ page }) => {
    await page.setContent(`<input id=inp value="actual">`);
    const error = await expect(page.locator('#inp')).toHaveValue('expected', { timeout: 1000 }).catch(e => e);
    const snapshot = error.matcherResult.ariaSnapshot;
    expect(snapshot).toContain('textbox');
  });

  test('EX-319 toHaveCSS failure prints the element line', async ({ page }) => {
    await page.setContent(`<button id=btn style="color: red">Press</button>`);
    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('EX-320 toBeVisible on hidden element prints full page snapshot', async ({ page }) => {
    await page.setContent(`
      <div id=hidden style="display: none"><span>secret</span></div>
      <main><h1>Page Heading</h1></main>
    `);
    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('EX-321 toBeVisible on missing element prints full page snapshot', async ({ page }) => {
    await page.setContent(`<header><h1>Hello</h1></header>`);
    const error = await expect(page.locator('#nope')).toBeVisible({ timeout: 1000 }).catch(e => e);
    const snapshot = error.matcherResult.ariaSnapshot;
    expect(snapshot).toContain('heading "Hello"');
  });

  test('EX-322 toHaveText on missing element prints full page snapshot', async ({ page }) => {
    await page.setContent(`<main><h1>Hello</h1></main>`);
    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('EX-323 toHaveTitle failure prints full page snapshot', async ({ page }) => {
    await page.setContent(`<title>Right</title><main><h1>Body Heading</h1></main>`);
    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('EX-324 toHaveCount failure has no aria snapshot', async ({ page }) => {
    await page.setContent(`<ul><li>a</li><li>b</li></ul>`);
    const error = await expect(page.locator('li')).toHaveCount(5, { timeout: 1000 }).catch(e => e);
    expect(error.matcherResult.ariaSnapshot).toBeUndefined();
  });

  test('EX-325 toHaveText with array has no aria snapshot', async ({ page }) => {
    await page.setContent(`<ul><li>x</li><li>y</li></ul>`);
    const error = await expect(page.locator('li')).toHaveText(['a', 'b', 'c'], { timeout: 1000 }).catch(e => e);
    expect(error.matcherResult.ariaSnapshot).toBeUndefined();
  });

  test('EX-326 toMatchAriaSnapshot failure has no extra aria snapshot', async ({ page }) => {
    await page.setContent(`<button id=btn>Y</button>`);
    const error = await expect(page.locator('#btn')).toMatchAriaSnapshot(`- button "X"`, { timeout: 1000 }).catch(e => e);
    expect(error.matcherResult.ariaSnapshot).toBeUndefined();
  });
});