Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Visual Regression Testing

Playwright Visual Regression Testing

This chapter introduces Playwright's visual regression testing feature, detecting unexpected UI changes through screenshot comparison.


What Is Visual Regression Testing

Visual regression testing (Visual Regression Testing) detects UI changes on a page by comparing pixel differences between the current screenshot and the baseline screenshot (Golden File).

When a CSS change unexpectedly affects the layout of other pages, visual regression testing can directly catch the issue.


expect(page).toHaveScreenshot() page screenshot comparison

Example

test

(

'RUNOOB Homepage Visual Regression'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

'https://www.runoob.com/'

)

;

 

// First run: save the baseline screenshot

 

// Subsequent runs: compare with the baseline screenshot

  await expect

(

page

)

.

toHaveScreenshot

(

'runoob-homepage.png'

)

;

}

)

;

On the first run, Playwright reports an error and generates the baseline screenshot:



Error: A snapshot doesn't exist at runoob-homepage-chromium-darwin.png,

writing actual.

On the second run, it automatically compares with the baseline screenshot.

Complete toHaveScreenshot Options

Example

await expect

(

page

)

.

toHaveScreenshot

(

'homepage.png'

,

{

  fullPage

:

true

,

               

// Full-page screenshot

  maxDiffPixelRatio

:

0.01

,

     

// Allow 1% pixel difference

  maxDiffPixels

:

100

,

           

// Allow up to 100 pixel differences

  threshold

:

0.2

,

               

// Pixel difference threshold (0~1)

  animations

:

'disabled'

,

       

// Disable animations (recommended)

  mask

:

[

                       

// Ignore specific areas

    page.

getByText

(

'Current Time'

)

,

 

]

,

}

)

;


expect(locator).toHaveScreenshot() element screenshot comparison

Example

test

(

'Navigation Bar Visual Regression'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

'https://www.runoob.com/'

)

;

 

// Only compare the navigation bar screenshot

 

const

navbar

=

page.

getByRole

(

'navigation'

)

;

  await expect

(

navbar

)

.

toHaveScreenshot

(

'navbar.png'

)

;

}

)

;


Golden File Management

Generate Golden File

On the first run of a screenshot assertion, Playwright automatically generates the Golden File and saves it in the directory next to the test file -snapshots directory:



tests/

├── example.spec.ts

└── example.spec.ts-snapshots/

    └── runoob-homepage-chromium-darwin.png

The filename includes the browser name and operating system, ensuring baselines for different platforms are managed independently.

Update Golden File

When the UI undergoes an expected design change, the baseline screenshot needs to be updated:



# Update all screenshot baselines

npx playwright test --update-snapshots



# Only update specific files

npx playwright test tests/example.spec.ts --update-snapshots


Visual Comparison Options in Detail

Option Type Description
maxDiffPixelRatio number Allowed ratio of pixel differences (0~1), default 0
maxDiffPixels number Allowed number of pixel differences, default 0
threshold number Tolerance for a single pixel's difference (0~1), default 0.2
animations string Whether to disable animations:'disabled' | 'allow'
mask Locator[] Areas to ignore (not considered during difference comparison)

CI Environment Considerations

Browser rendering results are affected by factors such as the operating system, browser version, hardware, power state, and headless mode. To ensure consistency, generate the Golden File and run tests in the same environment. It is recommended to use Docker containers or the same platform in CI to eliminate differences.

Using Visual Testing in Docker



# Use Playwright Docker image to ensure rendering consistency

docker run --rm -v $(pwd):/work -w /work \

  mcr.microsoft.com/playwright:v1.52.0-jammy \

  npx playwright test --update-snapshots


Best Practices for Visual Testing