Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve drag-and-drop JS event emulation on Selenium 3 #408

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 22 additions & 31 deletions src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,44 +1025,35 @@

public function dragTo(string $sourceXpath, string $destinationXpath)
{
$source = $this->findElement($sourceXpath);
$destination = $this->findElement($destinationXpath);
$source = $this->findElement($sourceXpath);
$target = $this->findElement($destinationXpath);

Check warning on line 1029 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1028-L1029

Added lines #L1028 - L1029 were not covered by tests

$this->getWebDriverSession()->moveto(array(
'element' => $source->getID()
));

$script = <<<JS
(function (element) {
var event = document.createEvent("HTMLEvents");
$this->getWebDriverSession()->moveto(array('element' => $source->getID()));
$this->getWebDriverSession()->buttondown();

Check warning on line 1032 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1031-L1032

Added lines #L1031 - L1032 were not covered by tests
aik099 marked this conversation as resolved.
Show resolved Hide resolved

event.initEvent("dragstart", true, true);
event.dataTransfer = {};
$this->executeJsOnElement($source, <<<'JS'

Check warning on line 1034 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1034

Added line #L1034 was not covered by tests
(function (sourceElement) {
window['__minkDragAndDropSourceElement'] = sourceElement;

element.dispatchEvent(event);
}({{ELEMENT}}));
JS;
$this->executeJsOnElement($source, $script);
sourceElement.dispatchEvent(new DragEvent('dragstart', {bubbles: true, cancelable: true}));
}({{ELEMENT}}));
JS
);

Check warning on line 1041 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1040-L1041

Added lines #L1040 - L1041 were not covered by tests

$this->getWebDriverSession()->buttondown();
if ($destination->getID() !== $source->getID()) {
$this->getWebDriverSession()->moveto(array(
'element' => $destination->getID()
));
}
$this->getWebDriverSession()->moveto(array('element' => $target->getID()));

Check warning on line 1043 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1043

Added line #L1043 was not covered by tests
$this->getWebDriverSession()->buttonup();

$script = <<<JS
(function (element) {
var event = document.createEvent("HTMLEvents");
$this->executeJsOnElement($target, <<<'JS'

Check warning on line 1046 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1046

Added line #L1046 was not covered by tests
(function (targetElement) {
var sourceElement = window['__minkDragAndDropSourceElement'];

event.initEvent("drop", true, true);
event.dataTransfer = {};

element.dispatchEvent(event);
}({{ELEMENT}}));
JS;
$this->executeJsOnElement($destination, $script);
sourceElement.dispatchEvent(new DragEvent('drag', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new DragEvent('dragover', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new DragEvent('drop', {bubbles: true, cancelable: true}));
sourceElement.dispatchEvent(new DragEvent('dragend', {bubbles: true, cancelable: true}));
}({{ELEMENT}}));
JS
);

Check warning on line 1056 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1055-L1056

Added lines #L1055 - L1056 were not covered by tests
}

public function executeScript(string $script)
Expand Down
Loading