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 @@ -77,13 +77,13 @@ * * @see \Behat\Mink\Element\ElementInterface::findAll() */ - protected function waitForElement($selector, $locator, $timeout = 10000) { + public function waitForElement($selector, $locator, $timeout = 10000) { $page = $this->session->getPage(); $xpath = $this->session->getSelectorsHandler()->selectorToXpath($selector, $locator); $xpath = $this->xpathManipulator->prepend($xpath, $page->getXpath()); $condition = 'document.evaluate("' . str_replace(PHP_EOL, '', $xpath) . '", document, null, XPathResult.BOOLEAN_TYPE, null).booleanValue'; - $this->assertJsCondition($condition, $timeout); + $this->session->wait($timeout, $condition); return $page->find($selector, $locator); } @@ -98,7 +98,7 @@ * @return \Behat\Mink\Element\NodeElement|null * The page element node if found, NULL if not. */ - protected function waitForButton($locator) { + public function waitForButton($locator) { return $this->waitForElement('named', array('button', $locator)); } @@ -111,7 +111,7 @@ * @return \Behat\Mink\Element\NodeElement|null * The page element node if found, NULL if not. */ - protected function waitForLink($locator) { + public function waitForLink($locator) { return $this->waitForElement('named', array('link', $locator)); } @@ -124,7 +124,7 @@ * @return \Behat\Mink\Element\NodeElement|null * The page element node if found, NULL if not. */ - protected function waitForField($locator) { + public function waitForField($locator) { return $this->waitForElement('named', array('field', $locator)); } @@ -137,7 +137,7 @@ * @return \Behat\Mink\Element\NodeElement|null * The page element node if found, NULL if not. */ - protected function waitForId($id) { + public function waitForId($id) { return $this->waitForElement('named', array('id', $id)); } @@ -145,6 +145,8 @@ * Waits for the jQuery autocomplete delay duration. * * @see https://api.jqueryui.com/autocomplete/#option-delay + * + * @deprecated in Drupal 8.3.x, will be removed before Drupal 9.0.0. */ public function waitOnAutocomplete() { // Wait for the autocomplete to be visible. only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/js/js_webassert_test.js @@ -0,0 +1,22 @@ +/** + * @file + * Attaches the behaviors for the Field UI module. + */ + +(function ($, Drupal, drupalSettings) { + + 'use strict'; + + /** + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Adds behaviors to the field storage add form. + */ + Drupal.behaviors.js_webassert_test = { + 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_webassert_test.info.yml @@ -0,0 +1,6 @@ +name: JS WebAssert test module +type: module +description: 'Module for the JSWebAssert test.' +package: Core +version: VERSION +core: 8.x \ No newline at end of file only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/js_webassert_test.libraries.yml @@ -0,0 +1,7 @@ +js_webassert_test: + version: VERSION + js: + js/js_webassert_test.js: {} + dependencies: + - core/jquery + - core/drupal \ No newline at end of file only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/js_webassert_test.routing.yml @@ -0,0 +1,7 @@ +js_webassert_test.js_webassert_test_form: + path: '/js_webassert_test_form' + defaults: + _form: 'Drupal\js_webassert_test\Form\JsWebAssertTestForm' + _title: 'JsWebAssertForm' + requirements: + _access: 'TRUE' \ No newline at end of file only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php @@ -0,0 +1,173 @@ +'; + $form['#suffix'] = ''; + + // Button to test for the waitForButton() assertion. + $form['test_button'] = [ + '#type' => 'submit', + '#value' => $this->t('Add button'), + '#button_type' => 'primary', + '#ajax' => [ + 'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addButton', + 'progress' => [ + 'type' => 'throbber', + 'message' => NULL, + ], + 'wrapper' => 'js_webassert_test_form_wrapper', + ], + ]; + // Button to test for the waitForLink() assertion. + $form['test_link'] = [ + '#type' => 'submit', + '#value' => $this->t('Add link'), + '#button_type' => 'primary', + '#ajax' => [ + 'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addLink', + 'progress' => [ + 'type' => 'throbber', + 'message' => NULL, + ], + 'wrapper' => 'js_webassert_test_form_wrapper', + ], + ]; + // Button to test for the waitForField() assertion. + $form['test_field'] = [ + '#type' => 'submit', + '#value' => $this->t('Add field'), + '#button_type' => 'primary', + '#ajax' => [ + 'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addField', + 'progress' => [ + 'type' => 'throbber', + 'message' => NULL, + ], + 'wrapper' => 'js_webassert_test_form_wrapper', + ], + ]; + // Button to test for the waitForId() assertion. + $form['test_id'] = [ + '#type' => 'submit', + '#value' => $this->t('Add ID'), + '#button_type' => 'primary', + '#ajax' => [ + 'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addId', + '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'), + '#button_type' => 'primary', + '#ajax' => [ + 'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addAssertWaitOnAjaxRequest', + 'progress' => [ + 'type' => 'throbber', + 'message' => NULL, + ], + 'wrapper' => 'js_webassert_test_form_wrapper', + ], + ]; + return $form; + } + + /** + * Ajax callback for the "Add button" button. + */ + public static function addButton(array $form, FormStateInterface $form_state) { + $form['added_button'] = [ + '#type' => 'submit', + '#value' => 'Added button', + '#button_type' => 'primary', + ]; + return $form; + } + + /** + * Ajax callback for the "Add link" button. + */ + public static function addLink(array $form, FormStateInterface $form_state) { + $form['added_link'] = [ + '#title' => 'Added link', + '#type' => 'link', + '#url' => Url::fromRoute('js_webassert_test.js_webassert_test_form') + ]; + return $form; + } + /** + * Ajax callback for the "Add field" button. + */ + public static function addField(array $form, FormStateInterface $form_state) { + $form['added_field'] = [ + '#type' => 'textfield', + '#title' => 'Added textfield', + '#name' => 'added_field', + ]; + return $form; + } + + /** + * Ajax callback for the "Add ID" button. + */ + public static function addId(array $form, FormStateInterface $form_state) { + $form['added_id'] = [ + '#id' => 'js_webassert_test_field_id', + '#type' => 'submit', + '#value' => 'Added ID', + '#button_type' => 'primary', + ]; + return $form; + } + + /** + * 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'; + + $form['test_assert_wait_on_ajax_input'] = [ + '#type' => 'textfield', + '#name' => 'test_assert_wait_on_ajax_input', + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + + } + +} only in patch2: unchanged: --- a/core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceAutocompleteWidgetTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceAutocompleteWidgetTest.php @@ -42,6 +42,23 @@ protected function setUp() { } /** + * Waits for the jQuery autocomplete popup. + */ + private function waitOnAutocomplete($field_name) { + $autocomplete_field_name = "[name='". $field_name . "[0][target_id]" ."']"; + $autocomplete_field_popup = 'jQuery(jQuery("'. $autocomplete_field_name .'").data("ui-autocomplete").menu.element).is(":visible")'; + $this->assertJsCondition($autocomplete_field_popup); + } + + /** + * Assert for the jQuery autocomplete result count. + */ + private function assertResultCount($count, $field_name) { + $autocomplete_field_name = "[name='". $field_name . "[0][target_id]" ."']"; + $autocomplete_field_popup = 'jQuery(jQuery("'. $autocomplete_field_name .'").data("ui-autocomplete").menu.element).find("li").length === ' . $count; + $this->assertJsCondition($autocomplete_field_popup); + } + /** * Tests that the default autocomplete widget return the correct results. */ public function testEntityReferenceAutocompleteWidget() { @@ -66,11 +83,11 @@ public function testEntityReferenceAutocompleteWidget() { $autocomplete_field = $page->findField($field_name . '[0][target_id]'); $autocomplete_field->setValue('Test'); $this->getSession()->getDriver()->keyDown($autocomplete_field->getXpath(), ' '); - $assert_session->waitOnAutocomplete(); - $results = $page->findAll('css', '.ui-autocomplete li'); + // Assert Autocomplete Popup is shown. + $this->waitOnAutocomplete($field_name); - $this->assertCount(2, $results); + $this->assertResultCount(2, $field_name); $assert_session->pageTextContains('Test page'); $assert_session->pageTextContains('Page test'); @@ -90,11 +107,11 @@ public function testEntityReferenceAutocompleteWidget() { $autocomplete_field = $page->findField($field_name . '[0][target_id]'); $autocomplete_field->setValue('Test'); $this->getSession()->getDriver()->keyDown($autocomplete_field->getXpath(), ' '); - $assert_session->waitOnAutocomplete(); - $results = $page->findAll('css', '.ui-autocomplete li'); + // Assert Autocomplete Popup is shown. + $this->waitOnAutocomplete($field_name); - $this->assertCount(1, $results); + $this->assertResultCount(1, $field_name); $assert_session->pageTextContains('Test page'); $assert_session->pageTextNotContains('Page test'); } only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebAssertTest.php @@ -0,0 +1,74 @@ +drupalGet('js_webassert_test_form'); + + $session = $this->getSession(); + $assert_session = $this->assertSession(); + $page = $session->getPage(); + + $test_button = $page->findButton('Add button'); + $test_link = $page->findButton('Add link'); + $test_field = $page->findButton('Add field'); + $test_id = $page->findButton('Add ID'); + $test_wait_on_ajax = $page->findButton('Test assertWaitOnAjaxRequest'); + + // Test the wait...() methods by first checking the fields aren't available + // and then are available after the wait method. + $test_button->click(); + $result = $page->findButton('Added button'); + $this->assertEmpty($result); + $result = $assert_session->waitForButton('Added button'); + $this->assertNotEmpty($result); + + $test_link->click(); + $result = $page->findLink('Added link'); + $this->assertEmpty($result); + $result = $assert_session->waitForLink('Added link'); + $this->assertNotEmpty($result); + + $test_field->click(); + $result = $page->findField('added_field'); + $this->assertEmpty($result); + $result = $assert_session->waitForField('added_field'); + $this->assertNotEmpty($result); + + $test_id->click(); + $result = $page->findById('js_webassert_test_field_id'); + $this->assertEmpty($result); + $result = $assert_session->waitForId('js_webassert_test_field_id'); + $this->assertNotEmpty($result); + + // Test waitOnAjaxRequest. Verify the element is available after the wait + // and the behaviors have run on completing by checking the value. + $test_wait_on_ajax->click(); + $result = $page->findField('test_assert_wait_on_ajax_input'); + $this->assertEmpty($result); + $assert_session->assertWaitOnAjaxRequest(); + $result = $page->findField('test_assert_wait_on_ajax_input'); + $this->assertNotEmpty($result); + $this->assertEquals('js_webassert_test', $result->getValue()); + } + +}