O'Reilly logo

Mastering Selenium WebDriver 3.0 by Mark Collin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required