You are here

protected function Select2JavascriptTestBase::dragDropElement in Select 2 8

Drag element in document with defined offset position.

Parameters

\Behat\Mink\Element\NodeElement $element: Element that will be dragged.

int $offsetX: Vertical offset for element drag in pixels.

int $offsetY: Horizontal offset for element drag in pixels.

1 call to Select2JavascriptTestBase::dragDropElement()
Select2EntityReferenceWidgetTest::testAutocompleteDragnDrop in tests/src/FunctionalJavascript/FieldWidget/Select2EntityReferenceWidgetTest.php
Tests the autocomplete drag n drop.

File

tests/src/FunctionalJavascript/Select2JavascriptTestBase.php, line 85

Class

Select2JavascriptTestBase
Class Select2JavascriptTestBase.

Namespace

Drupal\Tests\select2\FunctionalJavascript

Code

protected function dragDropElement(NodeElement $element, $offsetX, $offsetY) {
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $elemXpath = addslashes($element
    ->getXpath());
  $jsCode = "var fireMouseEvent = function (type, element, x, y) {" . "  var event = document.createEvent('MouseEvents');" . "  event.initMouseEvent(type, true, (type !== 'mousemove'), window, 0, 0, 0, x, y, false, false, false, false, 0, element);" . "  element.dispatchEvent(event); };";

  // XPath provided by getXpath uses single quote (') to encapsulate strings,
  // that's why xpath has to be quited with double quites in javascript code.
  $jsCode .= "(function() {" . "  var dragElement = document.evaluate(\"{$elemXpath}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" . "  var pos = dragElement.getBoundingClientRect();" . "  var centerX = Math.floor((pos.left + pos.right) / 2);" . "  var centerY = Math.floor((pos.top + pos.bottom) / 2);" . "  fireMouseEvent('mousedown', dragElement, centerX, centerY);" . "  fireMouseEvent('mousemove', document, centerX + {$offsetX}, centerY + {$offsetY});" . "  fireMouseEvent('mouseup', dragElement, centerX + {$offsetX}, centerY + {$offsetY});" . "})();";
  $this
    ->getSession()
    ->executeScript($jsCode);
}