3 namespace Drupal\Tests\system\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 * Base class contains common test functionality for the Off-canvas dialog.
10 abstract class OffCanvasTestBase extends WebDriverTestBase {
15 protected function drupalGet($path, array $options = [], array $headers = []) {
16 $return = parent::drupalGet($path, $options, $headers);
17 $this->assertPageLoadComplete();
22 * Assert the page is completely loaded.
24 * Ajax requests may happen after page loads. Also for users who have access
25 * to contextual links the contextual link placeholders will be filled after
26 * the page is received.
28 protected function assertPageLoadComplete() {
29 $this->assertSession()->assertWaitOnAjaxRequest();
30 if ($this->loggedInUser && $this->loggedInUser->hasPermission('access contextual links')) {
31 $this->assertAllContextualLinksLoaded();
36 * Assert all contextual link areas have be loaded.
38 * Contextual link placeholders will be filled after
39 * the page is received.
41 * @todo Move this function to https://www.drupal.org/node/2821724.
43 protected function assertAllContextualLinksLoaded() {
44 $this->waitForNoElement('[data-contextual-id]:empty');
50 * @param string $theme
53 protected function enableTheme($theme) {
55 \Drupal::service('theme_installer')->install([$theme]);
56 $theme_config = \Drupal::configFactory()->getEditable('system.theme');
57 $theme_config->set('default', $theme);
58 $theme_config->save();
62 * Waits for off-canvas dialog to open.
64 * @param string $position
65 * The position of the dialog.
67 * @throws \Behat\Mink\Exception\ElementNotFoundException
69 protected function waitForOffCanvasToOpen($position = 'side') {
70 $web_assert = $this->assertSession();
71 // Wait just slightly longer than the off-canvas dialog CSS animation.
72 // @see core/misc/dialog/off-canvas.motion.css
73 $this->getSession()->wait(800);
74 $web_assert->assertWaitOnAjaxRequest();
75 $this->assertElementVisibleAfterWait('css', '#drupal-off-canvas');
76 // Check that the canvas is positioned on the side.
77 $web_assert->elementExists('css', '.ui-dialog-position-' . $position);
81 * Waits for off-canvas dialog to close.
83 protected function waitForOffCanvasToClose() {
84 $this->waitForNoElement('#drupal-off-canvas');
88 * Gets the off-canvas dialog element.
90 * @return \Behat\Mink\Element\NodeElement|null
92 protected function getOffCanvasDialog() {
93 $off_canvas_dialog = $this->getSession()->getPage()->find('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]');
94 $this->assertEquals(FALSE, empty($off_canvas_dialog), 'The off-canvas dialog was found.');
95 return $off_canvas_dialog;
99 * Waits for an element to be removed from the page.
101 * @param string $selector
103 * @param int $timeout
104 * (optional) Timeout in milliseconds, defaults to 10000.
106 * @todo Remove in https://www.drupal.org/node/2892440.
108 protected function waitForNoElement($selector, $timeout = 10000) {
110 $start = microtime(TRUE);
111 $end = $start + ($timeout / 1000);
112 $page = $this->getSession()->getPage();
115 $result = $page->find('css', $selector);
117 if (empty($result)) {
122 } while (microtime(TRUE) < $end);
124 $this->assertEmpty($result, 'Element was not on the page after wait.');
128 * Get themes to test.
131 * Theme names to test.
133 protected function getTestThemes() {
134 return ['bartik', 'stark', 'classy', 'stable', 'seven'];
138 * Asserts the specified selector is visible after a wait.
140 * @param string $selector
141 * The selector engine name. See ElementInterface::findAll() for the
142 * supported selectors.
143 * @param string|array $locator
144 * The selector locator.
145 * @param int $timeout
146 * (Optional) Timeout in milliseconds, defaults to 10000.
148 protected function assertElementVisibleAfterWait($selector, $locator, $timeout = 10000) {
149 $this->assertSession()->assertWaitOnAjaxRequest();
150 $this->assertNotEmpty($this->assertSession()->waitForElementVisible($selector, $locator, $timeout));
154 * Dataprovider that returns theme name as the sole argument.
156 public function themeDataProvider() {
157 $themes = $this->getTestThemes();
159 foreach ($themes as $theme) {