Created
August 26, 2015 03:56
-
-
Save druska/624501b7209a74040175 to your computer and use it in GitHub Desktop.
Create the `simulateDragDrop` function which can be used to simulate clicking and dragging one DOM Node onto another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function simulateDragDrop(sourceNode, destinationNode) { | |
var EVENT_TYPES = { | |
DRAG_END: 'dragend', | |
DRAG_START: 'dragstart', | |
DROP: 'drop' | |
} | |
function createCustomEvent(type) { | |
var event = new CustomEvent("CustomEvent") | |
event.initCustomEvent(type, true, true, null) | |
event.dataTransfer = { | |
data: { | |
}, | |
setData: function(type, val) { | |
this.data[type] = val | |
}, | |
getData: function(type) { | |
return this.data[type] | |
} | |
} | |
return event | |
} | |
function dispatchEvent(node, type, event) { | |
if (node.dispatchEvent) { | |
return node.dispatchEvent(event) | |
} | |
if (node.fireEvent) { | |
return node.fireEvent("on" + type, event) | |
} | |
} | |
var event = createCustomEvent(EVENT_TYPES.DRAG_START) | |
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event) | |
var dropEvent = createCustomEvent(EVENT_TYPES.DROP) | |
dropEvent.dataTransfer = event.dataTransfer | |
dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent) | |
var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END) | |
dragEndEvent.dataTransfer = event.dataTransfer | |
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent) | |
} |
@Praveer-grover
Hi, is it possible to upgrade this script and move the element to the given coordinates?
If someone is doing this in 2021, and is having trouble with data transfer object (in the above example dataTransfer object is a very trimmed down version of the actual object in 2021, so some of the application implementation could reject this object) then the following would work. Just replaced the Custom event with DragEvent and used the actual DataTransfer object instead of the dummy one.
function simulateDragDrop(sourceNode, destinationNode) { var EVENT_TYPES = { DRAG_END: 'dragend', DRAG_START: 'dragstart', DROP: 'drop' } function createCustomEvent(type, dataTransfer) { var event = new DragEvent(type, {dataTransfer}); return event } function dispatchEvent(node, type, event) { if (node.dispatchEvent) { return node.dispatchEvent(event) } if (node.fireEvent) { return node.fireEvent("on" + type, event) } } var event = createCustomEvent(EVENT_TYPES.DRAG_START, new DataTransfer()) dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event) var dropEvent = createCustomEvent(EVENT_TYPES.DROP, event.dataTransfer) dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent) var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END, event.dataTransfer) dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent) }
@Praveer-grover this changes in this script are not moving elements for me, is it possible that you can confirm this method is still functional?
DragEvent
function simulateDragDrop(sourceNode, destinationNode) {
var EVENT_TYPES = {
DRAG_END: 'dragend',
DRAG_START: 'dragstart',
DROP: 'drop'
}
function createCustomEvent(type, dataTransfer) {
dataTransfer.data = {};
dataTransfer.setData = function(type,val){this.data[type] = val};
dataTransfer.getData = function(type){return this.data[type]};
dataTransfer.dropEffect = 'move';
dataTransfer.effectAllowed = 'move';
dataTransfer.types = [];
dataTransfer.items = {};
dataTransfer.files = {};
var event = new DragEvent(
type,
{
bubbles: true,
cancelable: true,
dataTransfer: dataTransfer
});
return event
}
function dispatchEvent(node, type, event) {
if (node.dispatchEvent) {
return node.dispatchEvent(event)
}
if (node.fireEvent) {
return node.fireEvent("on" + type, event)
}
}
var event = createCustomEvent(EVENT_TYPES.DRAG_START, new DataTransfer())
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event)
var dropEvent = createCustomEvent(EVENT_TYPES.DROP, event.dataTransfer)
dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent)
var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END, event.dataTransfer)
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mufasalg0 you can use the script above, save it in your repo and read the file into a String object.
I don't know exactly what stack you're using but selenium or most drivers have options for executing javascript directly.
look for "selenium javascript executor" for example.