1<?php
2
3namespace Drupal\js_webassert_test\Form;
4
5use Drupal\Core\Form\FormBase;
6use Drupal\Core\Form\FormStateInterface;
7use Drupal\Core\Url;
8
9/**
10 * Test form for JSWebAssert WebDriverTestBase.
11 *
12 * @internal
13 */
14class JsWebAssertTestForm extends FormBase {
15
16  /**
17   * {@inheritdoc}
18   */
19  public function getFormId() {
20    return 'js_webassert_test_form';
21  }
22
23  /**
24   * Form for testing the addition of various types of elements via AJAX.
25   */
26  public function buildForm(array $form, FormStateInterface $form_state) {
27    $form['#prefix'] = '<div id="js_webassert_test_form_wrapper">';
28    $form['#suffix'] = '</div>';
29
30    // Button to test for the waitForButton() assertion.
31    $form['test_button'] = [
32      '#type' => 'submit',
33      '#value' => $this->t('Add button'),
34      '#button_type' => 'primary',
35      '#ajax' => [
36        'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addButton',
37        'progress' => [
38          'type' => 'throbber',
39          'message' => NULL,
40        ],
41        'wrapper' => 'js_webassert_test_form_wrapper',
42      ],
43    ];
44    // Button to test for the waitForLink() assertion.
45    $form['test_link'] = [
46      '#type' => 'submit',
47      '#value' => $this->t('Add link'),
48      '#button_type' => 'primary',
49      '#ajax' => [
50        'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addLink',
51        'progress' => [
52          'type' => 'throbber',
53          'message' => NULL,
54        ],
55        'wrapper' => 'js_webassert_test_form_wrapper',
56      ],
57    ];
58    // Button to test for the waitForField() assertion.
59    $form['test_field'] = [
60      '#type' => 'submit',
61      '#value' => $this->t('Add field'),
62      '#button_type' => 'primary',
63      '#ajax' => [
64        'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addField',
65        'progress' => [
66          'type' => 'throbber',
67          'message' => NULL,
68        ],
69        'wrapper' => 'js_webassert_test_form_wrapper',
70      ],
71    ];
72    // Button to test for the waitForId() assertion.
73    $form['test_id'] = [
74      '#type' => 'submit',
75      '#value' => $this->t('Add ID'),
76      '#button_type' => 'primary',
77      '#ajax' => [
78        'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addId',
79        'progress' => [
80          'type' => 'throbber',
81          'message' => NULL,
82        ],
83        'wrapper' => 'js_webassert_test_form_wrapper',
84      ],
85    ];
86
87    // Button to test the assertWaitOnAjaxRequest() assertion.
88    $form['test_wait_for_element_visible'] = [
89      '#type' => 'submit',
90      '#value' => $this->t('Test waitForElementVisible'),
91      '#button_type' => 'primary',
92      '#ajax' => [
93        'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addWaitForElementVisible',
94        'progress' => [
95          'type' => 'throbber',
96          'message' => NULL,
97        ],
98        'wrapper' => 'js_webassert_test_form_wrapper',
99      ],
100    ];
101
102    // Button to test the assertWaitOnAjaxRequest() assertion.
103    $form['test_assert_wait_on_ajax_request'] = [
104      '#type' => 'submit',
105      '#value' => $this->t('Test assertWaitOnAjaxRequest'),
106      '#button_type' => 'primary',
107      '#ajax' => [
108        'callback' => 'Drupal\js_webassert_test\Form\JsWebAssertTestForm::addAssertWaitOnAjaxRequest',
109        'progress' => [
110          'type' => 'throbber',
111          'message' => NULL,
112        ],
113        'wrapper' => 'js_webassert_test_form_wrapper',
114      ],
115    ];
116
117    // Button to test the assertNoElementAfterWait() assertion, will pass.
118    $form['test_assert_no_element_after_wait_pass'] = [
119      '#type' => 'submit',
120      '#value' => $this->t('Test assertNoElementAfterWait: pass'),
121      '#button_type' => 'primary',
122      '#attached' => ['library' => 'js_webassert_test/no_element_after_wait'],
123    ];
124
125    // Button to test the assertNoElementAfterWait() assertion, will fail.
126    $form['test_assert_no_element_after_wait_fail'] = [
127      '#type' => 'submit',
128      '#value' => $this->t('Test assertNoElementAfterWait: fail'),
129      '#button_type' => 'primary',
130    ];
131
132    return $form;
133  }
134
135  /**
136   * Ajax callback for the "Add button" button.
137   */
138  public static function addButton(array $form, FormStateInterface $form_state) {
139    $form['added_button'] = [
140      '#type' => 'submit',
141      '#value' => 'Added button',
142      '#button_type' => 'primary',
143    ];
144    return $form;
145  }
146
147  /**
148   * Ajax callback for the "Add link" button.
149   */
150  public static function addLink(array $form, FormStateInterface $form_state) {
151    $form['added_link'] = [
152      '#title' => 'Added link',
153      '#type' => 'link',
154      '#url' => Url::fromRoute('js_webassert_test.js_webassert_test_form'),
155    ];
156    return $form;
157  }
158
159  /**
160   * Ajax callback for the "Add field" button.
161   */
162  public static function addField(array $form, FormStateInterface $form_state) {
163    $form['added_field'] = [
164      '#type' => 'textfield',
165      '#title' => 'Added textfield',
166      '#name' => 'added_field',
167    ];
168    return $form;
169  }
170
171  /**
172   * Ajax callback for the "Add ID" button.
173   */
174  public static function addId(array $form, FormStateInterface $form_state) {
175    $form['added_id'] = [
176      '#id' => 'js_webassert_test_field_id',
177      '#type' => 'submit',
178      '#value' => 'Added ID',
179      '#button_type' => 'primary',
180    ];
181    return $form;
182  }
183
184  /**
185   * Ajax callback for the "Test waitForAjax" button.
186   */
187  public static function addAssertWaitOnAjaxRequest(array $form, FormStateInterface $form_state) {
188    // Attach the library necessary for this test.
189    $form['#attached']['library'][] = 'js_webassert_test/wait_for_ajax_request';
190
191    $form['test_assert_wait_on_ajax_input'] = [
192      '#type' => 'textfield',
193      '#name' => 'test_assert_wait_on_ajax_input',
194    ];
195
196    return $form;
197  }
198
199  /**
200   * Ajax callback for the "Test waitForElementVisible" button.
201   */
202  public static function addWaitForElementVisible(array $form, FormStateInterface $form_state) {
203    // Attach the library necessary for this test.
204    $form['#attached']['library'][] = 'js_webassert_test/wait_for_element';
205
206    $form['element_invisible'] = [
207      '#id' => 'js_webassert_test_element_invisible',
208      '#type' => 'submit',
209      '#value' => 'Added WaitForElementVisible',
210      '#button_type' => 'primary',
211      '#attributes' => [
212        'style' => ['display: none;'],
213      ],
214    ];
215    return $form;
216  }
217
218  /**
219   * {@inheritdoc}
220   */
221  public function submitForm(array &$form, FormStateInterface $form_state) {
222
223  }
224
225}
226