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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.