CommandsTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. /**
  5. * Performs tests on AJAX framework commands.
  6. *
  7. * @group Ajax
  8. */
  9. class CommandsTest extends WebDriverTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $defaultTheme = 'stark';
  18. /**
  19. * Tests the various Ajax Commands.
  20. */
  21. public function testAjaxCommands() {
  22. $session = $this->getSession();
  23. $page = $this->getSession()->getPage();
  24. $form_path = 'ajax_forms_test_ajax_commands_form';
  25. $web_user = $this->drupalCreateUser(['access content']);
  26. $this->drupalLogin($web_user);
  27. $this->drupalGet($form_path);
  28. // Tests the 'add_css' command.
  29. $page->pressButton("AJAX 'add_css' command");
  30. $this->assertWaitPageContains('my/file.css');
  31. // Tests the 'after' command.
  32. $page->pressButton("AJAX 'After': Click to put something after the div");
  33. $this->assertWaitPageContains('<div id="after_div">Something can be inserted after this</div>This will be placed after');
  34. // Tests the 'alert' command.
  35. $page->pressButton("AJAX 'Alert': Click to alert");
  36. // Wait for the alert to appear.
  37. $page->waitFor(10, function () use ($session) {
  38. try {
  39. $session->getDriver()->getWebDriverSession()->getAlert_text();
  40. return TRUE;
  41. }
  42. catch (\Exception $e) {
  43. return FALSE;
  44. }
  45. });
  46. $alert_text = $this->getSession()->getDriver()->getWebDriverSession()->getAlert_text();
  47. $this->assertEquals('Alert', $alert_text);
  48. $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
  49. $this->drupalGet($form_path);
  50. $page->pressButton("AJAX 'Announce': Click to announce");
  51. $this->assertWaitPageContains('<div id="drupal-live-announce" class="visually-hidden" aria-live="polite" aria-busy="false">Default announcement.</div>');
  52. $this->drupalGet($form_path);
  53. $page->pressButton("AJAX 'Announce': Click to announce with 'polite' priority");
  54. $this->assertWaitPageContains('<div id="drupal-live-announce" class="visually-hidden" aria-live="polite" aria-busy="false">Polite announcement.</div>');
  55. $this->drupalGet($form_path);
  56. $page->pressButton("AJAX 'Announce': Click to announce with 'assertive' priority");
  57. $this->assertWaitPageContains('<div id="drupal-live-announce" class="visually-hidden" aria-live="assertive" aria-busy="false">Assertive announcement.</div>');
  58. $this->drupalGet($form_path);
  59. $page->pressButton("AJAX 'Announce': Click to announce twice");
  60. $this->assertWaitPageContains('<div id="drupal-live-announce" class="visually-hidden" aria-live="assertive" aria-busy="false">Assertive announcement.' . "\nAnother announcement.</div>");
  61. // Tests the 'append' command.
  62. $page->pressButton("AJAX 'Append': Click to append something");
  63. $this->assertWaitPageContains('<div id="append_div">Append inside this divAppended text</div>');
  64. // Tests the 'before' command.
  65. $page->pressButton("AJAX 'before': Click to put something before the div");
  66. $this->assertWaitPageContains('Before text<div id="before_div">Insert something before this.</div>');
  67. // Tests the 'changed' command.
  68. $page->pressButton("AJAX changed: Click to mark div changed.");
  69. $this->assertWaitPageContains('<div id="changed_div" class="ajax-changed">');
  70. // Tests the 'changed' command using the second argument.
  71. // Refresh page for testing 'changed' command to same element again.
  72. $this->drupalGet($form_path);
  73. $page->pressButton("AJAX changed: Click to mark div changed with asterisk.");
  74. $this->assertWaitPageContains('<div id="changed_div" class="ajax-changed"> <div id="changed_div_mark_this">This div can be marked as changed or not. <abbr class="ajax-changed" title="Changed">*</abbr> </div></div>');
  75. // Tests the 'css' command.
  76. $page->pressButton("Set the '#box' div to be blue.");
  77. $this->assertWaitPageContains('<div id="css_div" style="background-color: blue;">');
  78. // Tests the 'data' command.
  79. $page->pressButton("AJAX data command: Issue command.");
  80. $this->assertTrue($page->waitFor(10, function () use ($session) {
  81. return 'testvalue' === $session->evaluateScript('window.jQuery("#data_div").data("testkey")');
  82. }));
  83. // Tests the 'html' command.
  84. $page->pressButton("AJAX html: Replace the HTML in a selector.");
  85. $this->assertWaitPageContains('<div id="html_div">replacement text</div>');
  86. // Tests the 'insert' command.
  87. $page->pressButton("AJAX insert: Let client insert based on #ajax['method'].");
  88. $this->assertWaitPageContains('<div id="insert_div">insert replacement textOriginal contents</div>');
  89. // Tests the 'invoke' command.
  90. $page->pressButton("AJAX invoke command: Invoke addClass() method.");
  91. $this->assertWaitPageContains('<div id="invoke_div" class="error">Original contents</div>');
  92. // Tests the 'prepend' command.
  93. $page->pressButton("AJAX 'prepend': Click to prepend something");
  94. $this->assertWaitPageContains('<div id="prepend_div">prepended textSomething will be prepended to this div. </div>');
  95. // Tests the 'remove' command.
  96. $page->pressButton("AJAX 'remove': Click to remove text");
  97. $this->assertWaitPageContains('<div id="remove_div"></div>');
  98. // Tests the 'restripe' command.
  99. $page->pressButton("AJAX 'restripe' command");
  100. $this->assertWaitPageContains('<tr id="table-first" class="odd"><td>first row</td></tr>');
  101. $this->assertWaitPageContains('<tr class="even"><td>second row</td></tr>');
  102. // Tests the 'settings' command.
  103. $test_settings_command = <<<JS
  104. Drupal.behaviors.testSettingsCommand = {
  105. attach: function (context, settings) {
  106. window.jQuery('body').append('<div class="test-settings-command">' + settings.ajax_forms_test.foo + '</div>');
  107. }
  108. };
  109. JS;
  110. $session->executeScript($test_settings_command);
  111. // @todo: Replace after https://www.drupal.org/project/drupal/issues/2616184
  112. $session->executeScript('window.jQuery("#edit-settings-command-example").mousedown();');
  113. $this->assertWaitPageContains('<div class="test-settings-command">42</div>');
  114. }
  115. /**
  116. * Asserts that page contains a text after waiting.
  117. *
  118. * @param string $text
  119. * A needle text.
  120. */
  121. protected function assertWaitPageContains($text) {
  122. $page = $this->getSession()->getPage();
  123. $page->waitFor(10, function () use ($page, $text) {
  124. return stripos($page->getContent(), $text) !== FALSE;
  125. });
  126. $this->assertStringContainsString($text, $page->getContent());
  127. }
  128. }