diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php index 6dbe8eb924..748d0fbd5e 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php @@ -118,8 +118,7 @@ public function testLayoutBuilderUi() { $assert_session->linkExists('Powered by Drupal'); $this->clickLink('Powered by Drupal'); - $assert_session->assertWaitOnAjaxRequest(); - $assert_session->elementExists('css', '#drupal-off-canvas'); + $this->waitForOffCanvasForm('layout_builder_add_block'); $page->fillField('settings[label]', 'This is the label'); $page->checkField('settings[label_display]'); @@ -188,8 +187,7 @@ public function testLayoutBuilderUi() { $this->markCurrentPage(); $this->clickContextualLink('.block-system-powered-by-block', 'Configure'); - $assert_session->assertWaitOnAjaxRequest(); - $assert_session->elementExists('css', '#drupal-off-canvas'); + $this->waitForOffCanvasForm('layout_builder_update_block'); $page->fillField('settings[label]', 'This is the new label'); $page->pressButton('Update'); @@ -203,8 +201,7 @@ public function testLayoutBuilderUi() { // Remove a block. $this->clickContextualLink('.block-system-powered-by-block', 'Remove block'); - $assert_session->assertWaitOnAjaxRequest(); - $assert_session->elementExists('css', '#drupal-off-canvas'); + $this->waitForOffCanvasForm('layout_builder_remove_block'); $page->pressButton('Remove'); $assert_session->assertWaitOnAjaxRequest(); @@ -229,8 +226,7 @@ public function testLayoutBuilderUi() { $assert_session->linkExists('My custom block'); $this->clickLink('My custom block'); - $assert_session->assertWaitOnAjaxRequest(); - + $this->waitForOffCanvasForm('layout_builder_add_block'); $page->pressButton('Add Block'); $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextContains('This is the block content'); @@ -238,15 +234,13 @@ public function testLayoutBuilderUi() { // Remove both sections. $assert_session->linkExists('Remove section'); $this->clickLink('Remove section'); - $assert_session->assertWaitOnAjaxRequest(); - + $this->waitForOffCanvasForm('layout_builder_remove_section'); $page->pressButton('Remove'); $assert_session->assertWaitOnAjaxRequest(); $assert_session->linkExists('Remove section'); $this->clickLink('Remove section'); - $assert_session->assertWaitOnAjaxRequest(); - + $this->waitForOffCanvasForm('layout_builder_remove_section'); $page->pressButton('Remove'); $assert_session->assertWaitOnAjaxRequest(); @@ -294,7 +288,7 @@ public function testConfigurableLayouts() { $assert_session->linkExists('Layout plugin (with settings)'); $this->clickLink('Layout plugin (with settings)'); - $assert_session->assertWaitOnAjaxRequest(); + $this->waitForOffCanvasForm('layout_builder_configure_section'); $assert_session->fieldExists('layout_settings[setting_1]'); $page->pressButton('Add section'); $assert_session->assertWaitOnAjaxRequest(); @@ -306,7 +300,7 @@ public function testConfigurableLayouts() { // Configure the existing section. $assert_session->linkExists('Configure section'); $this->clickLink('Configure section'); - $assert_session->assertWaitOnAjaxRequest(); + $this->waitForOffCanvasForm('layout_builder_configure_section'); $page->fillField('layout_settings[setting_1]', 'Test setting value'); $page->pressButton('Update'); $assert_session->assertWaitOnAjaxRequest(); @@ -374,8 +368,8 @@ protected function clickContextualLink($selector, $link_locator, $force_visible $assert_session = $this->assertSession(); if ($force_visible) { - $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').removeClass('visually-hidden');"); - $assert_session->assertWaitOnAjaxRequest(); + $this->getSession()->executeScript("jQuery('{$selector} .contextual button').removeClass('visually-hidden');"); + $assert_session->waitForElementVisible('css', '.contextual button'); } $element = $this->getSession()->getPage()->find('css', $selector); @@ -387,7 +381,7 @@ protected function clickContextualLink($selector, $link_locator, $force_visible // If the link is not visible, click the contextual link button first. if (!$link->isVisible()) { $element->find('css', '.contextual button')->press(); - $assert_session->assertWaitOnAjaxRequest(); + $assert_session->waitForLink($link_locator); } $this->assertTrue($link->isVisible(), "Link $link_locator is visible."); $link->click(); @@ -399,4 +393,33 @@ protected function clickContextualLink($selector, $link_locator, $force_visible } } + /** + * Waits for the specified form and returns it when available and visible. + * + * @param string $expected_form_id + * The expected form ID. + * @param int $timeout + * (Optional) Timeout in milliseconds, defaults to 10000. + * + * @return \Behat\Mink\Element\NodeElement|null + * The form element if found and visible, NULL if not. + */ + protected function waitForOffCanvasForm($expected_form_id, $timeout = 10000) { + $page = $this->getSession()->getPage(); + return $page->waitFor($timeout / 1000, function () use ($page, $expected_form_id) { + // Ensure the form ID exists, is visible, and has the correct value. + $form_id_element = $page->find('hidden_field_selector', ['hidden_field', 'form_id']); + if (!$form_id_element || !$form_id_element->isVisible() || $expected_form_id !== $form_id_element->getValue()) { + return NULL; + } + + // Ensure the off canvas dialog is visible. + $off_canvas = $page->find('css', '#drupal-off-canvas'); + if (!$off_canvas || !$off_canvas->isVisible()) { + return NULL; + } + return $form_id_element; + }); + } + }