1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.tests;
20
21 import junit.framework.AssertionFailedError;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27
28 import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
29
30 import org.junit.Test;
31
32 import static net.sourceforge.jwebunit.junit.JWebUnit.*;
33 import static org.junit.Assert.*;
34
35
36
37
38
39
40
41
42
43 public class FormSubmissionTest extends JWebUnitAPITestCase {
44
45 public void setUp() throws Exception {
46 super.setUp();
47 setBaseUrl(HOST_PATH + "/FormSubmissionTest");
48 }
49
50 @Test
51 public void testSetTextField() {
52 beginAt("/SingleNamedButtonForm.html");
53 setTextField("color", "blue");
54 submit("button");
55 assertTextPresent("Submitted parameters");
56 assertTextPresent("color=[blue]");
57 clickLink("return");
58 setTextField("color", "red");
59 submit();
60 assertTextPresent("color=[red]");
61 clickLink("return");
62 setTextField("color", "black");
63 submit("button2");
64 assertTextPresent("color=[black]");
65 }
66
67 @Test
68 public void testSetTextArea() {
69 beginAt("/TextAreaForm.html");
70 setTextField("text", "sometext");
71 submit("button");
72 assertTextPresent("Submitted parameters");
73 assertTextPresent("text=[sometext]");
74 clickLink("return");
75 setTextField("text", "anothertext");
76 submit();
77 assertTextPresent("text=[anothertext]");
78 }
79
80 @Test
81 public void testSetFileField() {
82 beginAt("/InputFileForm.html");
83 File temp = null;
84 try {
85
86 temp = File.createTempFile("data", ".txt");
87
88 temp.deleteOnExit();
89
90 BufferedWriter out = new BufferedWriter(new FileWriter(temp));
91 out.write("abcdefgh");
92 out.close();
93 } catch (IOException e) {
94 fail(e.toString());
95 }
96 String filename = temp.getAbsolutePath();
97 setTextField("file", filename);
98 submit("button");
99 assertTextPresent("Submitted parameters");
100
101 assertTextPresent("file=[" + temp.getName() + "{abcdefgh}]");
102 }
103
104 @Test
105 public void testSubmitImageInput() {
106 beginAt("/InputImageForm.html");
107 setTextField("color", "toto");
108 assertSubmitButtonPresent();
109 submit();
110 assertTextPresent("Submitted parameters");
111 assertTextPresent("color=[toto]");
112 }
113
114 @Test
115 public void testSubmitImageInputByName() {
116 beginAt("/InputImageForm.html");
117 setTextField("color", "toto");
118 assertSubmitButtonPresent("image");
119 submit("image");
120 assertTextPresent("Submitted parameters");
121 assertTextPresent("color=[toto]");
122 }
123
124 @Test
125 public void testCheckBoxSelection() {
126 beginAt("/SingleNamedButtonForm.html");
127 checkCheckbox("checkBox");
128 setTextField("color", "blue");
129 submit();
130 assertTextPresent("color=[blue]");
131
132
133 assertTextPresent("checkBox=[,on]");
134 }
135
136 @Test
137 public void testCheckBoxSelectionWithSameFieldName() {
138 beginAt("/CheckboxForm.html");
139 checkCheckbox("checkBox", "1");
140 checkCheckbox("checkBox", "3");
141 checkCheckbox("checkBox", "3");
142 submit();
143 assertTextPresent("checkBox=[1,3]");
144 }
145
146 @Test
147 public void testCheckBoxDeSelectionWithSameFieldName() {
148 beginAt("/CheckboxForm.html");
149 checkCheckbox("checkBox", "1");
150 checkCheckbox("checkBox", "3");
151 uncheckCheckbox("checkBox", "3");
152 submit();
153 assertTextPresent("checkBox=[1]");
154 }
155
156 @Test
157 public void testCheckBoxDeselection() {
158 beginAt("/SingleNamedButtonForm.html");
159 checkCheckbox("checkBox");
160
161 assertCheckboxSelected("checkBox");
162 setTextField("color", "blue");
163 uncheckCheckbox("checkBox");
164 submit();
165 assertTextPresent("color=[blue]");
166 }
167
168 @Test
169 public void testRadioSelection() {
170 beginAt("/RadioForm.html");
171 clickRadioOption("radio", "1");
172 assertRadioOptionSelected("radio", "1");
173 submit();
174 assertTextPresent("radio=[1]");
175 clickLink("return");
176 clickRadioOption("radio", "2");
177 clickRadioOption("radio", "3");
178 assertRadioOptionNotSelected("radio", "1");
179 assertRadioOptionNotSelected("radio", "2");
180 assertRadioOptionSelected("radio", "3");
181 submit();
182 assertTextPresent("radio=[3]");
183 }
184
185 @Test
186 public void testSingleFormSingleUnnamedButtonSubmission() {
187 beginAt("/SingleUnnamedButtonForm.html");
188 setTextField("color", "blue");
189 submit();
190 assertTextPresent("color=[blue]");
191 }
192
193 @Test
194 public void testSingleNamedButtonSubmission() {
195 beginAt("/SingleNamedButtonForm.html");
196 setTextField("color", "red");
197 submit();
198 assertTextPresent("color=[red]");
199 }
200
201 @Test
202 public void testSingleFormMultipleButtonSubmission() {
203 gotoMultiButtonPage();
204 submit("color");
205 assertTextPresent("color=[red]");
206 gotoMultiButtonPage();
207 submit("color", "blue");
208 assertTextPresent("color=[blue]");
209 }
210
211 @Test
212 public void testBogusParameter() {
213 gotoMultiButtonPage();
214 try {
215 setTextField("nonexistent", "anyvalue");
216 fail("Expected AssertionError");
217 } catch (AssertionError e) {
218
219 }
220 }
221
222 @Test
223 public void testParamSetOnMultiForm() {
224 beginAt("/MultiFormPage.html");
225 setTextField("param1", "anyvalue");
226 setWorkingForm("form2");
227 setTextField("param2", "anyvalue");
228 submit("button2a");
229 assertTextPresent("param2=[anyvalue]");
230 }
231
232 @Test
233 public void testTextFieldSetOnMultiFormWithSameName() {
234 beginAt("/MultiFormPage.html");
235 setWorkingForm("form2");
236 setTextField("param2", "foo");
237 setTextField("email", "anyvalue");
238 submit();
239 assertTextPresent("email=[anyvalue]");
240 assertTextPresent("param2=[foo]");
241 closeBrowser();
242 beginAt("/MultiFormPage.html");
243 setWorkingForm("form3");
244 setTextField("param3", "foo");
245 setTextField("email", "anyvalue");
246 submit();
247 assertTextPresent("param3=[foo]");
248 assertTextPresent("email=[anyvalue]");
249 }
250
251 @Test
252 public void testSetWorkingFormById() {
253 beginAt("/MultiFormPage.html");
254 setWorkingForm("form5");
255 }
256
257 @Test
258 public void testSetWorkingFormWithSameName() {
259 beginAt("/MultiFormPage.html");
260 setWorkingForm("myForm", 0);
261 assertSubmitButtonPresent("myInput1");
262 assertSubmitButtonNotPresent("myInput2");
263 setWorkingForm("myForm", 1);
264 assertSubmitButtonNotPresent("myInput1");
265 assertSubmitButtonPresent("myInput2");
266 }
267
268 @Test(expected=AssertionError.class)
269 public void testSetWorkingFormWithIndexPresent() {
270 beginAt("/MultiFormPage.html");
271 setWorkingForm("dontExists", 2);
272 }
273
274 @Test
275 public void testInvalidButton() {
276 beginAt("/InvalidActionForm.html");
277 try {
278 submit("button1");
279 fail("A TestingEngineResponseException was expected.");
280 } catch (TestingEngineResponseException e) {
281 assertEquals(404, e.getHttpStatusCode());
282 }
283 }
284
285 @Test
286 public void testUnnamedSubmitOnSpecificForm() {
287 beginAt("/MultiFormPage.html");
288 setTextField("param4", "anyvalue");
289 submit();
290 assertTextPresent("param4=[anyvalue]");
291 }
292
293 @Test
294 public void testNamedSubmitOnSpecificForm() {
295 beginAt("/MultiFormPage.html");
296 setTextField("param2", "anyvalue");
297 submit("button2b");
298 assertTextPresent("param2=[anyvalue]");
299 assertTextPresent("button2b=[b2b]");
300 }
301
302 @Test
303 public void testSubmissionReset() {
304 beginAt("/MultiFormPage.html");
305 setTextField("param2", "anyvalue");
306 reset();
307 submit("button2b");
308 assertTextNotPresent("param2=[anyvalue]");
309 assertTextPresent("button2b=[b2b]");
310 }
311
312 @Test
313 public void testSelectOption() {
314 beginAt("/MultiFormPage.html");
315 assertSelectedOptionEquals("select1", "one");
316 selectOption("select1", "two");
317 assertSelectedOptionEquals("select1", "two");
318 }
319
320 @Test
321 public void testSelectOptionInAnotherForm() {
322 beginAt("/MultiFormPage.html");
323 setWorkingForm("form6bis");
324 assertSelectedOptionEquals("select1", "four");
325 selectOption("select1", "five");
326 assertSelectedOptionEquals("select1", "five");
327 }
328
329 @Test
330 public void testSelectOptionByValue() {
331 beginAt("/MultiFormPage.html");
332 assertSelectedOptionValueEquals("select1", "1");
333 selectOptionByValue("select1", "2");
334 assertSelectedOptionValueEquals("select1", "2");
335 }
336
337 private void gotoMultiButtonPage() {
338 beginAt("/MultiNamedButtonForm.html");
339 }
340
341 @Test
342 public void testCachedForm() {
343 beginAt("/Submit1.html");
344 assertTextPresent("Page 1");
345 submit();
346 assertTextPresent("Page 2");
347 submit();
348 assertTextPresent("Page 3");
349 }
350
351
352
353
354 @Test
355 public void testClickButtonWithText1() {
356 beginAt("/SingleNamedButtonForm.html");
357 setTextField("color", "blue");
358 clickButtonWithText("click me");
359 assertTextPresent("Submitted parameters");
360 assertTextPresent("color=[blue]");
361 }
362
363 @Test
364 public void testSetHiddenField() {
365 beginAt("/SingleNamedButtonForm.html");
366 assertHiddenFieldPresent("hidden", "foo");
367 setHiddenField("hidden", "bar");
368 submit();
369 assertTextPresent("hidden=[bar]");
370 }
371
372
373 }