1 package net.sourceforge.jwebunit; 2 3 import net.sourceforge.jwebunit.JWebUnitTest; 4 5 import java.io.IOException ; 6 7 import junit.framework.AssertionFailedError; 8 import com.meterware.pseudoserver.PseudoServlet; 9 import com.meterware.pseudoserver.WebResource; 10 import com.meterware.httpunit.WebResponse; 11 12 20 public class FormSubmissionTest extends JWebUnitTest { 21 22 public void setUp() throws Exception { 23 super.setUp(); 24 addServletResource(); 25 } 26 27 public void testSetInputField() { 28 addSingleNamedButtonForm(); 29 beginAt("/QueryForm.html"); 30 setFormElement("color", "blue"); 31 submit("button"); 32 assertTextPresent("Parms are: color=blue"); 33 beginAt("/QueryForm.html"); 34 setFormElement("color", "red"); 35 submit(); 36 assertTextPresent("Parms are: color=red"); 37 } 38 39 public void testCheckBoxSelection() { 40 addSingleNamedButtonForm(); 41 beginAt("/QueryForm.html"); 42 checkCheckbox("checkBox"); 43 setFormElement("color", "blue"); 44 submit(); 45 assertTextPresent("Parms are: color=blue&checkBox=on"); 46 } 47 48 public void testCheckBoxSelectionWithSameFieldName() { 49 addCheckboxForm(); 50 beginAt("/QueryForm.html"); 51 checkCheckbox("checkBox", "1"); 52 checkCheckbox("checkBox", "3"); 53 checkCheckbox("checkBox", "3"); submit(); 55 assertTextPresent("Parms are: checkBox=1&checkBox=3"); 56 } 57 58 public void testCheckBoxDeSelectionWithSameFieldName() { 59 addCheckboxForm(); 60 beginAt("/QueryForm.html"); 61 checkCheckbox("checkBox", "1"); 62 checkCheckbox("checkBox", "3"); 63 uncheckCheckbox("checkBox", "3"); 64 submit(); 65 assertTextPresent("Parms are: checkBox=1"); 66 } 67 68 public void testCheckBoxDeselection() { 69 addSingleNamedButtonForm(); 70 beginAt("/QueryForm.html"); 71 checkCheckbox("checkBox"); 72 setFormElement("color", "blue"); 73 uncheckCheckbox("checkBox"); 74 submit(); 75 assertTextPresent("Parms are: color=blue"); 76 } 77 78 public void testSingleFormSingleUnnamedButtonSubmission() { 79 addSingleUnnamedButtonForm(); 80 beginAt("/QueryForm.html"); 81 submit(); 82 assertTextPresent("Parms are: color=blue"); 83 } 84 85 public void testSingleNamedButtonSubmission() { 86 addSingleNamedButtonForm(); 87 beginAt("/QueryForm.html"); 88 setFormElement("color", "red"); 89 submit(); 90 assertTextPresent("Parms are: color=red"); 91 } 92 93 public void testSingleFormMultipleButtonSubmission() { 94 gotoMultiButtonPage(); 95 submit("color"); 96 assertTextPresent("Parms are: color=red"); 97 } 98 99 public void testBogusParameter() { 100 gotoMultiButtonPage(); 101 try { 102 setFormElement("nonexistent", "anyvalue"); 103 } catch (AssertionFailedError e) { 104 return; 105 } 106 fail("Expected AssertionFailedError"); 107 } 108 109 public void testParamSetOnMultiForm() { 110 addMultiForm(); 111 beginAt("/QueryForm.html"); 112 setFormElement("param1", "anyvalue"); 113 setWorkingForm("form2"); 114 setFormElement("param2", "anyvalue"); 115 submit("button2a"); 116 assertTextPresent("param2=anyvalue"); 117 } 118 119 public void testSetWorkingFormById() { 120 addMultiForm(); 121 beginAt("/QueryForm.html"); 122 setWorkingForm("form5"); 123 } 124 125 public void testInvalidButton() { 126 gotoMultiButtonPage(); 127 try { 128 submit("button1"); 129 } catch (RuntimeException e) { 130 assertTrue(e.getMessage(), 131 e.getMessage().indexOf("com.meterware.httpunit.HttpNotFoundException") != -1); 132 return; 133 } 134 fail("Should have failed"); 135 } 136 137 public void testUnnamedSubmitOnSpecificForm() { 138 addMultiForm(); 139 beginAt("/QueryForm.html"); 140 setFormElement("param4", "anyvalue"); 141 submit(); 142 assertTextPresent("param4=anyvalue"); 143 } 144 145 public void testNamedSubmitOnSpecificForm() { 146 addMultiForm(); 147 beginAt("/QueryForm.html"); 148 setFormElement("param2", "anyvalue"); 149 submit("button2b"); 150 assertTextPresent("param2=anyvalue&button2b=b2b"); 151 } 152 153 public void testSubmissionReset() { 154 addMultiForm(); 155 beginAt("/QueryForm.html"); 156 setFormElement("param2", "anyvalue"); 157 WebResponse oldResp = getDialog().getResponse(); 158 submit("button2b"); 159 assertNull(getDialog().getForm()); 160 assertTrue(getDialog().getResponse() != oldResp); 161 } 162 163 public void testSelectOption() { 164 addMultiForm(); 165 beginAt("/QueryForm.html"); 166 assertOptionEquals("select1", "one"); 167 selectOption("select1", "two"); 168 assertOptionEquals("select1", "two"); 169 assertFormElementEquals("select1", "2"); 170 } 171 172 public void testSimpleLabeledForm() { 173 addSimpleLabeledForm(); 174 beginAt("/QueryForm.html"); 175 setFormElementWithLabel("First", "oneValue"); 176 setFormElementWithLabel("Second", "anotherValue"); 177 submit(); 178 assertTextPresent("param1=oneValue¶m2=anotherValue"); 179 } 180 181 public void testTrickyLabeledForm() { 182 addTrickyLabeledForm(); 183 beginAt("/QueryForm.html"); 184 setFormElementWithLabel("Trick", "oneValue"); 185 setFormElementWithLabel("Treat", "anotherValue"); 186 submit(); 187 assertTextPresent("param3=oneValue¶m4=anotherValue"); 188 } 189 190 private void gotoMultiButtonPage() { 191 addMultiNamedButtonForm(); 192 beginAt("/QueryForm.html"); 193 } 194 195 private void addServletResource() { 196 addTargetResource("TargetPage", "color=blue"); 197 addTargetResource("TargetPage", "color=red"); 198 addTargetResource("TargetPage", "color=blue&checkBox=on"); 199 addTargetResource("TargetPage", "color=blue&size=big"); 200 addTargetResource("TargetPage", "color=blue&size=small"); 201 addTargetResource("TargetPage", "param2=anyvalue"); 202 addTargetResource("TargetPage", "param4=anyvalue"); 203 addTargetResource("TargetPage", "param2=anyvalue&button2b=b2b"); 204 addTargetResource("TargetPage", "param2=anyvalue&button2a=b2a"); 205 addTargetResource("TargetPage", "param1=oneValue¶m2=anotherValue"); 206 addTargetResource("TargetPage", "param3=oneValue¶m4=anotherValue"); 207 addTargetResource("TargetPage", "checkBox=1&checkBox=3"); 208 addTargetResource("TargetPage", "checkBox=1"); 209 } 210 211 private void addTargetResource(String name, final String parms) { 212 String resourceName = name; 213 if (parms != null && !parms.equals("")) { 214 resourceName += "?" + parms; 215 } 216 defineResource(resourceName, new PseudoServlet() { 218 public WebResource getPostResponse() { 219 WebResource result = new WebResource("<html><body><table><tr><td>Parms are: " + parms + 220 "</td></tr></table></body></html>"); 221 return result; 222 } 223 224 public WebResource getGetResponse() throws IOException { 225 return getPostResponse(); 226 } 227 }); 228 } 229 230 private void addSingleUnnamedButtonForm() { 231 defineResource("QueryForm.html", 232 "<html><head></head>" + 233 "<form method=GET action=\"TargetPage?color=blue\">" + 234 "<input type=submit></form></body></html>"); 235 } 236 237 private void addSingleNamedButtonForm() { 238 defineResource("QueryForm.html", 239 "<html><head></head>" + 240 "<form method=GET action=\"TargetPage\">" + 241 "<input type=\"text\" name=\"color\"><input type=\"submit\" name=\"button\">" + 242 "<input type=\"checkbox\" name=\"checkBox\" value=\"on\">" + 243 "</form></body></html>"); 244 } 245 246 247 private void addCheckboxForm() { 248 defineResource("QueryForm.html", 249 "<html><head></head>" + 250 "<form method=GET action=\"TargetPage\">" + 251 "<input type=\"checkbox\" name=\"checkBox\" value=\"1\">" + 252 "<input type=\"checkbox\" name=\"checkBox\" value=\"2\">" + 253 "<input type=\"checkbox\" name=\"checkBox\" value=\"3\">" + 254 "<input type=\"submit\" name=\"button\">" + 255 "</form></body></html>"); 256 } 257 258 private void addMultiNamedButtonForm() { 259 defineResource("QueryForm.html", 260 "<html><head></head>" + 261 "<form method=GET action=\"TargetPage\">" + 262 "<input name=\"button1\" value=\"b1\" type=submit>" + 263 "<input name=\"color\" value=\"red\" type=submit>" + 264 "</form></body></html>"); 265 } 266 267 private void addMultiForm() { 268 defineResource("QueryForm.html", 269 "<html><head></head>" + 270 "<form method=GET action=\"TargetPage\">" + 271 "<input type=\"text\" name=\"param1\">" + 272 "<input type=\"submit\" name=\"button1\"></form>" + 273 274 "<form name=\"form2\" method=GET action=\"TargetPage\">" + 275 "<input type=\"text\" name=\"param2\">" + 276 "<input type=\"submit\" name=\"button2a\" value=\"b2a\">" + 277 "<input type=\"submit\" name=\"button2b\" value=\"b2b\"></form>" + 278 279 "<form name=\"form3\" method=GET action=\"TargetPage\">" + 280 "<input type=\"text\" name=\"param3\"><input type=\"submit\"></form>" + 281 282 "<form name=\"form4\" method=GET action=\"TargetPage\">" + 283 "<input type=\"text\" name=\"param4\"><input type=\"submit\"></form>" + 284 "<form id=\"form5\"/>" + 285 "<form id=\"form6\">" + 286 "<select name=\"select1\">" + 287 "<option value=\"1\">one</option>" + 288 "<option value=\"2\">two</option>" + 289 "<option value=\"3\">three</option>" + 290 "</select></form>" + 291 "</body></html>"); 292 293 } 294 295 private void addSimpleLabeledForm() { 296 defineResource("QueryForm.html", 297 "<html><head></head>" + 298 "<body>" + 299 "<form method=GET action=\"TargetPage\">" + 300 "First : <input type=\"text\" name=\"param1\">" + 301 "Second : <textarea name=\"param2\"></form>" + 302 "</body></html>"); 303 } 304 305 private void addTrickyLabeledForm() { 306 defineResource("QueryForm.html", 307 "<html><head></head>" + 308 "Trick!" + 309 "<form method=GET action=\"TargetPage\">" + 310 "First : <input type=\"text\" name=\"param1\">" + 311 "Second : <input type=\"text\" name=\"param2\">" + 312 "Trick!</form>" + 313 "<form name=\"form2\" method=GET action=\"TargetPage\">" + 314 "Trick! <input type=\"text\" name=\"param3\">" + 315 "Treat! <input type=\"text\" name=\"param4\">" + 316 "</form></html>"); 317 } 318 319 } | Popular Tags |