Executing complex blocks of JavaScript with the JavascriptExecutor

First of all, we need to write some JavaScript code that can simulate the events that we need to trigger to perform the drag-and-drop action. To do this, we are going to create three JavaScript functions (note that these three functions are not Java code). The first function is going to create a JavaScript event:

function createEvent(typeOfEvent) { 
    var event = document.createEvent("CustomEvent"); 
    event.initCustomEvent(typeOfEvent, true, true, null); 
    event.dataTransfer = { 
        data: {},         setData: function (key, value) { 
            this.data[key] = value; 
        },         getData: function (key) { 
            return this.data[key]; 
    return event; 

We then need to write a function that will fire event instances ...

Get Mastering Selenium WebDriver 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.