reverted: --- b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * @file - * Testing behavior for JSWebAssertTest. - */ - -(function ($, Drupal, drupalSettings) { - - 'use strict'; - - /** - * @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 = { - attach: function (context) { - $('input[name="test_assert_wait_on_ajax_input"]').val('js_webassert_test'); - } - }; - -})(jQuery, Drupal, drupalSettings); diff -u b/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 --- b/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 @@ -1,7 +1,14 @@ -js_webassert_test: +wait_for_ajax_request: version: VERSION js: - js/js_webassert_test.js: {} + js/js_webassert_test.wait_for_ajax_request.js: {} + dependencies: + - core/jquery + - core/drupal +wait_for_element: + version: VERSION + js: + js/js_webassert_test.wait_for_element.js: {} dependencies: - core/jquery - core/drupal diff -u b/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 --- b/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 @@ -83,6 +83,21 @@ ]; // Button to test the assertWaitOnAjaxRequest() assertion. + $form['test_wait_for_element_visible'] = [ + '#type' => 'submit', + '#value' => $this->t('Test waitForElementVisible'), + '#button_type' => 'primary', + '#ajax' => [ + 'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addWaitForElementVisible', + 'progress' => [ + 'type' => 'throbber', + 'message' => NULL, + ], + 'wrapper' => 'js_webassert_test_form_wrapper', + ], + ]; + + // Button to test the assertWaitOnAjaxRequest() assertion. $form['test_assert_wait_on_ajax_request'] = [ '#type' => 'submit', '#value' => $this->t('Test assertWaitOnAjaxRequest'), @@ -151,9 +166,8 @@ * Ajax callback for the "Test waitForAjax" button. */ public static function addAssertWaitOnAjaxRequest(array $form, FormStateInterface $form_state) { - // Attach the library necessary for using the OpenModalDialogCommand and - // set the attachments for this Ajax response. - $form['#attached']['library'][] = 'js_webassert_test/js_webassert_test'; + // Attach the library necessary for this test. + $form['#attached']['library'][] = 'js_webassert_test/wait_for_ajax_request'; $form['test_assert_wait_on_ajax_input'] = [ '#type' => 'textfield', @@ -163,6 +177,26 @@ return $form; } + + /** + * Ajax callback for the "Test waitForElementVisible" button. + */ + public static function addWaitForElementVisible(array $form, FormStateInterface $form_state) { + // Attach the library necessary for this test. + $form['#attached']['library'][] = 'js_webassert_test/wait_for_element'; + + $form['element_invisible'] = [ + '#id' => 'js_webassert_test_element_invisible', + '#type' => 'submit', + '#value' => 'Added WaitForElementVisible', + '#button_type' => 'primary', + '#attributes' => [ + 'style' => ['display: none;'], + ], + ]; + return $form; + } + /** * {@inheritdoc} */ diff -u b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php --- b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php @@ -110,6 +110,11 @@ // Wait for the element to be in the DOM. $element = $this->waitForElement($selector, $locator, $timeout); + // Element not found in the DOM, return immediately. + if (empty($element)) { + return NULL; + } + $end = $start + $timeout / 1000.0; do { $result = $element->isVisible(); diff -u b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php --- b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php @@ -33,6 +33,7 @@ $test_field = $page->findButton('Add field'); $test_id = $page->findButton('Add ID'); $test_wait_on_ajax = $page->findButton('Test assertWaitOnAjaxRequest'); + $test_wait_on_element_visible = $page->findButton('Test waitForElementVisible'); // Test the wait...() methods by first checking the fields aren't available // and then are available after the wait method. @@ -69,6 +70,13 @@ $result = $page->findField('test_assert_wait_on_ajax_input'); $this->assertNotEmpty($result); $this->assertEquals('js_webassert_test', $result->getValue()); + + $test_wait_on_element_visible->click(); + $result = $page->findButton('Added WaitForElementVisible'); + $this->assertEmpty($result); + $result = $assert_session->waitForElementVisible('named', array('button', 'Added WaitForElementVisible')); + $this->assertNotEmpty($result); + $this->assertEquals(TRUE, $result->isVisible()); } } only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.wait_for_ajax_request.js @@ -0,0 +1,22 @@ +/** + * @file + * Testing behavior for JSWebAssertTest. + */ + +(function ($, Drupal, drupalSettings) { + + 'use strict'; + + /** + * @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: function (context) { + $('input[name="test_assert_wait_on_ajax_input"]').val('js_webassert_test'); + } + }; + +})(jQuery, Drupal, drupalSettings); only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.wait_for_element.js @@ -0,0 +1,22 @@ +/** + * @file + * Testing behavior for JSWebAssertTest. + */ + +(function ($, Drupal, drupalSettings) { + + 'use strict'; + + /** + * @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_element = { + attach: function (context) { + $('#js_webassert_test_element_invisible').show(); + } + }; + +})(jQuery, Drupal, drupalSettings); only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/FunctionalJavascriptTests/js/JSWebAssert.assertWaitOnAjaxRequest.js @@ -0,0 +1,12 @@ +(function() { + function IsNotAjaxing(instance, index, array) { + "use strict"; + return instance ? !instance.ajaxing : true; + } + return ( + // Assert no AJAX request is running (via jQuery or Drupal) and no + // animation is running. + (typeof jQuery === "undefined" || (jQuery.active === 0 && jQuery(':animated').length === 0)) && + (typeof Drupal === 'undefined' || typeof Drupal.ajax === 'undefined' || Drupal.ajax.instances.every(IsNotAjaxing)) + ); +}()); \ No newline at end of file