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