You are here

private function JSWebAssert::checkNodeVisibilityInViewport in Drupal 8

Same name and namespace in other branches
  1. 9 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
Test that a node, or its specific corner, is not visible in the viewport.
JSWebAssert::assertVisibleInViewport in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Test that a node, or its specific corner, is visible in the viewport.

File

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

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);
}