[Playwright Interview question #19]: Làm thế nào để test upload/download file?

0 0 0

Người đăng: Playwright Việt Nam

Theo Viblo Asia

Câu hỏi phỏng vấn #19: Làm thế nào để test upload/download file?

Trả lời mẫu:

1. File upload:

// Method 1: Direct input
await page.locator('input[type="file"]').setInputFiles('path/to/file.pdf'); // Method 2: Multiple files
await page.locator('input[type="file"]').setInputFiles([ 'file1.png', 'file2.jpg'
]); // Method 3: File chooser
const [fileChooser] = await Promise.all([ page.waitForEvent('filechooser'), page.click('#upload-btn')
]);
await fileChooser.setFiles('document.pdf');

2. File download:

// Wait for download
const [download] = await Promise.all([ page.waitForEvent('download'), page.click('#download-btn')
]); // Save to specific path
await download.saveAs('downloads/file.pdf'); // Get download info
console.log(download.suggestedFilename());
console.log(download.url());

3. Download với custom path:

const context = await browser.newContext({ acceptDownloads: true, downloadsPath: './test-downloads'
});

4. Verify file content:

// Read downloaded file
const download = await page.waitForEvent('download');
const path = await download.path();
const content = await fs.readFile(path, 'utf-8');
expect(content).toContain('expected text');

5. Buffer/stream handling:

// Create download stream
const download = await page.waitForEvent('download');
const stream = await download.createReadStream();
// Process stream...

💡Tips:

  • Clear input trước khi upload: setInputFiles([])
  • Test drag-and-drop upload với page.dragAndDrop()
  • Verify upload progress nếu có progress bar
  • Cleanup downloaded files sau test

Lời Kết

Playwright đang trở thành một trong những automation frameworks phổ biến nhất cho web testing. Thông qua series này, hy vọng bạn sẽ:

  • Nắm vững kiến thức từ cơ bản đến nâng cao
  • Tự tin trong các buổi phỏng vấn
  • Áp dụng hiệu quả vào dự án thực tế
  • Trở thành một phần của cộng đồng Playwright Việt Nam năng động

📚 Bắt đầu hành trình của bạn với: Bài 1: Playwright vs Selenium

💬 Có câu hỏi? Tham gia group Facebook của chúng mình!

Theo dõi series để không bỏ lỡ bài viết mới!

Bình luận

Bài viết tương tự

- vừa được xem lúc

Lỗ hổng File Upload

1. FILE UPLOAD.

0 0 23

- vừa được xem lúc

File upload vulnerabilities (Phần 3)

II. Phân tích và khai thác các lỗ hổng File upload (tiếp). 6. Bypass bằng cách chèn metadata trong file ảnh.

0 0 34

- vừa được xem lúc

File upload vulnerabilities (Phần 2)

II. Phân tích và khai thác các lỗ hổng File upload (tiếp). 3. Kết hợp kỹ thuật path traversal.

0 0 54

- vừa được xem lúc

File upload vulnerabilities (Phần 1)

I. Đặt vấn đề. 1. Giới thiệu lỗ hổng File upload.

0 0 17

- vừa được xem lúc

Sử dụng Playwright để crawl dữ liệu - Phần 1

Nhân dịp đầu xuân, chúc anh chị em Viblo dồi dào sức khỏe, mã đáo thành công ạ ^^. Xin chào mọi người, hôm nay mình xin chia sẻ cách sử dụng Playwright - 1 framework cho testing mới ra lò cách đây.

0 0 47

- vừa được xem lúc

End-to-End Testing Renec Chain - Tương tác ví Demon - Playwright

In the fast-paced world of software development, ensuring the reliability and robustness of your blockchain-based applications is crucial. One way to achieve this is through end-to-end (E2E) testing.

0 0 38