function JSWebAssert::checkNodeVisibilityInViewport

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::checkNodeVisibilityInViewport()
  2. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::checkNodeVisibilityInViewport()
  3. 11.x core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::checkNodeVisibilityInViewport()

Check the visibility of a node, or its specific corner.

Parameters

\Behat\Mink\Element\NodeElement $node: A valid node.

bool|string $corner: (Optional) Corner to test: topLeft, topRight, bottomRight, bottomLeft. Or FALSE to check the complete element (default).

Return value

bool Returns TRUE if the node is visible in the viewport, FALSE otherwise.

Throws

\Behat\Mink\Exception\UnsupportedDriverActionException When an invalid corner specification is given.

2 calls to JSWebAssert::checkNodeVisibilityInViewport()
JSWebAssert::assertNotVisibleInViewport in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Tests that a node, or its specific corner, is not visible in the viewport.
JSWebAssert::assertVisibleInViewport in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Tests that a node, or its specific corner, is visible in the viewport.

File

core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php, line 401

Class

JSWebAssert
Defines a class with methods for asserting presence of elements during tests.

Namespace

Drupal\FunctionalJavascriptTests

Code

private function checkNodeVisibilityInViewport(NodeElement $node, $corner = FALSE) {
    $xpath = $node->getXpath();
    // Build the JavaScript to test if the complete element or a specific corner
    // is in the viewport.
    switch ($corner) {
        case 'topLeft':
            $test_javascript_function = <<<JS
          function t(r, lx, ly) {
            return (
              r.top >= 0 &&
              r.top <= ly &&
              r.left >= 0 &&
              r.left <= lx
            )
          }
JS;
            break;
        case 'topRight':
            $test_javascript_function = <<<JS
          function t(r, lx, ly) {
            return (
              r.top >= 0 &&
              r.top <= ly &&
              r.right >= 0 &&
              r.right <= lx
            );
          }
JS;
            break;
        case 'bottomRight':
            $test_javascript_function = <<<JS
          function t(r, lx, ly) {
            return (
              r.bottom >= 0 &&
              r.bottom <= ly &&
              r.right >= 0 &&
              r.right <= lx
            );
          }
JS;
            break;
        case 'bottomLeft':
            $test_javascript_function = <<<JS
          function t(r, lx, ly) {
            return (
              r.bottom >= 0 &&
              r.bottom <= ly &&
              r.left >= 0 &&
              r.left <= lx
            );
          }
JS;
            break;
        case FALSE:
            $test_javascript_function = <<<JS
          function t(r, lx, ly) {
            return (
              r.top >= 0 &&
              r.left >= 0 &&
              r.bottom <= ly &&
              r.right <= lx
            );
          }
JS;
            break;
        // Throw an exception if an invalid corner parameter is given.
        default:
            throw new UnsupportedDriverActionException($corner, $this->session
                ->getDriver());
    }
    // Build the full JavaScript test. The shared logic gets the corner
    // specific test logic injected.
    $full_javascript_visibility_test = <<<JS
      (function(t){
        var w = window,
        d = document,
        e = d.documentElement,
        n = d.evaluate("{<span class="php-variable">$xpath</span>}", d, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue,
        r = n.getBoundingClientRect(),
        lx = (w.innerWidth || e.clientWidth),
        ly = (w.innerHeight || e.clientHeight);

        return t(r, lx, ly);
      }({<span class="php-variable">$test_javascript_function</span>}));
JS;
    // Check the visibility by injecting and executing the full JavaScript test
    // script in the page.
    return $this->session
        ->evaluateScript($full_javascript_visibility_test);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.