dragAndDrop action performs drag-and-drop operations by moving an element from a source location to a target location. It supports HTML5 drag-and-drop with automatic fallback mechanisms.
Basic Usage
Parameters
Source Element (Required)
The element to drag. Can be a simple string (text, selector, or regex pattern) or a detailed object with multiple finding criteria.
Simple Format
Whensource is a string, it can be:
- Display text of the element
- CSS selector
- Regex pattern (enclosed in forward slashes)
Detailed Format
Whensource is an object, you can use any of these properties:
CSS selector of the element. If combined with
elementText, the element must match both.Display text or regex pattern (enclosed in forward slashes) of the element. If combined with
selector, the element must match both.ID attribute of the element to find. Supports exact match or regex pattern using
/pattern/ syntax.data-testid attribute of the element to find. Supports exact match or regex pattern using /pattern/ syntax.Class or array of classes that the element must have. Each class supports exact match or regex pattern using
/pattern/ syntax. Element must have all specified classes.Object of attribute key-value pairs that the element must have. Values can be strings (supporting
/pattern/ regex), numbers, or booleans. Boolean true matches attribute presence, false matches absence.Computed accessible name of the element per ARIA specification. Supports exact match or regex pattern using
/pattern/ syntax.Maximum duration in milliseconds to wait for the element to exist.
Target Element (Required)
The target location to drop the element. Supports the same format and finding strategies as
source.target parameter supports all the same properties as source (selector, elementText, elementId, elementTestId, elementClass, elementAttribute, elementAria, timeout).
Drag Options
Duration of the drag operation in milliseconds. Controls how fast the drag animation occurs.
Examples
Basic Drag and Drop
Drag by text and drop to selector:Drag with Selectors
Use CSS selectors for both source and target:Drag with Combined Criteria
Combine text and selector for precise targeting:Drag with Custom Timeout
Wait longer for elements to appear:Drag with Regex Patterns
Use regex to match dynamic content:Drag with Multiple Regex Criteria
Combine regex patterns for flexible matching:How It Works
ThedragAndDrop action:
- Finds both the source and target elements using the specified criteria
- Attempts to perform the drag-and-drop using WebDriver.io’s native method
- Verifies the operation succeeded by checking if the source element moved
- If the native method fails silently, falls back to HTML5 drag-and-drop simulation
- Dispatches all necessary drag events:
dragstart,dragover,drop,dragend
The action includes automatic fallback mechanisms to ensure compatibility with different drag-and-drop implementations.
When to Use
UsedragAndDrop when:
- Testing drag-and-drop interfaces like page builders or dashboards
- Rearranging sortable lists or kanban boards
- Moving items between containers
- Testing file upload via drag-and-drop zones
- Validating drag-and-drop interactions in visual editors
Error Handling
The action fails when:- Source element is not found within the timeout period
- Target element is not found within the timeout period
- Element exists but is not draggable
- Both WebDriver.io and HTML5 fallback methods fail