diff -u b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php --- b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php @@ -84,42 +84,26 @@ */ public function testFragmentLink() { $session = $this->getSession(); + $web_assert = $this->assertSession(); + $ckeditor_id = '#cke_edit-body-0-value'; $this->drupalGet('node/add/page'); - $this->getSession()->getPage(); + $session->getPage(); // Add a bottom margin to the title field to be sure the body field is not // visible. PhantomJS runs with a resolution of 1024x768px. $session->executeScript("document.getElementById('edit-title-0-value').style.marginBottom = '800px';"); - // Javascript to test if top of the CKEditor-enabled body field is visible - // in the viewport. The NodeElement::isVisible() method doesn't take the - // viewport into account. - $visibility_check_javascript = <<= 0 && - r.top < (w.innerHeight || e.clientHeight) - ); - }()); -JS; - // Check that the CKEditor-enabled body field is currently not visible in // the viewport. - $this->assertFalse($session->evaluateScript($visibility_check_javascript), 'CKEditor-enabled body field is not visible.'); + $web_assert->assertNotVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is visible.'); // Trigger a hash change with as target the hidden textarea. $session->executeScript("document.location.hash = '#edit-body-0-value';"); // Check that the CKEditor-enabled body field is visible in the viewport. - $this->assertTrue($session->evaluateScript($visibility_check_javascript), 'CKEditor-enabled body field is visible.'); + $web_assert->assertVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is not visible.'); } } only in patch2: unchanged: --- a/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php @@ -2,6 +2,10 @@ namespace Drupal\FunctionalJavascriptTests; +use Behat\Mink\Element\NodeElement; +use Behat\Mink\Exception\ElementHtmlException; +use Behat\Mink\Exception\ElementNotFoundException; +use Behat\Mink\Exception\UnsupportedDriverActionException; use Drupal\Tests\WebAssert; /** @@ -28,4 +32,201 @@ public function assertWaitOnAjaxRequest($timeout = 10000, $message = 'Unable to } } + /** + * Test that a node, or it's specific corner, is visible in the viewport. + * + * Note: Always set the viewport size. This can be done with a PhantomJS + * startup parameter or in your test with \Behat\Mink\Session->resizeWindow(). + * Drupal CI Javascript tests by default use a viewport of 1024x768px. + * + * @param string $selector_type + * The element selector type (css, xpath). + * @param string|array $selector + * The element selector. Note: the first found element is used. + * @param bool|string $corner + * (Optional) The corner to test: + * topLeft, topRight, bottomRight, bottomLeft. + * Or FALSE to check the complete element (default behavior). + * @param string $message + * (optional) A message for the exception. + * + * @throws \Behat\Mink\Exception\ElementHtmlException + * When the element doesn't exist. + * @throws \Behat\Mink\Exception\ElementNotFoundException + * When the element is not visible in the viewport. + */ + public function assertVisibleInViewport($selector_type, $selector, $corner = FALSE, $message = 'Element is not visible in the viewport.') { + $node = $this->session->getPage()->find($selector_type, $selector); + if ($node === NULL) { + if (is_array($selector)) { + $selector = implode(' ', $selector); + } + throw new ElementNotFoundException($this->session->getDriver(), 'element', $selector_type, $selector); + } + + // Check if the node is visible on the page, which is a perquisite of being + // visible in the viewport. + if (!$node->isVisible()) { + throw new ElementHtmlException($message, $this->session->getDriver(), $node); + } + + $result = $this->checkNodeVisibilityInViewport($node, $corner); + + if (!$result) { + throw new ElementHtmlException($message, $this->session->getDriver(), $node); + } + } + + /** + * Test that a node, or it's specific corner, is not visible in the viewport. + * + * Note: the node should exist in the page, otherwise this assertion fails. + * + * @param string $selector_type + * The element selector type (css, xpath). + * @param string|array $selector + * The element selector. Note: the first found element is used. + * @param bool|string $corner + * (Optional) The corner to test: + * topLeft, topRight, bottomRight, bottomLeft. + * Or FALSE to check the complete element (default behavior). + * @param string $message + * (optional) A message for the exception. + * + * @throws \Behat\Mink\Exception\ElementHtmlException + * When the element doesn't exist. + * @throws \Behat\Mink\Exception\ElementNotFoundException + * When the element is not visible in the viewport. + * + * @see \Drupal\FunctionalJavascriptTests\JSWebAssert::assertVisibleInViewport() + */ + public function assertNotVisibleInViewport($selector_type, $selector, $corner = FALSE, $message = 'Element is visible in the viewport.') { + $node = $this->session->getPage()->find($selector_type, $selector); + if ($node === NULL) { + if (is_array($selector)) { + $selector = implode(' ', $selector); + } + throw new ElementNotFoundException($this->session->getDriver(), 'element', $selector_type, $selector); + } + + $result = $this->checkNodeVisibilityInViewport($node, $corner); + + if ($result) { + throw new ElementHtmlException($message, $this->session->getDriver(), $node); + } + } + + /** + * Check the visibility of a node, or it's specific corner. + * + * @param \Behat\Mink\Element\NodeElement $node + * A valid node. + * @param bool|string $corner + * (Optional) The corner to test: + * topLeft, topRight, bottomRight, bottomLeft. + * Or FALSE to check the complete element (default behavior). + * + * @return 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. + */ + 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 = <<= 0 && + r.top <= ly && + r.left >= 0 && + r.left <= lx + ) + } +JS; + break; + + case 'topRight': + $test_javascript_function = <<= 0 && + r.top <= ly && + r.right >= 0 && + r.right <= lx + ); + } +JS; + break; + + case 'bottomRight': + $test_javascript_function = <<= 0 && + r.bottom <= ly && + r.right >= 0 && + r.right <= lx + ); + } +JS; + break; + + case 'bottomLeft': + $test_javascript_function = <<= 0 && + r.bottom <= ly && + r.left >= 0 && + r.left <= lx + ); + } +JS; + break; + + case FALSE: + $test_javascript_function = <<= 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 = <<session->evaluateScript($full_javascript_visibility_test); + } + }