Skip to content

Commit

Permalink
Replace selenium events with js events
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jan 19, 2025
1 parent 319602d commit 9fb6d9c
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,42 +1025,51 @@ public function keyUp(string $xpath, $char, ?string $modifier = null)

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

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

$script = <<<JS
(function (sourceElement) {
window['__minkDragAndDropSourceElement'] = sourceElement;
sourceElement.dispatchEvent(new DragEvent('dragstart', {bubbles: true, cancelable: true}));
}({{ELEMENT}}));
JS;
$this->executeJsOnElement($source, $script);

if ($destination->getID() === $source->getID()) {
$this->getWebDriverSession()->click(0);
} else {
$this->getWebDriverSession()->buttondown();
$this->getWebDriverSession()->moveto(array(
'element' => $destination->getID()
));
$this->getWebDriverSession()->buttonup();
}

$script = <<<JS
(function (targetElement) {
targetElement.dispatchEvent(new DragEvent('dragover', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new DragEvent('drop', {bubbles: true, cancelable: true}));
$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(['element' => $source->getID()]);

Check warning on line 1031 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1031

Added line #L1031 was not covered by tests

$this->executeJsOnElement($source, <<<'JS'

Check warning on line 1033 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1033

Added line #L1033 was not covered by tests
(function (sourceElement) {
var withPointerEvents = 'PointerEvent' in window;
window['__minkDragAndDropSourceElement'] = sourceElement;
withPointerEvents && sourceElement.dispatchEvent(new PointerEvent('pointerdown', {bubbles: true, cancelable: true}));
sourceElement.dispatchEvent(new MouseEvent('mousedown', {bubbles: true, cancelable: true}));
sourceElement.dispatchEvent(new DragEvent('dragstart', {bubbles: true, cancelable: true}));
withPointerEvents && sourceElement.dispatchEvent(new PointerEvent('pointercancel', {bubbles: true, cancelable: true}));
withPointerEvents && sourceElement.dispatchEvent(new PointerEvent('pointerout', {bubbles: true, cancelable: true}));
withPointerEvents && sourceElement.dispatchEvent(new PointerEvent('pointerleave', {bubbles: true, cancelable: true}));
}({{ELEMENT}}));
JS
);

Check warning on line 1046 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1045-L1046

Added lines #L1045 - L1046 were not covered by tests

window['__minkDragAndDropSourceElement'].dispatchEvent(new DragEvent('dragend', {bubbles: true, cancelable: true}));
}({{ELEMENT}}));
JS;
$this->executeJsOnElement($destination, $script);
$this->getWebDriverSession()->moveto(['element' => $target->getID()]);

Check warning on line 1048 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1048

Added line #L1048 was not covered by tests

$this->executeJsOnElement($target, <<<'JS'

Check warning on line 1050 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1050

Added line #L1050 was not covered by tests
(function (targetElement) {
var withPointerEvents = 'PointerEvent' in window;
var sourceElement = window['__minkDragAndDropSourceElement'];
withPointerEvents && targetElement.dispatchEvent(new PointerEvent('pointerover', {bubbles: true, cancelable: true}));
withPointerEvents && targetElement.dispatchEvent(new PointerEvent('pointerenter', {bubbles: true, cancelable: true}));
sourceElement.dispatchEvent(new MouseEvent('mouseout', {bubbles: true, cancelable: true}));
sourceElement.dispatchEvent(new MouseEvent('mouseleave', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new MouseEvent('mouseover', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new MouseEvent('mouseenter', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new MouseEvent('mousemove', {bubbles: true, cancelable: true}));
withPointerEvents && targetElement.dispatchEvent(new PointerEvent('pointerout', {bubbles: true, cancelable: true}));
withPointerEvents && targetElement.dispatchEvent(new PointerEvent('pointerleave', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new MouseEvent('mouseout', {bubbles: true, cancelable: true}));
targetElement.dispatchEvent(new MouseEvent('mouseleave', {bubbles: true, cancelable: true}));
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 1072 in src/Selenium2Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Selenium2Driver.php#L1071-L1072

Added lines #L1071 - L1072 were not covered by tests
}

public function executeScript(string $script)
Expand Down

0 comments on commit 9fb6d9c

Please sign in to comment.