Drag & Drop
The browser's built-in drag and drop, a sortable list like the ones drag libraries build, and a Kanban board.
Built-in drag and drop: dragTo()
Elements with the draggable attribute use the browser's own drag events. dragTo() handles them:
await page.getByTestId('drag-source').dragTo(page.getByTestId('drop-target'));Drag libraries need real movement
Libraries such as dnd-kit and SortableJS listen to pointer moves instead. Most start a drag only after the pointer has moved a few pixels, then track where it is on later moves. dragTo() jumps from source to target in one move, so the library starts the drag and never sees where it went. Drive the mouse yourself, in steps:
const list = page.getByRole('list', { name: 'Priorities' });
const handle = list.getByRole('button', { name: 'Reorder Deploy' });
const target = list.getByRole('listitem').first();
await handle.hover();
await page.mouse.down();
const box = (await target.boundingBox())!;
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2, { steps: 10 });
await page.mouse.up();
await expect(list.getByRole('listitem')).toHaveText(['Deploy', 'Write tests', /* … */]);Or use the keyboard
Accessible drag libraries also support the keyboard: Space picks an item up, the arrow keys move it, Space drops it. It tests the same feature without any coordinates:
const handle = list.getByRole('button', { name: 'Reorder Deploy' });
await handle.press('Space');
await handle.press('ArrowUp');
await page.keyboard.press('Space');Built-in drag and drop
Drag and drop not completed
Sortable list
Drag a row to reorder it. Or Tab to its handle and press Space, the arrow keys, then Space again.
- Write tests
- Review PR
- Fix flaky test
- Update docs
- Deploy
Nothing moved yet
Kanban board
To do (2)
- Design login page
- Write release notes
In progress (1)
- Set up CI
Done (1)
- Plan sprint
Related exercises
- Mouse & KeyboardIntermediateMouse ActionsUse the low-level mouse: modifier clicks, positions, drawing, the wheel and a slider.
- FormsIntermediateFile UploadUpload files without the operating system dialog: plain inputs, checked files, hidden inputs, drop zones and a real upload.
- Mouse & KeyboardBeginnerKeyboard ActionsDrive the page with the keyboard alone: typing, Tab order, shortcuts and held keys.
Read the guide: Playwright cheat sheet: locators, actions and assertions