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

Playwright File Operations

Playwright File Operations

Although Playwright is mainly for browser automation, it is alsopowerful in file upload and downloadand can easily handle common front-end file interaction scenarios.


File upload

In web forms, file uploads are usually done via . Playwright provides dedicated APIs to handle this:

Method 1: setInputFiles()

await page.setInputFiles('input[type="file"]', 'tests/fixtures/avatar.png');

- Selector: how the file input box is located. - File path: relative/absolute path, supporting a single file or an array of multiple files.

Method 2: Directly via Locator



const upload = page.locator('input[type=file]');

await upload.setInputFiles(['tests/fixtures/1.png', 'tests/fixtures/2.png']);

Method 3: Simulate drag-and-drop upload (supported by some applications)



const fileChooserPromise = page.waitForEvent('filechooser');

await page.click('button#upload'); // 触发选择文件

const fileChooser = await fileChooserPromise;

await fileChooser.setFiles('tests/fixtures/test.pdf');


File Download

Playwright can capture browser download events and obtain the file save path.

Basic usage: listening for downloads

Example

const

[

download

]

=

await Promise.

all

(

[

  page.

waitForEvent

(

'download'

)

,

         

// Wait for the download to start

  page.

click

(

'a#downloadButton'

)

,

       

// Trigger the download

]

)

;

// Save the file to a specified path

await download.

saveAs

(

'downloads/report.pdf'

)

;

// Get the default save path (system temp directory)

const

path

=

await download.

path

(

)

;

console.

log

(

'Download path:'

,

path

)

;

// Get download information

console.

log

(

await download.

url

(

)

)

;

       

// Download URL

console.

log

(

await download.

suggestedFilename

(

)

)

;

// Suggested filename


Screenshot and Recording Files

Playwright itself also generates files (screenshots, videos, trace, etc.):

await page.screenshot({ path: 'screenshots/home.png', fullPage: true });

await page.video()?.saveAs('videos/test.mp4');  // 在配置中开启 video 记录才可用


Complete File Operations Example

Example

import

{

test

,

expect

}

from

'@playwright/test'

;

test

(

'File Upload and Download Example'

,

async

(

{

page

}

)

=>

{

 

// Upload a file

  await page.

goto

(

'https://the-internet.herokuapp.com/upload'

)

;

 

const

uploadInput

=

page.

locator

(

'input[type=file]'

)

;

  await uploadInput.

setInputFiles

(

'tests/fixtures/avatar.png'

)

;

  await page.

click

(

'#file-submit'

)

;

  await expect

(

page.

locator

(

'#uploaded-files'

)

)

.

toHaveText

(

'avatar.png'

)

;

 

// Download a file

  await page.

goto

(

'https://the-internet.herokuapp.com/download'

)

;

 

const

[

download

]

=

await Promise.

all

(

[

    page.

waitForEvent

(

'download'

)

,

    page.

click

(

'a[href="some-file.txt"]'

)

,

 

]

)

;

  await download.

saveAs

(

'downloads/some-file.txt'

)

;

  console.

log

(

'Download complete:'

,

await download.

suggestedFilename

(

)

)

;

}

)

;


File Operations API

API Purpose Parameter/Description
page.setInputFiles(selector, files) Upload files files Can be a path string, an array, or FilePayload
locator.setInputFiles(files) Upload a file (Locator method)
page.waitForEvent('filechooser') Wait for the file chooser dialog Return FileChooser
fileChooser.setFiles(files) Set the selected files
page.waitForEvent('download') Listen for download events Return Download
download.saveAs(path) Save the downloaded file Custom storage location
download.path() Get the system default storage path
download.url() Get the URL of the download request
download.suggestedFilename() Get the suggested filename
page.screenshot({ path }) Page screenshot
locator.screenshot({ path }) Element screenshot
page.video()?.saveAs(path) Save the recorded video Requires video to be enabled in config