Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active February 22, 2024 17:21
Show Gist options
  • Save YumaInaura/b6e90b919c19ff4753ec04f20dd8838f to your computer and use it in GitHub Desktop.
Save YumaInaura/b6e90b919c19ff4753ec04f20dd8838f to your computer and use it in GitHub Desktop.
Playwright – select file and send formData

Playwright – select file and send formData

Trouble

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'));

Resolve Example

  • make Buffer object. ( e.g use fs and read local file )
  • At setFiles specify that Buffer 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,
  })

})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment