ElementValidationTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. /**
  5. * Various tests of AJAX behavior.
  6. *
  7. * @group Ajax
  8. */
  9. class ElementValidationTest extends WebDriverTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static $modules = ['ajax_test', 'ajax_forms_test'];
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $defaultTheme = 'classy';
  18. /**
  19. * Tries to post an Ajax change to a form that has a validated element.
  20. *
  21. * Drupal AJAX commands update the DOM echoing back the validated values in
  22. * the form of messages that appear on the page.
  23. */
  24. public function testAjaxElementValidation() {
  25. $this->drupalGet('ajax_validation_test');
  26. $page = $this->getSession()->getPage();
  27. $assert = $this->assertSession();
  28. // Partially complete the form with a string.
  29. $page->fillField('drivertext', 'some dumb text');
  30. // Move focus away from this field to trigger AJAX.
  31. $page->findField('spare_required_field')->focus();
  32. // When the AJAX command updates the DOM a <ul> unsorted list
  33. // "message__list" structure will appear on the page echoing back the
  34. // "some dumb text" message.
  35. $placeholder_text = $assert->waitForElement('css', "ul.messages__list li.messages__item em:contains('some dumb text')");
  36. $this->assertNotNull($placeholder_text, 'A callback successfully echoed back a string.');
  37. $this->drupalGet('ajax_validation_test');
  38. // Partially complete the form with a number.
  39. $page->fillField('drivernumber', '12345');
  40. $page->findField('spare_required_field')->focus();
  41. // The AJAX request/resonse will complete successfully when a InsertCommand
  42. // injects a message with a placeholder element into the DOM with the
  43. // submitted number.
  44. $placeholder_number = $assert->waitForElement('css', "ul.messages__list li.messages__item em:contains('12345')");
  45. $this->assertNotNull($placeholder_number, 'A callback successfully echoed back a number.');
  46. }
  47. }