diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFormMessagesTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFormMessagesTest.php index 6c977c34e39e..8a86fa3ecc7e 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFormMessagesTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFormMessagesTest.php @@ -71,7 +71,7 @@ class BlockFormMessagesTest extends WebDriverTestBase { $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 @@ class BlockFormMessagesTest extends WebDriverTestBase { $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. */ diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/ContentPreviewToggleTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/ContentPreviewToggleTest.php index 2b1395c48ce6..46b78b23df9b 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/ContentPreviewToggleTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/ContentPreviewToggleTest.php @@ -78,8 +78,7 @@ class ContentPreviewToggleTest extends WebDriverTestBase { $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 @@ class ContentPreviewToggleTest extends WebDriverTestBase { $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 @@ class ContentPreviewToggleTest extends WebDriverTestBase { $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); - } - } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php index 859805b35458..ea0d170085c7 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php @@ -110,8 +110,9 @@ class ContextualLinksTest extends WebDriverTestBase { $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 @@ class ContextualLinksTest extends WebDriverTestBase { $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); - } - } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php index fa8650a5de0b..971a7308d092 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php @@ -116,8 +116,8 @@ abstract class InlineBlockTestBase extends WebDriverTestBase { $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 @@ abstract class InlineBlockTestBase extends WebDriverTestBase { $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 @@ abstract class InlineBlockTestBase extends WebDriverTestBase { * @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 @@ abstract class InlineBlockTestBase extends WebDriverTestBase { */ 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) { diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderDisableInteractionsTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderDisableInteractionsTest.php index a32ff7d232da..8dcd507c8dac 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderDisableInteractionsTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderDisableInteractionsTest.php @@ -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 @@ class LayoutBuilderDisableInteractionsTest extends WebDriverTestBase { $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 @@ class LayoutBuilderDisableInteractionsTest extends WebDriverTestBase { $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 @@ class LayoutBuilderDisableInteractionsTest extends WebDriverTestBase { $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); - } - } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php index e2341ca2af9f..7c0830a79ddb 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php @@ -125,7 +125,7 @@ class LayoutBuilderTest extends WebDriverTestBase { // Save the new block, and ensure it is displayed on the page. $page->pressButton('Add block'); $assert_session->assertWaitOnAjaxRequest(); - $this->assertNoElementAfterWait('#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $assert_session->addressEquals($layout_url); $assert_session->pageTextContains('Powered by Drupal'); $assert_session->pageTextContains('This is the label'); @@ -158,7 +158,7 @@ class LayoutBuilderTest extends WebDriverTestBase { $page->pressButton('Add section'); $assert_session->assertWaitOnAjaxRequest(); - $this->assertNoElementAfterWait('.layout__region--second .block-system-powered-by-block'); + $assert_session->assertNoElementAfterWait('css', '.layout__region--second .block-system-powered-by-block'); $assert_session->elementTextNotContains('css', '.layout__region--second', 'Powered by Drupal'); // Drag the block to a region in different section. @@ -190,7 +190,7 @@ class LayoutBuilderTest extends WebDriverTestBase { $page->fillField('settings[label]', 'This is the new label'); $page->pressButton('Update'); $assert_session->assertWaitOnAjaxRequest(); - $this->assertNoElementAfterWait('#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $assert_session->addressEquals($layout_url); $assert_session->pageTextContains('Powered by Drupal'); @@ -204,7 +204,7 @@ class LayoutBuilderTest extends WebDriverTestBase { $assert_session->pageTextContains('This action cannot be undone.'); $page->pressButton('Remove'); $assert_session->assertWaitOnAjaxRequest(); - $this->assertNoElementAfterWait('#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $assert_session->pageTextNotContains('Powered by Drupal'); $assert_session->linkExists('Add block'); @@ -296,7 +296,7 @@ class LayoutBuilderTest extends WebDriverTestBase { $page->pressButton('Add section'); $assert_session->assertWaitOnAjaxRequest(); - $this->assertNoElementAfterWait('#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $assert_session->pageTextContains('Default'); $assert_session->linkExists('Add block'); @@ -307,7 +307,7 @@ class LayoutBuilderTest extends WebDriverTestBase { $page->fillField('layout_settings[setting_1]', 'Test setting value'); $page->pressButton('Update'); $assert_session->assertWaitOnAjaxRequest(); - $this->assertNoElementAfterWait('#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $assert_session->pageTextContains('Test setting value'); $this->assertPageNotReloaded(); } @@ -350,7 +350,7 @@ class LayoutBuilderTest extends WebDriverTestBase { 'region' => 'content', 'plugin_id' => 'system_powered_by_block', ])); - $this->assertNoElementAfterWait('#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $page->fillField('settings[label]', 'The block label'); $page->fillField('settings[label_display]', TRUE); $page->pressButton('Add block'); @@ -372,28 +372,6 @@ class LayoutBuilderTest extends WebDriverTestBase { $assert_session->linkNotExists('Add block'); } - /** - * 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. - * @param string $message - * (optional) Custom message to display with the assertion. - * - * @todo: Remove after https://www.drupal.org/project/drupal/issues/2892440 - */ - public function assertNoElementAfterWait($selector, $timeout = 10000, $message = '') { - $page = $this->getSession()->getPage(); - if ($message === '') { - $message = "Element '$selector' was not on the page after wait."; - } - $this->assertTrue($page->waitFor($timeout / 1000, function () use ($page, $selector) { - return empty($page->find('css', $selector)); - }), $message); - } - /** * {@inheritdoc} * diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php index d2f94920a2fc..1f39a538a4de 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php @@ -254,23 +254,10 @@ class LayoutBuilderUiTest extends WebDriverTestBase { * Waits for the dialog to close and confirms no highlights are present. */ private function assertHighlightNotExists() { - $this->waitForNoElement('#drupal-off-canvas'); - $this->waitForNoElement('.is-layout-builder-highlighted'); - } + $assert_session = $this->assertSession(); - /** - * 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); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '.is-layout-builder-highlighted'); } } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php index e68ff5ad5bbb..e8cf15f4074b 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php @@ -83,10 +83,10 @@ class MoveBlockFormTest extends WebDriverTestBase { $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'input[value="Add block"]')); $assert_session->assertWaitOnAjaxRequest(); $page->pressButton('Add block'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $this->assertNotEmpty($assert_session->waitForElementVisible('css', $first_region_block_locator)); // Ensure the request has completed before the test starts. - $this->waitForNoElement('#drupal-off-canvas'); $assert_session->assertWaitOnAjaxRequest(); } @@ -152,21 +152,6 @@ class MoveBlockFormTest extends WebDriverTestBase { } } - /** - * 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); - } - /** * Moves a block in the draggable table. * @@ -221,7 +206,7 @@ class MoveBlockFormTest extends WebDriverTestBase { $assert_session = $this->assertSession(); $assert_session->assertWaitOnAjaxRequest(); - $this->waitForNoElement('#drupal-off-canvas'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); $region_selector = "[data-layout-delta=\"$section_delta\"] [data-region=\"$region\"]"; diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/TestMultiWidthLayoutsTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/TestMultiWidthLayoutsTest.php index 996170128ad6..ecfcf27a0501 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/TestMultiWidthLayoutsTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/TestMultiWidthLayoutsTest.php @@ -101,25 +101,10 @@ class TestMultiWidthLayoutsTest extends WebDriverTestBase { $this->clickLink('Remove section'); $this->assertNotEmpty($assert_session->waitForElementVisible('css', '#drupal-off-canvas input[type="submit"][value="Remove"]')); $page->pressButton('Remove'); - $this->waitForNoElement(".$width_class"); + $assert_session->assertNoElementAfterWait('css', ".$width_class"); } } - /** - * 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 the width class is applied to the first section. * diff --git a/core/modules/quickedit/tests/src/FunctionalJavascript/FieldTest.php b/core/modules/quickedit/tests/src/FunctionalJavascript/FieldTest.php index 7555220acfbe..56d303d659de 100644 --- a/core/modules/quickedit/tests/src/FunctionalJavascript/FieldTest.php +++ b/core/modules/quickedit/tests/src/FunctionalJavascript/FieldTest.php @@ -92,22 +92,9 @@ class FieldTest extends WebDriverTestBase { // Wait and click by "Save" button after body field was changed. $this->assertSession()->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click(); // Wait until the save occurs and the editor UI disappears. - $this->waitForNoElement('.cke_button.cke_button__blockquote'); + $this->assertSession()->assertNoElementAfterWait('css', '.cke_button.cke_button__blockquote'); // Ensure that the changes take effect. $assert->responseMatches("|
\s*$body_value\s*
|"); } - /** - * 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. - */ - protected function waitForNoElement($selector, $timeout = 10000) { - $condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)"; - $this->assertJsCondition($condition, $timeout); - } - } diff --git a/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php b/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php index a5db109f34c6..be89b7c576d4 100644 --- a/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php +++ b/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php @@ -79,14 +79,14 @@ class SettingsTrayBlockFormTest extends SettingsTrayTestBase { if ($element = $page->find('css', "#toolbar-administration a.is-active")) { // If a tray was open from page load close it. $element->click(); - $this->waitForNoElement("#toolbar-administration a.is-active"); + $web_assert->assertNoElementAfterWait('css', "#toolbar-administration a.is-active"); } $page->find('css', $toolbar_item)->click(); $this->assertElementVisibleAfterWait('css', "{$toolbar_item}.is-active"); } $this->enableEditMode(); if (isset($toolbar_item)) { - $this->waitForNoElement("{$toolbar_item}.is-active"); + $web_assert->assertNoElementAfterWait('css', "{$toolbar_item}.is-active"); } $this->openBlockForm($block_selector); switch ($block_plugin) { @@ -144,7 +144,7 @@ class SettingsTrayBlockFormTest extends SettingsTrayTestBase { $this->getSession()->getPage()->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver(); $this->assertEditModeDisabled(); $this->assertNotEmpty($web_assert->waitForElement('css', '#drupal-live-announce:contains(Exited edit mode)')); - $this->waitForNoElement('.contextual-toolbar-tab button:contains(Editing)'); + $web_assert->assertNoElementAfterWait('css', '.contextual-toolbar-tab button:contains(Editing)'); $web_assert->elementAttributeNotContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode'); // Clean up test data so each test does not impact the next. diff --git a/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.no_element_after_wait.es6.js b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.no_element_after_wait.es6.js new file mode 100644 index 000000000000..984125d8af9e --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.no_element_after_wait.es6.js @@ -0,0 +1,30 @@ +/** + * @file + * Testing behavior for JSWebAssertTest. + */ + +(($, Drupal) => { + /** + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Makes changes in the DOM to be able to test the completion of AJAX in assertWaitOnAjaxRequest. + */ + Drupal.behaviors.js_webassert_test_wait_for_ajax_request = { + attach() { + $('#edit-test-assert-no-element-after-wait-pass').on('click', e => { + e.preventDefault(); + setTimeout(() => { + $('#edit-test-assert-no-element-after-wait-pass').remove(); + }, 500); + }); + + $('#edit-test-assert-no-element-after-wait-fail').on('click', e => { + e.preventDefault(); + setTimeout(() => { + $('#edit-test-assert-no-element-after-wait-fail').remove(); + }, 2000); + }); + }, + }; +})(jQuery, Drupal); diff --git a/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.no_element_after_wait.js b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.no_element_after_wait.js new file mode 100644 index 000000000000..c529bb3e1230 --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.no_element_after_wait.js @@ -0,0 +1,26 @@ +/** +* DO NOT EDIT THIS FILE. +* See the following change record for more information, +* https://www.drupal.org/node/2815083 +* @preserve +**/ + +(function ($, Drupal) { + Drupal.behaviors.js_webassert_test_wait_for_ajax_request = { + attach: function attach() { + $('#edit-test-assert-no-element-after-wait-pass').on('click', function (e) { + e.preventDefault(); + setTimeout(function () { + $('#edit-test-assert-no-element-after-wait-pass').remove(); + }, 500); + }); + + $('#edit-test-assert-no-element-after-wait-fail').on('click', function (e) { + e.preventDefault(); + setTimeout(function () { + $('#edit-test-assert-no-element-after-wait-fail').remove(); + }, 2000); + }); + } + }; +})(jQuery, Drupal); \ No newline at end of file diff --git a/core/modules/system/tests/modules/js_webassert_test/js_webassert_test.libraries.yml b/core/modules/system/tests/modules/js_webassert_test/js_webassert_test.libraries.yml index 3a17b1dcf402..898e6375078a 100644 --- a/core/modules/system/tests/modules/js_webassert_test/js_webassert_test.libraries.yml +++ b/core/modules/system/tests/modules/js_webassert_test/js_webassert_test.libraries.yml @@ -12,3 +12,11 @@ wait_for_element: dependencies: - core/jquery - core/drupal + +no_element_after_wait: + version: VERSION + js: + js/js_webassert_test.no_element_after_wait.js: {} + dependencies: + - core/jquery + - core/drupal diff --git a/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php b/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php index 3197aa5bef60..c8815a11f735 100644 --- a/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php +++ b/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php @@ -113,6 +113,22 @@ class JsWebAssertTestForm extends FormBase { 'wrapper' => 'js_webassert_test_form_wrapper', ], ]; + + // Button to test the assertNoElementAfterWait() assertion, will pass. + $form['test_assert_no_element_after_wait_pass'] = [ + '#type' => 'submit', + '#value' => $this->t('Test assertNoElementAfterWait: pass'), + '#button_type' => 'primary', + '#attached' => ['library' => 'js_webassert_test/no_element_after_wait'], + ]; + + // Button to test the assertNoElementAfterWait() assertion, will fail. + $form['test_assert_no_element_after_wait_fail'] = [ + '#type' => 'submit', + '#value' => $this->t('Test assertNoElementAfterWait: fail'), + '#button_type' => 'primary', + ]; + return $form; } diff --git a/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTest.php b/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTest.php index c42d777c135e..4eac4ec6c7ea 100644 --- a/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTest.php +++ b/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTest.php @@ -156,7 +156,7 @@ class OffCanvasTest extends OffCanvasTestBase { // Click the first test like that should open the page. $page->clickLink($link_text); if ($this->lastDialogClass) { - $this->waitForNoElement('.' . $this->lastDialogClass); + $web_assert->assertNoElementAfterWait('css', '.' . $this->lastDialogClass); } $this->waitForOffCanvasToOpen($position); $this->lastDialogClass = "$position-$link_index"; diff --git a/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTestBase.php b/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTestBase.php index b2e6c2b65d84..fc3fa2331604 100644 --- a/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTestBase.php +++ b/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTestBase.php @@ -41,7 +41,7 @@ abstract class OffCanvasTestBase extends WebDriverTestBase { * @todo Move this function to https://www.drupal.org/node/2821724. */ protected function assertAllContextualLinksLoaded() { - $this->waitForNoElement('[data-contextual-id]:empty'); + $this->assertSession()->assertNoElementAfterWait('css', '[data-contextual-id]:empty'); } /** @@ -81,7 +81,7 @@ abstract class OffCanvasTestBase extends WebDriverTestBase { * Waits for off-canvas dialog to close. */ protected function waitForOffCanvasToClose() { - $this->waitForNoElement('#drupal-off-canvas'); + $this->assertSession()->assertNoElementAfterWait('css', '#drupal-off-canvas'); } /** @@ -103,25 +103,12 @@ abstract class OffCanvasTestBase extends WebDriverTestBase { * @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) { - - $start = microtime(TRUE); - $end = $start + ($timeout / 1000); - $page = $this->getSession()->getPage(); - - do { - $result = $page->find('css', $selector); - - if (empty($result)) { - return; - } - - usleep(100000); - } while (microtime(TRUE) < $end); - - $this->assertEmpty($result, 'Element was not on the page after wait.'); + @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); + $this->assertSession()->assertNoElementAfterWait('css', $selector, $timeout); } /** diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/ThrobberTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/ThrobberTest.php index 56715e6dd135..3ece94942b67 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/ThrobberTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/ThrobberTest.php @@ -59,7 +59,7 @@ JS; JS; $this->drupalGet('admin/structure/views/view/content'); - $this->waitForNoElement('.ajax-progress-fullscreen'); + $web_assert->assertNoElementAfterWait('css', '.ajax-progress-fullscreen'); // Test theming fullscreen throbber. $session->executeScript($custom_ajax_progress_indicator_fullscreen); @@ -67,7 +67,7 @@ JS; $page->clickLink('Content: Published (grouped)'); $this->assertNotNull($web_assert->waitForElement('css', '.custom-ajax-progress-fullscreen'), 'Custom ajaxProgressIndicatorFullscreen.'); hold_test_response(FALSE); - $this->waitForNoElement('.custom-ajax-progress-fullscreen'); + $web_assert->assertNoElementAfterWait('css', '.custom-ajax-progress-fullscreen'); // Test theming throbber message. $web_assert->waitForElementVisible('css', '[data-drupal-selector="edit-options-group-info-add-group"]'); @@ -76,7 +76,7 @@ JS; $page->pressButton('Add another item'); $this->assertNotNull($web_assert->waitForElement('css', '.ajax-progress-throbber .custom-ajax-progress-message'), 'Custom ajaxProgressMessage.'); hold_test_response(FALSE); - $this->waitForNoElement('.ajax-progress-throbber'); + $web_assert->assertNoElementAfterWait('css', '.ajax-progress-throbber'); // Test theming throbber. $web_assert->waitForElementVisible('css', '[data-drupal-selector="edit-options-group-info-group-items-3-title"]'); @@ -85,22 +85,7 @@ JS; $page->pressButton('Add another item'); $this->assertNotNull($web_assert->waitForElement('css', '.custom-ajax-progress-throbber'), 'Custom ajaxProgressThrobber.'); hold_test_response(FALSE); - $this->waitForNoElement('.custom-ajax-progress-throbber'); - } - - /** - * 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); + $web_assert->assertNoElementAfterWait('css', '.custom-ajax-progress-throbber'); } } diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php index 959b663d20fe..92f40091ef39 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php @@ -387,4 +387,35 @@ JS; return $this->session->evaluateScript($full_javascript_visibility_test); } + /** + * 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->session->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); + } + } diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php index 6dd591f5fe4f..fa3a3d1aac23 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php @@ -3,6 +3,7 @@ namespace Drupal\FunctionalJavascriptTests\Tests; use Behat\Mink\Element\NodeElement; +use Behat\Mink\Exception\ElementHtmlException; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; /** @@ -29,6 +30,22 @@ class JSWebAssertTest extends WebDriverTestBase { $assert_session = $this->assertSession(); $page = $session->getPage(); + $assert_session->elementExists('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-pass"]'); + $page->findButton('Test assertNoElementAfterWait: pass')->press(); + $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-pass"]', 1000); + + $assert_session->elementExists('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]'); + $page->findButton('Test assertNoElementAfterWait: fail')->press(); + try { + $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]', 500, 'Element exists on page after too short wait.'); + $this->fail('Element not exists on page after too short wait.'); + } + catch (ElementHtmlException $e) { + $this->assertSame('Element exists on page after too short wait.', $e->getMessage()); + } + + $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]', 2500, 'Element remove after another wait.ss'); + $test_button = $page->findButton('Add button'); $test_link = $page->findButton('Add link'); $test_field = $page->findButton('Add field');