Skip to content

Commit 2cd7d77

Browse files
authored
Improve drag-and-drop JS event emulation on Selenium 3 (#408)
1 parent 717c5b3 commit 2cd7d77

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/Selenium2Driver.php

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,44 +1025,35 @@ public function keyUp(string $xpath, $char, ?string $modifier = null)
10251025

10261026
public function dragTo(string $sourceXpath, string $destinationXpath)
10271027
{
1028-
$source = $this->findElement($sourceXpath);
1029-
$destination = $this->findElement($destinationXpath);
1028+
$source = $this->findElement($sourceXpath);
1029+
$target = $this->findElement($destinationXpath);
10301030

1031-
$this->getWebDriverSession()->moveto(array(
1032-
'element' => $source->getID()
1033-
));
1034-
1035-
$script = <<<JS
1036-
(function (element) {
1037-
var event = document.createEvent("HTMLEvents");
1031+
$this->getWebDriverSession()->moveto(array('element' => $source->getID()));
1032+
$this->getWebDriverSession()->buttondown();
10381033

1039-
event.initEvent("dragstart", true, true);
1040-
event.dataTransfer = {};
1034+
$this->executeJsOnElement($source, <<<'JS'
1035+
(function (sourceElement) {
1036+
window['__minkDragAndDropSourceElement'] = sourceElement;
10411037
1042-
element.dispatchEvent(event);
1043-
}({{ELEMENT}}));
1044-
JS;
1045-
$this->executeJsOnElement($source, $script);
1038+
sourceElement.dispatchEvent(new DragEvent('dragstart', {bubbles: true, cancelable: true}));
1039+
}({{ELEMENT}}));
1040+
JS
1041+
);
10461042

1047-
$this->getWebDriverSession()->buttondown();
1048-
if ($destination->getID() !== $source->getID()) {
1049-
$this->getWebDriverSession()->moveto(array(
1050-
'element' => $destination->getID()
1051-
));
1052-
}
1043+
$this->getWebDriverSession()->moveto(array('element' => $target->getID()));
10531044
$this->getWebDriverSession()->buttonup();
10541045

1055-
$script = <<<JS
1056-
(function (element) {
1057-
var event = document.createEvent("HTMLEvents");
1046+
$this->executeJsOnElement($target, <<<'JS'
1047+
(function (targetElement) {
1048+
var sourceElement = window['__minkDragAndDropSourceElement'];
10581049
1059-
event.initEvent("drop", true, true);
1060-
event.dataTransfer = {};
1061-
1062-
element.dispatchEvent(event);
1063-
}({{ELEMENT}}));
1064-
JS;
1065-
$this->executeJsOnElement($destination, $script);
1050+
sourceElement.dispatchEvent(new DragEvent('drag', {bubbles: true, cancelable: true}));
1051+
targetElement.dispatchEvent(new DragEvent('dragover', {bubbles: true, cancelable: true}));
1052+
targetElement.dispatchEvent(new DragEvent('drop', {bubbles: true, cancelable: true}));
1053+
sourceElement.dispatchEvent(new DragEvent('dragend', {bubbles: true, cancelable: true}));
1054+
}({{ELEMENT}}));
1055+
JS
1056+
);
10661057
}
10671058

10681059
public function executeScript(string $script)

0 commit comments

Comments
 (0)