Unverified Commit 9a11de65 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2892440 by bnjmnm, tedbow, alexpott, Krzysztof Domański, Lendude:...

Issue #2892440 by bnjmnm, tedbow, alexpott, Krzysztof Domański, Lendude: Provide helper test method to wait for an element to be removed from the page
parent 2be8f419
Loading
Loading
Loading
Loading
+1 −16
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ public function testValidationMessage() {
    $block_css_locator = '#layout-builder .block-system-powered-by-block';
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', $block_css_locator));

    $this->waitForNoElement('#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $assert_session->assertWaitOnAjaxRequest();
    $this->drupalGet($this->getUrl());
    $this->clickElementWhenClickable($page->findButton('Save layout'));
@@ -87,21 +87,6 @@ public function testValidationMessage() {
    $this->assertMessagesDisplayed();
  }

  /**
   * Waits for an element to be removed from the page.
   *
   * @param string $selector
   *   CSS selector.
   * @param int $timeout
   *   (optional) Timeout in milliseconds, defaults to 10000.
   *
   * @todo Remove in https://www.drupal.org/node/2892440.
   */
  protected function waitForNoElement($selector, $timeout = 10000) {
    $condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)";
    $this->assertJsCondition($condition, $timeout);
  }

  /**
   * Asserts that the validation messages are shown correctly.
   */
+2 −18
Original line number Diff line number Diff line
@@ -78,8 +78,7 @@ public function testContentPreviewToggle() {
    $page->uncheckField('layout-builder-content-preview');
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '.layout-builder-block__content-preview-placeholder-label'));

    // Wait for preview content hide() to complete.
    $this->waitForNoElement('[data-layout-content-preview-placeholder-label] .field--name-body:visible');
    // Confirm that block content is not on page.
    $assert_session->pageTextNotContains($content_preview_body_text);
    $this->assertContextualLinks();

@@ -124,7 +123,7 @@ protected function assertContextualLinks() {
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertNotEmpty($this->assertSession()->waitForButton('Close'));
    $page->pressButton('Close');
    $this->waitForNoElement('#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
  }

  /**
@@ -147,19 +146,4 @@ protected function assertOrderInPage(array $items) {
    $this->assertCount(count($items), $blocks_with_expected_text);
  }

  /**
   * Waits for an element to be removed from the page.
   *
   * @param string $selector
   *   CSS selector.
   * @param int $timeout
   *   (optional) Timeout in milliseconds, defaults to 10000.
   *
   * @todo Remove in https://www.drupal.org/node/2892440.
   */
  protected function waitForNoElement($selector, $timeout = 10000) {
    $condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)";
    $this->assertJsCondition($condition, $timeout);
  }

}
+2 −16
Original line number Diff line number Diff line
@@ -110,8 +110,9 @@ protected function addBlock($block_name) {
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', "#drupal-off-canvas a:contains('$block_name')"));
    $page->clickLink($block_name);
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '[data-drupal-selector=\'edit-actions-submit\']'));

    $page->pressButton('Add block');
    $this->waitForNoElement('#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $assert_session->assertWaitOnAjaxRequest();
  }

@@ -148,19 +149,4 @@ protected function assertCorrectContextualLinksInNode() {
    $this->assertNotEmpty($page->findAll('css', '.layout-content [data-contextual-id]'));
  }

  /**
   * Waits for an element to be removed from the page.
   *
   * @param string $selector
   *   CSS selector.
   * @param int $timeout
   *   (optional) Timeout in milliseconds, defaults to 10000.
   *
   * @todo Remove in https://www.drupal.org/node/2892440.
   */
  protected function waitForNoElement($selector, $timeout = 10000) {
    $condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)";
    $this->assertJsCondition($condition, $timeout);
  }

}
+7 −5
Original line number Diff line number Diff line
@@ -116,8 +116,8 @@ protected function removeInlineBlockFromLayout() {
    $assert_session->waitForElement('css', "#drupal-off-canvas input[value='Remove']");
    $assert_session->assertWaitOnAjaxRequest();
    $page->find('css', '#drupal-off-canvas')->pressButton('Remove');
    $this->waitForNoElement('#drupal-off-canvas');
    $this->waitForNoElement(static::INLINE_BLOCK_LOCATOR);
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', static::INLINE_BLOCK_LOCATOR);
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->pageTextNotContains($block_text);
  }
