dragAndDrop
The dragAndDrop action allows you to drag an element and drop it at a target location. This is useful for testing drag-and-drop interfaces, reordering lists, moving items between containers, and other interactive UI elements.
The dragAndDrop action requires both a source and a target element. Specify each with element text, a CSS or XPath selector, or a detailed object carrying more configuration options.
To move between points instead of elements, use swipe, the movement subset of this action. That covers carousels, sliders, and plain scrolling. dragAndDrop locates two elements and moves between them, while swipe moves between two coordinates or a direction shorthand.
Drag-and-drop has many different implementations, and Doc Detective wants to support them all. If you find that your specific use case isn’t working, don’t give up. Please open an issue or start a thread on our Discord.
Use cases
The dragAndDrop action is particularly useful for testing:
- Kanban boards: Moving tasks between different status columns
- File uploads: Dragging files from desktop to upload areas
- List reordering: Changing the order of items in sortable lists
- Dashboard widgets: Rearranging components on customizable dashboards
- Form builders: Moving form elements between different sections
- Shopping carts: Adding items by dragging them to the cart area
Properties
The dragAndDrop action requires an object with the following properties:
source: (Required) The element to drag from. Can be a string (element text or selector) or a detailed object.target: (Required) The element to drop onto. Can be a string (element text or selector) or a detailed object.duration: (Optional) Duration of the drag in milliseconds.0or more. Default is1000. A0drags instantly.surface: (Optional) The browser (session, window, or tab) the source and target elements live in, for example{ "browser": "chrome", "tab": "board" }. Omit to act on the active tab. See Test across multiple tabs, windows, and browsers.
Source and target element properties
When using detailed objects for source or target, you can specify:
elementText: (Optional) Display text of the element. If combined withselector, the element must match both the text and the selector.selector: (Optional) CSS selector or XPath selector of the element. If combined withelementText, the element must match both the text and the selector.timeout: (Optional) Maximum duration in milliseconds to wait for this specific element to exist. Default is5000.0checks once.
Outputs: the action exposes outputs.sourceFound and outputs.targetFound, booleans indicating whether the source and target elements were located. To capture them for later steps, use the step-level variables object with the expressions $$sourceFound and $$targetFound. When a browser drag misses its source or target, outputs.nearMatches lists the visible elements closest to the missing element, as it does for find. When both elements are missing, nearMatches reflects only the source. When the list isn’t empty, the step’s resultHint names them, and its resultCode is drift. Otherwise, its resultCode is assertion.
For comprehensive options, see the full
dragAndDropreference.
Examples
The docs tests run every example below except the browser-tab example, which needs a named tab the tests don’t open.
Drag and drop using selectors or element text
Drag and drop with detailed element objects
Set the drag duration
Set duration to control how long the drag takes, in milliseconds (default 1000). A longer drag gives animated or physics-based interfaces time to register the move.
Match the source or target with a regular expression
A string source or target matches a substring by regular expression when you wrap it in slashes. This targets an element whose text carries a variable part.
Target elements by class, attribute, or other criteria
A detailed source or target object accepts the same element-finding fields as find: elementText, selector, elementId, elementTestId, elementClass, elementAttribute, and elementAria. Each object needs at least one field, and combined fields must all match. Set a per-element timeout to wait longer for a specific element.
Drag within a specific browser tab
Set surface to a browser reference to drag between elements outside the active tab. Omit it to act on the active tab.