Unable to perform drag and drop action even code executed without any error

Unable to see element dragged in AnjularJS application even though drag and drop actions performed without any error.

Below is the Code :

WebElement draggable = driver.findElement(By.xpath(“//*[@id=’page-content-wrapper’]/div[2]/div[3]/div[4]/div/div[2]/div/div/div[2]/div/div[1]/ul/li/ul/li[2]/span/span[1]”));
WebElement droppable = driver.findElement(By.xpath(“.//*[@id=’page-content-wrapper’]/div[2]/div[3]/div[3]/div[2]/div[1]/div[1]/div/div[6]/div[2]/div[1]”));

Below are different methods performed and executed without error:
1)
Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL).click(draggable).click(droppable). keyUp(Keys.CONTROL).build().perform();

2)
Actions builder = new Actions(driver);
builder.clickAndHold(draggable).moveToElement(droppable).build();
builder.dragAndDrop(draggable, droppable).perform();

3)
Actions action = new Actions(driver);
Thread.sleep(1000);
action.clickAndHold(draggable2).build().perform();
action.moveToElement(droppable2).build().perform();
action.release(droppable2).build().perform();

4)

Actions actions = new Actions(driver);
browser.actions()
.mouseDown(draggable2)
.mouseMove(draggable2)
.mouseUp(droppable2)
.perform();

Also performed JavaScript methods :

A)
public static void DragAndDropJS2(WebElement source, WebElement destination, WebDriver driver) throws Exception
{
// JavascriptExecutor js = (JavascriptExecutor) driver;
String xto=Integer.toString(source.getLocation().x);
String yto=Integer.toString(destination.getLocation().y);
((JavascriptExecutor)driver).executeScript(“function simulate(f,c,d,e){var b,a=null;for(b in eventMatchers)if(eventMatchers[b].test(c)){a=b;break}if(!a)return!1;document.createEvent?(b=document.createEvent(a),a==\”HTMLEvents\”?b.initEvent(c,!0,!0):b.initMouseEvent(c,!0,!0,document.defaultView,0,d,e,d,e,!1,!1,!1,!1,0,null),f.dispatchEvent(b)):(a=document.createEventObject(),a.detail=0,a.screenX=d,a.screenY=e,a.clientX=d,a.clientY=e,a.ctrlKey=!1,a.altKey=!1,a.shiftKey=!1,a.metaKey=!1,a.button=1,f.fireEvent(\”on\”+c,a));return!0} var eventMatchers={HTMLEvents:/^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,MouseEvents:/^(?:click|dblclick|mouse(?:down|up|over|move|out))$/}; ” +
“simulate(arguments[0],\”mousedown\”,0,0); simulate(arguments[0],\”mousemove\”,arguments[1],arguments[2]); simulate(arguments[0],\”mouseup\”,arguments[1],arguments[2]); “,
source,xto,yto);

}

B)
public static void DragAndDropJS(WebElement source, WebElement destination, WebDriver driver) throws Exception
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“function createEvent(typeOfEvent) {\n” +”var event =document.createEvent(\”CustomEvent\”);\n” +”event.initCustomEvent(typeOfEvent,true, true, null);\n” +”event.dataTransfer = {\n” +”data: {},\n” +”setData: function (key, value) {\n” +”this.data[key] = value;\n” +”},\n” +”getData: function (key) {\n” +”return this.data[key];\n” +”}\n” +”};\n” +”return event;\n” +”}\n” +”\n” +”function dispatchEvent(element, event,transferData) {\n” +”if (transferData !== undefined) {\n” +”event.dataTransfer = transferData;\n” +”}\n” +”if (element.dispatchEvent) {\n” + “element.dispatchEvent(event);\n” +”} else if (element.fireEvent) {\n” +”element.fireEvent(\”on\” + event.type, event);\n” +”}\n” +”}\n” +”\n” +”function simulateHTML5DragAndDrop(element, destination) {\n” +”var dragStartEvent =createEvent(‘dragstart’);\n” +”dispatchEvent(element, dragStartEvent);\n” +”var dropEvent = createEvent(‘drop’);\n” +”dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n” +”var dragEndEvent = createEvent(‘dragend’);\n” +”dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n” +”}\n” +”\n” +”var source = arguments[0];\n” +”var destination = arguments[1];\n” +”simulateHTML5DragAndDrop(source,destination);”,source, destination);
Thread.sleep(1500);

}

C)
public static void DragAndDropJS1(WebElement source, WebElement destination, WebDriver driver) throws Exception
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“function createEvent(typeOfEvent) {\n” +”var event =document.createEvent(\”CustomEvent\”);\n” +”event.initCustomEvent(typeOfEvent,true, true, null);\n” +”event.dataTransfer = {\n” +”data: {},\n” +”setData: function (key, value) {\n” +”this.data[key] = value;\n” +”},\n” +”getData: function (key) {\n” +”return this.data[key];\n” +”}\n” +”};\n” +”return event;\n” +”}\n” +”\n” +”function dispatchEvent(element, event,transferData) {\n” +”if (transferData !== undefined) {\n” +”event.dataTransfer = transferData;\n” +”}\n” +”if (element.dispatchEvent) {\n” + “element.dispatchEvent(event);\n” +”} else if (element.fireEvent) {\n” +”element.fireEvent(\”on\” + event.type, event);\n” +”}\n” +”}\n” +”\n” +”function simulateHTML5DragAndDrop(element, destination) {\n” +”var dragStartEvent =createEvent(‘dragstart’);\n” +”dispatchEvent(element, dragStartEvent);\n” +”var dropEvent = createEvent(‘drop’);\n” +”dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n” +”}\n” +”\n” +”var source = arguments[0];\n” +”var destination = arguments[1];\n” +”simulateHTML5DragAndDrop(source,destination);”,source, destination);
Thread.sleep(1500);

}

All the above mentioned methods and commands are compilled and executed without any error but unable to perform drag and drop operation in UI (PLEASE HELP).

Default Asked on September 23, 2016 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)

    RE: Unable to perform drag and drop action even code executed without any error

    RE: Unable to perform drag and drop action even code executed without any error

    Default Answered on September 23, 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.