|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +--- |
| 4 | + |
| 5 | +# dragAndDrop |
| 6 | + |
| 7 | +Drag and drop an element from a source location to a target location on the page. |
| 8 | + |
| 9 | +## Syntax |
| 10 | + |
| 11 | +### String (simple) |
| 12 | + |
| 13 | +```json |
| 14 | +{ |
| 15 | + "dragAndDrop": { |
| 16 | + "source": "string", |
| 17 | + "target": "string" |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +### Object (detailed) |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "dragAndDrop": { |
| 27 | + "source": { |
| 28 | + "selector": "string", |
| 29 | + "elementText": "string" |
| 30 | + }, |
| 31 | + "target": { |
| 32 | + "selector": "string", |
| 33 | + "elementText": "string" |
| 34 | + }, |
| 35 | + "duration": "number" |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Properties |
| 41 | + |
| 42 | +| Property | Type | Description | Default | |
| 43 | +| :-- | :-- | :-- | :-- | |
| 44 | +| `source` | string or object | **Required.** The element to drag. Can be a string (element text or CSS selector) or an object with `selector` and/or `elementText` properties. | | |
| 45 | +| `target` | string or object | **Required.** The element to drop onto. Can be a string (element text or CSS selector) or an object with `selector` and/or `elementText` properties. | | |
| 46 | +| `duration` | number | Optional. Duration of the drag operation in milliseconds. | `1000` | |
| 47 | + |
| 48 | +### Source and Target Properties |
| 49 | + |
| 50 | +When using the detailed object format for `source` or `target`, you can specify: |
| 51 | + |
| 52 | +| Property | Type | Description | |
| 53 | +| :-- | :-- | :-- | |
| 54 | +| `selector` | string | CSS selector or XPath to locate the element. | |
| 55 | +| `elementText` | string | Text content of the element to match. Supports regex patterns when wrapped with forward slashes (e.g., `/pattern/`). | |
| 56 | + |
| 57 | +## Examples |
| 58 | + |
| 59 | +### Basic drag and drop |
| 60 | + |
| 61 | +Drag a "Table" widget to a drop zone: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "dragAndDrop": { |
| 66 | + "source": "Table", |
| 67 | + "target": ".drop-zone" |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Using CSS selectors |
| 73 | + |
| 74 | +Drag an element by selector to another element by selector: |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "dragAndDrop": { |
| 79 | + "source": ".draggable-item[data-widget='chart']", |
| 80 | + "target": "#canvas-area", |
| 81 | + "duration": 1500 |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Detailed syntax with element text and selector |
| 87 | + |
| 88 | +Combine selector and text matching for precise element targeting: |
| 89 | + |
| 90 | +```json |
| 91 | +{ |
| 92 | + "dragAndDrop": { |
| 93 | + "source": { |
| 94 | + "selector": ".widget-item", |
| 95 | + "elementText": "Chart Widget" |
| 96 | + }, |
| 97 | + "target": { |
| 98 | + "selector": ".drop-zone[data-zone='primary']" |
| 99 | + }, |
| 100 | + "duration": 2000 |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Using regex patterns |
| 106 | + |
| 107 | +Use regex patterns to match element text dynamically: |
| 108 | + |
| 109 | +```json |
| 110 | +{ |
| 111 | + "dragAndDrop": { |
| 112 | + "source": "/Widget Item.*/", |
| 113 | + "target": ".drop-zone" |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +```json |
| 119 | +{ |
| 120 | + "dragAndDrop": { |
| 121 | + "source": { |
| 122 | + "selector": ".draggable", |
| 123 | + "elementText": "/Widget.*[0-9]+/" |
| 124 | + }, |
| 125 | + "target": { |
| 126 | + "selector": ".drop-zone" |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +## How it works |
| 133 | + |
| 134 | +The `dragAndDrop` action locates the source and target elements using the specified criteria, then performs a drag and drop operation between them. The action: |
| 135 | + |
| 136 | +1. **Finds the source element** using the provided selector, element text, or combination of both |
| 137 | +2. **Finds the target element** using the provided selector, element text, or combination of both |
| 138 | +3. **Performs the drag and drop** using WebDriver's native drag and drop functionality |
| 139 | +4. **Falls back to HTML5 simulation** if the native method fails silently |
| 140 | + |
| 141 | +### Element matching |
| 142 | + |
| 143 | +Elements can be matched using: |
| 144 | + |
| 145 | +- **Element text**: Matches elements by their visible text content |
| 146 | +- **CSS selectors**: Standard CSS selector syntax (e.g., `.class`, `#id`, `[attribute="value"]`) |
| 147 | +- **XPath selectors**: XPath expressions for complex element targeting |
| 148 | +- **Regex patterns**: Regular expressions for flexible text matching (wrap with `/pattern/`) |
| 149 | +- **Combined matching**: Use both selector and elementText for precise targeting |
| 150 | + |
| 151 | +### Drag and drop implementation |
| 152 | + |
| 153 | +The action uses a robust approach with multiple fallback mechanisms: |
| 154 | + |
| 155 | +1. **WebDriver native method**: Uses the browser's built-in drag and drop functionality |
| 156 | +2. **HTML5 simulation**: If the native method fails, falls back to JavaScript-based HTML5 drag and drop events |
| 157 | +3. **Error handling**: Provides detailed error messages for troubleshooting |
| 158 | + |
| 159 | +## Notes |
| 160 | + |
| 161 | +- The `dragAndDrop` action requires a browser context and cannot be used with API-only testing |
| 162 | +- Some web applications may require specific drag and drop implementations - the action includes fallbacks to handle different scenarios |
| 163 | +- When using regex patterns in `elementText`, wrap the pattern with forward slashes (e.g., `/pattern/`) |
| 164 | +- The `duration` parameter controls how long the drag operation takes, which can be useful for applications that require slower drag movements |
| 165 | +- Both source and target elements must be visible and interactable on the page |
| 166 | + |
| 167 | +## Related actions |
| 168 | + |
| 169 | +- [**click**](/docs/get-started/actions/click): Click or tap an element |
| 170 | +- [**find**](/docs/get-started/actions/find): Locate and interact with an element |
| 171 | +- [**type**](/docs/get-started/actions/type): Type text into an element |
0 commit comments