This formal example select file but does not send Form data.
https://playwright.dev/docs/api/class-filechooser
// Start waiting for file chooser before clicking. Note no await.
const fileChooserPromise = page.waitForEvent('filechooser');
await page.getByText('Upload file').click();
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(path.join(__dirname, 'myfile.pdf'));
- make
Buffer
object. ( e.g usefs
and read local file ) - At
setFiles
specify thatBuffer
in argument.
import fs from 'fs'
test('UPLOAD SAMPLE', async ({ page }) => {
const fileBuffer = fs.readFileSync('/path/to/example.csv')
await page.getByText('Upload file').click()
const fileChooserPromise = page.waitForEvent('filechooser')
const fileChooser = await fileChooserPromise
await fileChooser.setFiles({
name: 'filename',
mimeType: 'xxx',
buffer: fileBuffer,
})
})