Skip to navigation

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. 0 or more. Default is 1000. A 0 drags 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 with selector, the element must match both the text and the selector.
  • selector: (Optional) CSS selector or XPath selector of the element. If combined with elementText, 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 is 5000. 0 checks 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 dragAndDrop reference.

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

{
"tests": [
{
"steps": [
{
"description": "Drag 'Item 1' and drop it onto 'Drop Zone'",
"dragAndDrop": {
"source": "#source-list .draggable-item:first-child",
"target": "Drop Zone"
}
}
]
}
]
}

Drag and drop with detailed element objects

{
"tests": [
{
"steps": [
{
"description": "Drag a specific task to a different column",
"dragAndDrop": {
"source": {
"selector": ".task-card",
"elementText": "Complete documentation"
},
"target": {
"selector": ".column",
"elementText": "In Progress"
}
}
}
]
}
]
}

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.

{
"tests": [
{
"steps": [
{
"description": "Drag a widget slowly onto a drop zone.",
"dragAndDrop": {
"source": ".draggable[data-widget='image']",
"target": "Drop Zone 1",
"duration": 2000
}
}
]
}
]
}

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.

{
"tests": [
{
"steps": [
{
"description": "Drag the first widget matching a text pattern.",
"dragAndDrop": {
"source": "/Widget Item [0-9]/",
"target": "Drop Zone 1"
}
}
]
}
]
}

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.

{
"tests": [
{
"steps": [
{
"description": "Drag by class and attribute onto a zone matched by attribute.",
"dragAndDrop": {
"source": {
"elementClass": "draggable",
"elementAttribute": { "data-widget": "chart" }
},
"target": {
"selector": ".drop-zone",
"elementAttribute": { "data-drop": "zone2" },
"timeout": 8000
}
}
}
]
}
]
}

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.

{
"tests": [
{
"steps": [
{
"description": "Reorder a card in the board tab.",
"dragAndDrop": {
"source": "Draft",
"target": "In progress",
"surface": { "browser": "chrome", "tab": "board" }
}
}
]
}
]
}
  • click: Click elements to interact with them
  • find: Locate elements before performing other actions
  • wait: Add delays for animations to complete