@@ -167,7 +167,7 @@ protected function configureInlineBlock($old_body, $new_body, $block_css_locator
    $this->assertSame($old_body, $textarea->getValue());
    $textarea->setValue($new_body);
    $page->pressButton('Update');
    $this->waitForNoElement('#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $assert_session->assertWaitOnAjaxRequest();
    $this->assertDialogClosedAndTextVisible($new_body);
  }
@@ -180,9 +180,11 @@ protected function configureInlineBlock($old_body, $new_body, $block_css_locator
   * @param int $timeout
   *   (optional) Timeout in milliseconds, defaults to 10000.
   *
   * @todo Remove in https://www.drupal.org/node/2892440.
   * @deprecated in Drupal 8.8.x, will be removed before Drupal 9.0.0. Use
   *   Drupal\FunctionalJavascriptTests\JSWebAssert::assertNoElementAfterWait()
   */
  protected function waitForNoElement($selector, $timeout = 10000) {
    @trigger_error('::waitForNoElement is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal\FunctionalJavascriptTests\JSWebAssert::assertNoElementAfterWait() instead.', E_USER_DEPRECATED);
    $condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)";
    $this->assertJsCondition($condition, $timeout);
  }
@@ -197,7 +199,7 @@ protected function waitForNoElement($selector, $timeout = 10000) {
   */
  protected function assertDialogClosedAndTextVisible($text, $css_locator = NULL) {
    $assert_session = $this->assertSession();
    $this->waitForNoElement('#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->elementNotExists('css', '#drupal-off-canvas');
    if ($css_locator) {
+2 −33
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
namespace Drupal\Tests\layout_builder\FunctionalJavascript;

use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementHtmlException;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Render\FormattableMarkup;
@@ -206,7 +205,7 @@ protected function assertContextualLinksClickable() {
    $this->clickContextualLink('.block-field-blocknodebundle-with-section-fieldbody [data-contextual-id^="layout_builder_block"]', 'Configure');
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ui-dialog-titlebar [title="Close"]'));
    $page->pressButton('Close');
    $this->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');

    // Run the steps a second time after closing dialog, which reverses the
    // order that behaviors.layoutBuilderDisableInteractiveElements and
@@ -214,7 +213,7 @@ protected function assertContextualLinksClickable() {
    $this->clickContextualLink('.block-field-blocknodebundle-with-section-fieldbody [data-contextual-id^="layout_builder_block"]', 'Configure');
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '#drupal-off-canvas'));
    $page->pressButton('Close');
    $this->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $this->assertContextualLinkRetainsMouseup();
  }

@@ -303,34 +302,4 @@ protected function movePointerTo($selector) {
    $driver_session->moveto(['element' => $element->getID()]);
  }

  /**
   * Asserts that no matching element exists on the page after a wait.
   *
   * @param string $selector_type
   *   The element selector type (CSS, XPath).
   * @param string|array $selector
   *   The element selector.
   * @param int $timeout
   *   (optional) Timeout in milliseconds, defaults to 10000.
   * @param string $message
   *   (optional) The exception message.
   *
   * @throws \Behat\Mink\Exception\ElementHtmlException
   *   When an element still exists on the page.
   */
  public function assertNoElementAfterWait($selector_type, $selector, $timeout = 10000, $message = 'Element exists on the page.') {
    $start = microtime(TRUE);
    $end = $start + ($timeout / 1000);
    $page = $this->getSession()->getPage();
    do {
      $node = $page->find($selector_type, $selector);
      if (empty($node)) {
        return;
      }
      usleep(100000);
    } while (microtime(TRUE) < $end);

    throw new ElementHtmlException($message, $this->session->getDriver(), $node);
  }

}
Loading