3 namespace Drupal\Tests\system\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
8 * Base class contains common test functionality for the Off-canvas dialog.
10 abstract class OffCanvasTestBase extends JavascriptTestBase {
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 protected function waitForOffCanvasToOpen() {
65 $web_assert = $this->assertSession();
66 // Wait just slightly longer than the off-canvas dialog CSS animation.
67 // @see core/misc/dialog/off-canvas.motion.css
68 $this->getSession()->wait(800);
69 $web_assert->assertWaitOnAjaxRequest();
70 $this->assertElementVisibleAfterWait('css', '#drupal-off-canvas');
74 * Waits for off-canvas dialog to close.
76 protected function waitForOffCanvasToClose() {
77 $this->waitForNoElement('#drupal-off-canvas');
81 * Gets the off-canvas dialog element.
83 * @return \Behat\Mink\Element\NodeElement|null
85 protected function getOffCanvasDialog() {
86 $off_canvas_dialog = $this->getSession()->getPage()->find('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]');
87 $this->assertEquals(FALSE, empty($off_canvas_dialog), 'The off-canvas dialog was found.');
88 return $off_canvas_dialog;
92 * Waits for an element to be removed from the page.
94 * @param string $selector
97 * (optional) Timeout in milliseconds, defaults to 10000.
99 * @todo Remove in https://www.drupal.org/node/2892440.
101 protected function waitForNoElement($selector, $timeout = 10000) {
102 $condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)";
103 $this->assertJsCondition($condition, $timeout);
107 * Get themes to test.
110 * Theme names to test.
112 protected function getTestThemes() {
113 return ['bartik', 'stark', 'classy', 'stable', 'seven'];
117 * Asserts the specified selector is visible after a wait.
119 * @param string $selector
120 * The selector engine name. See ElementInterface::findAll() for the
121 * supported selectors.
122 * @param string|array $locator
123 * The selector locator.
124 * @param int $timeout
125 * (Optional) Timeout in milliseconds, defaults to 10000.
127 protected function assertElementVisibleAfterWait($selector, $locator, $timeout = 10000) {
128 $this->assertNotEmpty($this->assertSession()->waitForElementVisible($selector, $locator, $timeout));