Mastering Snapshot Testing with Playwright

HOME

import { test, expect } from '@playwright/test';

test('snapshot test example', async ({ page }) => {
    
    // Navigate to the page you want to test
     await page.goto('https://playwright.dev/');

     // Capture a snapshot of the whole page
     const screenshot = await page.screenshot();

     // Compare the screenshot to a stored baseline snapshot
     expect(screenshot).toMatchSnapshot('login_page.png');
  
    });

npx playwright test snapshot_fullpage_tests.spec.ts

import { test, expect } from '@playwright/test';

test('snapshot test example', async ({ page }) => {

    // Navigate to the page you want to test
    await page.goto('https://playwright.dev/');

    // Select the specific UI component using a locator method
    const component = await page.locator('//*[@id="__docusaurus_skipToContent_fallback"]/header/div/h1/span'); 
    
    // Capture a snapshot of a component in the page
    const screenshot = await component.screenshot();

    // Compare the screenshot to a stored baseline snapshot
    expect(screenshot).toMatchSnapshot('Title.png');

});

Leave a comment