How to use the WebUI.dragAndDropToObject custom keyword successfully?
In most cases, the Katalon keyword WebUI.dragAndDropToObject
works as intended. There are still some special cases that the keyword cannot cover perfectly. One of the specific scenarios that we can find is HTML5 Drag and Drop with Selenium Webdriver. It happens because Selenium still does not provide official support for testing HTML5 Drag and Drop.
To figure out a solution for the special case above, we observed the behavior of the Web Page when performing the DnD action, simulated the events, and performed action on them.
The scripts below will demonstrate how we simulate said events:
async function simulateDragDrop(sourceNode, destinationNode) {
var EVENT_TYPES = {
DRAG_START: 'dragstart',
DRAG_ENTER: 'dragenter',
DRAG_OVER: 'dragover',
DRAG_END: 'dragend',
DROP: 'drop'
}
function createCustomEvent(type) {
var event = new CustomEvent("CustomEvent")
event.initCustomEvent(type, true, true, null)
event.dataTransfer = {
effectAllowed: "move",
dropEffect: "none",
data: {
},
types: [],
setData: function(type, val) {console.log("setdata", type);
this.data[type] = val;
this.types.push(type);
},
getData: function(type) {
return this.data[type]
}
}
return event
}
function dispatchEvent(node, type, event) {
var rect = node.getBoundingClientRect();
event.clientX = rect.x + (rect.width / 2);
event.clientY = rect.y + (rect.height / 2);
if (node.dispatchEvent) {
return node.dispatchEvent(event)
}
if (node.fireEvent) {
return node.fireEvent("on" + type, event)
}
}
function delay(timeout) {
return new Promise((resolve) => setTimeout(resolve, timeout));
}
var timeout = 1000
var dragStartEvent = createCustomEvent(EVENT_TYPES.DRAG_START)
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, dragStartEvent)
await delay(timeout)
var dragEnterEvent = createCustomEvent(EVENT_TYPES.DRAG_ENTER)
dispatchEvent(destinationNode, EVENT_TYPES.DRAG_ENTER, dragEnterEvent)
await delay(timeout)
var dragOverEvent = createCustomEvent(EVENT_TYPES.DRAG_OVER)
dispatchEvent(destinationNode, EVENT_TYPES.DRAG_OVER, dragOverEvent)
await delay(timeout)
var dropEvent = createCustomEvent(EVENT_TYPES.DROP)
dropEvent.dataTransfer = dragStartEvent.dataTransfer
dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent)
await delay(timeout)
var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END)
dragEndEvent.dataTransfer = dragStartEvent.dataTransfer
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent)
await delay(timeout)
}
''';
}
We will also be sharing with you the custom keyword below:
DragandDropHelper.groovy (4.3 KB)
And below is the sample scripts for you to check if the provided custom keyword works:
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
WebUI.openBrowser('HTML Drag and Drop API ')
//WebUI.dragAndDropToObject(findTestObject('Page_HTML Drag and Drop API/img_Example_drag1'), findTestObject('Page_HTML Drag and Drop API/div_Example_div2'))
//WebUI.delay(3)
CustomKeywords.'customkeywords.DragandDropHelper.dragAndDrop'(findTestObject('Page_HTML Drag and Drop API/img_Example_drag1'), findTestObject('Page_HTML Drag and Drop API/div_Example_div2'))
Let us know if the custom keywords that we have provided works for you. And if they do, then don’t forget to show us some love by clicking on the like button below !
And feel free to leave us your feedback so that we could improve our articles in the future!
We also have other topics related to working with custom keywords in Katalon Studio, check them out below
How to use the Windows.switchToDesktop() custom keyword to switch window faster?
How to use the WebUI.setText() custom keyword successfully?
How to use the WebUI.dragAndDropToObject custom keyword successfully? → You are here