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

Playwright Screenshots and Videos

Playwright Screenshots and Videos

This chapter explains how to take screenshots and record videos with Playwright, helping you quickly identify problems when tests fail.


page.screenshot() screenshots

Full-page screenshot

Example

// Save to file

await page.

screenshot

(

{

path

:

'screenshots/runoob-homepage.png'

}

)

;

// Get Buffer data (use directly, without writing a file)

const

buffer

=

await page.

screenshot

(

)

;

Screenshot options

Example

await page.

screenshot

(

{

  path

:

'screenshots/full-page.png'

,

  fullPage

:

true

,

         

// Capture the entire page (not just the viewport)

  type

:

'png'

,

             

// Format: 'png' (default) or 'jpeg'

  quality

:

90

,

             

// JPEG quality (0-100, JPEG only)

  clip

:

{

                 

// Clip region

    x

:

0

,

    y

:

0

,

    width

:

800

,

    height

:

600

,

 

}

,

  animations

:

'disabled'

,

 

// Disable CSS animations (more stable screenshots)

  omitBackground

:

false

,

   

// Whether to hide the default background

}

)

;

Element screenshot

Example

// Capture a screenshot of a specific element

await page.

getByRole

(

'navigation'

)

.

screenshot

(

{

path

:

'nav.png'

}

)

;

// Capture the button

await page.

getByRole

(

'button'

,

{

name

:

'Submit'

}

)

.

screenshot

(

{

path

:

'btn.png'

}

)

;


Automatic screenshot configuration

Set the automatic screenshot strategy in the configuration file, with no need to call it manually in every test:

Example

// File path: playwright.config.ts

export

default

defineConfig

(

{

  use

:

{

   

// Automatically screenshot only when a test fails

    screenshot

:

'only-on-failure'

,

   

// Possible values:

   

// 'off' - no screenshots

   

// 'on' - screenshot every test

   

// 'only-on-failure' - screenshot only on failure (recommended)

 

}

,

}

)

;

Failure screenshots will be saved in the test-results/ directory and displayed in the HTML report.


Video recording

Configuring video recording

Example

// File path: playwright.config.ts

export

default

defineConfig

(

{

  use

:

{

   

// Retain video only on failure

    video

:

'retain-on-failure'

,

   

// Possible values:

   

// 'off' - no recording

   

// 'on' - record every time

   

// 'retain-on-failure' - retain on failure, delete on pass (recommended)

   

// 'on-first-retry' - record only on the first retry

 

}

,

}

)

;

Manually controlling video

Example

test

(

'Manually save video'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

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

)

;

 

// Perform operations

  await page.

getByText

(

'RUNOOB Tutorial'

)

.

click

(

)

;

 

// Get the video object

 

const

video

=

page.

video

(

)

;

 

// Save the video to a specified path

  await video.

saveAs

(

'videos/runoob-test.mp4'

)

;

 

// Or delete the video

 

// await video.delete();

}

)

;

Video recording options

Example

// File path: playwright.config.ts

export

default

defineConfig

(

{

  use

:

{

    video

:

{

      mode

:

'retain-on-failure'

,

      size

:

{

width

:

1280

,

height

:

720

}

,

// Video size

   

}

,

 

}

,

}

)

;


Use cases for screenshots and videos

Scenario Recommended solution
Quickly understand page state when CI tests fail screenshot: 'only-on-failure'
Review the complete test execution process video: 'retain-on-failure'
Visual regression testing expect(page).toHaveScreenshot()
Debug specific steps Manual invocation page.screenshot()

Screenshots and videos increase test execution time and disk usage. It is recommended to use them only in production environments 'only-on-failure' or 'retain-on-failure', avoiding generating media files every time.