Playwright Hover and Drag Drop
Pointing a finger at a menu item reveals its description, and carrying a book from one shelf to another changes where it lives. Websites respond to both gestures. Playwright reproduces them with the hover and dragTo methods, so tests can open hidden menus and move items across a screen.
The Hover Action
Many sites hide extra options until the mouse rests on a heading. A dropdown navigation bar is a common case. The hover method moves the pointer onto the target and holds it there.
await page.getByText('Products').hover();
await page.getByRole('link', { name: 'Laptops' }).click();
Hover Flow Diagram
Menu closed Pointer arrives Submenu opens
+----------+ +----------+ +----------+
| Products | hover | Products | visible | Products |
+----------+ ---> +----------+ ---> | Laptops |
| Phones |
+----------+
|
click Laptops
Checking Tooltips
A tooltip appears after the pointer rests on an icon for a moment. A test hovers over the icon and asserts that the tooltip text shows up.
await page.getByTestId('help-icon').hover();
await expect(page.getByRole('tooltip')).toHaveText('Need help?');
The Drag and Drop Shortcut
The dragTo method picks up one element and drops it on another. Playwright waits until both elements are ready before it starts.
const card = page.getByText('Write report');
const doneColumn = page.getByTestId('column-done');
await card.dragTo(doneColumn);
await expect(doneColumn).toContainText('Write report');
Drag Diagram
To Do Column In Progress Done Column
+-----------+ +-----------+ +-----------+
| [Report] | ----------------------> | |
| [Email] | | | | [Report] |
+-----------+ +-----------+ +-----------+
pick up drop here
Manual Drag With the Mouse
Some widgets, such as sliders and custom sortable lists, ignore the shortcut. A manual sequence of mouse steps gives full control.
const box = await page.getByTestId('slider-handle').boundingBox();
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
await page.mouse.down();
await page.mouse.move(box.x + 150, box.y + box.height / 2, { steps: 10 });
await page.mouse.up();
The steps option moves the pointer in small increments. Many widgets need those in-between movements to notice the drag.
A Real-World Scenario
A project board app lets teams move task cards between columns. A test drags the card named "Write report" from "To Do" into "Done" and confirms the column count changes from 2 to 1. A second check reloads the page and verifies the card stays in its new column, which proves the app saved the move.
Helpful Tips
- Hover before clicking items inside a menu that opens on mouse over.
- Assert the final result after a drag, never only the gesture.
- Use manual mouse steps for sliders and canvas-based tools.
- Scroll the target into view when a page is long.
Quick Practice Task
Find a public demo page with a sortable list. Write a test that drags the first item below the third item and asserts the new order of the list.
Common Questions
Why does hover sometimes fail to open a menu?
Some menus open only after the pointer rests for a moment, and others close when the pointer moves away. Check the final visible state with an assertion after the hover.
Can a test drag between two browser tabs?
No. Drag operations work inside one page. Use the manual mouse steps for custom widgets on that page.
What if dragTo does nothing?
Libraries that listen for low-level mouse events ignore the shortcut. Switch to the manual sequence with mouse.down, several mouse.move steps, and mouse.up.
Key Takeaways
- The
hovermethod reveals menus and tooltips. - The
dragTomethod moves one element onto another. - Manual mouse steps handle stubborn sliders and custom widgets.
- Every gesture needs a check of the final visible result.
