🤯 Playwright Challenge: Simulating File Drag-and-Drop WITHOUT <input type="file">!
Ever faced a modern web app where file uploads rely purely on drag-and-drop zones, leaving locator.setInputFiles() useless? It's a common automation hurdle.
📌 Problem Statement
Many modern web apps use drag-and-drop for file uploads, skipping <input type="file">. Playwright's setInputFiles() is useless here. Instead, these UIs depend on native dragenter, dragover, and drop DOM events, utilizing the DataTransfer object.
❌ locator.setInputFiles() fails when no direct <input type="file">.
• Requires simulating native browser events to trigger the application's JavaScript.
💡 Solution & Code Walkthrough
We simulate native drag-and-drop: prepare file data, dispatch dragover (crucial for enabling drops via event.preventDefault()), then dispatch drop with the file payloads.
import { Page, expect } from '@playwright/test';
import path from 'path';
import fs from 'fs';
/** Simulates file drag-and-drop onto a target element without <input type="file">. */
async function simulateFileDragAndDrop(page: Page, selector: string, filePaths: string[]) {
const dropTarget = await page.locator(selector);
await expect(dropTarget, `'${selector}' not found`).toBeVisible(); // Ensure target visible
const files = filePaths.map(filePath => ({
name: path.basename(filePath),
mimeType: 'application/octet-stream', // Generic
buffer: fs.readFileSync(filePath),
}));
// Crucial: Dispatch 'dragover'. App's JS calls event.preventDefault() here.
await dropTarget.dispatchEvent('dragover', { dataTransfer: { files } });
// Dispatch 'drop' event with file data
await dropTarget.dispatchEvent('drop', { dataTransfer: { files } });
}
// Usage: await simulateFileDragAndDrop(page, '#drop-zone', ['path/to/file.txt']);
🔑 Key Takeaways
✅ page.dispatchEvent() enables custom event simulation.
✅ dragover with preventDefault() is vital to enable drop events.
✅ drop event's dataTransfer carries file contents.
• Indispensable for modern, component-driven UIs.
❓ Quick Summary Q&A
Q: Why can't locator.setInputFiles() be used?
A: No direct <input type="file"> element.
Q: Which events are critical for simulation?
A: dragover (allows drops) and drop (delivers files).
Q: How does file data get transferred?
A: Via dataTransfer.files in the drop event.
TAGS: playwright, typescript, e2e testing, automation, drag and drop, web testing
────────────────────────────────────────
Get more expert insights and advanced testing strategies!
────────────────────────────────────────
📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:
🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261109
🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261109&mt=8
────────────────────────────────────────
────────────────────────────────────────
Top comments (0)