LoginSignup
1
1

PlayWrightを用いたファイルアップロード,

Last updated at Posted at 2023-05-23

公式ドキュメント

実装例

ローカルのレポジトリに含まれているファイルをアップロードする

fileupload.ts
import { chromium } from 'playwright'

describe('upload file', () => {
    
    const filePathA = '../videos/a.webm':
    const filePathB = '../videos/b.webm';

 test('upload file', async () => {
    const browser = await chromium.lanch()
    const context = await browser.newContext()
    const page = await context.newPage()
    await page.goto(url)
     //複数投稿可能であればArrayにしてテスト可能
     //setInputFilesを使う
    await page.setInputFiles('input[name="file"]', [filepathA, filepathB])
}

こんなやり方も

import { chromium } from 'playwright'

describe('upload file', () => {
    
    const filePathA = '../videos/a.webm':
    const filePathB = '../videos/b.webm';

  test('upload file', async () => {
    const browser = await chromium.lanch()
    const context = await browser.newContext()
    const page = await context.newPage()
    await page.goto(url)
    //filechooserを使う
    page.on('filechooser', async (filechooser) => {
        await filechooser.setFiles([filePathA, filePathB])
    }
    await page.click("#fileUpload", {force: true})

参考youtube

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1