OutsideInJavascriptTestBase.php 3.95 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\Tests\outside_in\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\JavascriptTestBase;

/**
8
 * Base class contains common test functionality for the Settings Tray module.
9 10 11
 */
abstract class OutsideInJavascriptTestBase extends JavascriptTestBase {

12 13 14
  /**
   * {@inheritdoc}
   */
15
  protected function drupalGet($path, array $options = [], array $headers = []) {
16 17 18 19 20 21 22 23 24
    $return = parent::drupalGet($path, $options, $headers);

    // After the page loaded we need to additionally wait until the settings
    // tray Ajax activity is done.
    $this->assertSession()->assertWaitOnAjaxRequest();

    return $return;
  }

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  /**
   * Enables a theme.
   *
   * @param string $theme
   *   The theme.
   */
  public function enableTheme($theme) {
    // Enable the theme.
    \Drupal::service('theme_installer')->install([$theme]);
    $theme_config = \Drupal::configFactory()->getEditable('system.theme');
    $theme_config->set('default', $theme);
    $theme_config->save();
  }

  /**
   * Waits for Off-canvas tray to open.
   */
  protected function waitForOffCanvasToOpen() {
43 44
    $web_assert = $this->assertSession();
    $web_assert->assertWaitOnAjaxRequest();
45
    $web_assert->waitForElementVisible('css', '#drupal-offcanvas');
46 47 48 49 50 51
  }

  /**
   * Waits for Off-canvas tray to close.
   */
  protected function waitForOffCanvasToClose() {
52
    $this->waitForNoElement('#drupal-offcanvas');
53 54 55 56 57 58 59 60
  }

  /**
   * Gets the Off-Canvas tray element.
   *
   * @return \Behat\Mink\Element\NodeElement|null
   */
  protected function getTray() {
61 62 63
    $tray = $this->getSession()->getPage()->find('css', '.ui-dialog[aria-describedby="drupal-offcanvas"]');
    $this->assertEquals(FALSE, empty($tray), 'The tray was found.');
    return $tray;
64 65
  }

66 67 68 69 70 71
  /**
   * Waits for an element to be removed from the page.
   *
   * @param string $selector
   *   CSS selector.
   * @param int $timeout
72
   *   (optional) Timeout in milliseconds, defaults to 10000.
73
   */
74
  protected function waitForNoElement($selector, $timeout = 10000) {
75 76 77 78
    $condition = "(jQuery('$selector').length == 0)";
    $this->assertJsCondition($condition, $timeout);
  }

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  /**
   * Clicks a contextual link.
   *
   * @todo Remove this function when related trait added in
   *   https://www.drupal.org/node/2821724.
   *
   * @param string $selector
   *   The selector for the element that contains the contextual link.
   * @param string $link_locator
   *   The link id, title, or text.
   * @param bool $force_visible
   *   If true then the button will be forced to visible so it can be clicked.
   */
  protected function clickContextualLink($selector, $link_locator, $force_visible = TRUE) {
    if ($force_visible) {
      $this->toggleContextualTriggerVisibility($selector);
    }

    $element = $this->getSession()->getPage()->find('css', $selector);
    $element->find('css', '.contextual button')->press();
    $element->findLink($link_locator)->click();

    if ($force_visible) {
      $this->toggleContextualTriggerVisibility($selector);
    }
  }

  /**
   * Toggles the visibility of a contextual trigger.
   *
   * @todo Remove this function when related trait added in
   *   https://www.drupal.org/node/2821724.
   *
   * @param string $selector
   *   The selector for the element that contains the contextual link.
   */
  protected function toggleContextualTriggerVisibility($selector) {
    // Hovering over the element itself with should be enough, but does not
    // work. Manually remove the visually-hidden class.
    $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').toggleClass('visually-hidden');");
  }

121 122 123 124 125 126 127 128 129 130 131 132
  /**
   * Waits for Toolbar to load.
   */
  protected function waitForToolbarToLoad() {
    $web_assert = $this->assertSession();
    // Waiting for Toolbar module.
    // @todo Remove the hack after https://www.drupal.org/node/2542050.
    $web_assert->waitForElementVisible('css', '.toolbar-fixed');
    // Waiting for Toolbar animation.
    $web_assert->assertWaitOnAjaxRequest();
  }

133
}