You are here

protected function Select2JavascriptTestBase::scrollElementInView in Select 2 8

Scroll element with defined css selector in middle of browser view.

Parameters

string $cssSelector: CSS Selector for element that should be centralized.

2 calls to Select2JavascriptTestBase::scrollElementInView()
Select2WidgetTest::testEmptyField in tests/src/FunctionalJavascript/FieldWidget/Select2WidgetTest.php
Test rendering of an empty field.
Select2WidgetTest::testLimitedCount in tests/src/FunctionalJavascript/FieldWidget/Select2WidgetTest.php
Test multiple field selection with 2 items.

File

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

Class

Select2JavascriptTestBase
Class Select2JavascriptTestBase.

Namespace

Drupal\Tests\select2\FunctionalJavascript

Code

protected function scrollElementInView($cssSelector) {
  $this
    ->getSession()
    ->executeScript('
        var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
        var element = jQuery(\'' . addcslashes($cssSelector, '\'') . '\');
        var scrollTop = element.offset().top - (viewPortHeight/2);
        var scrollableParent = jQuery.isFunction(element.scrollParent) ? element.scrollParent() : [];
        if (scrollableParent.length > 0 && scrollableParent[0] !== document && scrollableParent[0] !== document.body) { scrollableParent[0].scrollTop = scrollTop } else { window.scroll(0, scrollTop); };
      ');
}