You are here

protected function EntityBrowserWebDriverTestBase::dragDropElement in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 tests/src/FunctionalJavascript/EntityBrowserWebDriverTestBase.php \Drupal\Tests\entity_browser\FunctionalJavascript\EntityBrowserWebDriverTestBase::dragDropElement()

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.

2 calls to EntityBrowserWebDriverTestBase::dragDropElement()
EntityReferenceWidgetTest::testDragAndDrop in tests/src/FunctionalJavascript/EntityReferenceWidgetTest.php
Tests that drag and drop functions properly.
InlineEntityFormTest::testEntityBrowserInsideInlineEntityForm in tests/src/FunctionalJavascript/InlineEntityFormTest.php
Check that selection state in entity browser Inline Entity Form.

File

tests/src/FunctionalJavascript/EntityBrowserWebDriverTestBase.php, line 205

Class

EntityBrowserWebDriverTestBase
Base class for Entity browser Javascript functional tests.

Namespace

Drupal\Tests\entity_browser\FunctionalJavascript

Code

protected function dragDropElement(NodeElement $element, $offsetX, $offsetY) {
  $elemXpath = $element
    ->getXpath();
  $jsCode = "var fireMouseEvent = function (type, element, x, y) {\n      var event = document.createEvent('MouseEvents');\n      event.initMouseEvent(type, true, (type !== 'mousemove'), window, 0, 0, 0, x, y, false, false, false, false, 0, element);\n      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() {\n      var dragElement = document.evaluate(\"{$elemXpath}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\n      var pos = dragElement.getBoundingClientRect();\n      var centerX = Math.floor((pos.left + pos.right) / 2);\n      var centerY = Math.floor((pos.top + pos.bottom) / 2);\n      fireMouseEvent('mousedown', dragElement, centerX, centerY);\n      var xOffset = {$offsetX};\n      var yOffset = {$offsetY};\n      var moves = 3;\n\t    for (i = 0 ; i < moves ; i++ ) {\n\t\t\t  centerX += xOffset / moves;\n\t\t\t  centerY += yOffset / moves;\n\t\t    fireMouseEvent('mousemove', dragElement, Math.round(centerX), Math.round(centerY));\n\t\t  }\n      fireMouseEvent('mouseup', dragElement, centerX, centerY);\n    })();";
  $this
    ->getSession()
    ->executeScript($jsCode);
}