KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > WebTester


1 /********************************************************************************
2  * Copyright (c) 2001, ThoughtWorks, Inc.
3  * Distributed open-source, see full license under licenses/jwebunit_license.txt
4  **********************************/

5 package net.sourceforge.jwebunit;
6
7 import java.io.PrintStream JavaDoc;
8 import java.util.Locale JavaDoc;
9 import java.util.ResourceBundle JavaDoc;
10
11 import junit.framework.Assert;
12 import junit.framework.AssertionFailedError;
13 import net.sourceforge.jwebunit.util.ExceptionUtility;
14
15 import org.w3c.dom.Element JavaDoc;
16
17 /**
18  * Provides a high-level API for basic web application navigation and validation
19  * by wrapping HttpUnit and providing Junit assertions. It supports use of a property file for web
20  * resources (a la Struts), though a resource file for the app is not required.
21  *
22  * @author Jim Weaver
23  * @author Wilkes Joiner
24  */

25 public class WebTester {
26     private HttpUnitDialog dialog;
27     private TestContext context = new TestContext();
28
29     /**
30      * Provides access to the httpunit wrapper for subclasses - in case functionality not
31      * yet wrappered required by test.
32      *
33      * @return HttpUnitDialog instance used to wrapper httpunit conversation.
34      */

35     public HttpUnitDialog getDialog() {
36         return dialog;
37     }
38
39     /**
40      * Provide access to test context.
41      *
42      * @return TestContext
43      */

44     public TestContext getTestContext() {
45         return context;
46     }
47
48     /**
49      * Begin conversation at a url relative to the application root.
50      *
51      * @param relativeURL
52      */

53     public void beginAt(String JavaDoc relativeURL) {
54         String JavaDoc url = createUrl(relativeURL);
55         dialog = new HttpUnitDialog(url, context);
56     }
57
58     private String JavaDoc createUrl(String JavaDoc suffix) {
59         suffix = suffix.startsWith("/") ? suffix.substring(1) : suffix;
60         return getTestContext().getBaseUrl() + suffix;
61     }
62
63     /**
64      * Return the value of a web resource based on its key. This translates to a
65      * property file lookup with the locale based on the current TestContext.
66      *
67      * @param key name of the web resource.
68      * @return value of the web resource, encoded according to TestContext.
69      */

70     public String JavaDoc getMessage(String JavaDoc key) {
71         String JavaDoc message = "";
72         Locale JavaDoc locale = context.getLocale();
73         try {
74             message = ResourceBundle.getBundle(getTestContext().getResourceBundleName(), locale).getString(key);
75         } catch (Exception JavaDoc e) {
76             e.printStackTrace();
77             throw new RuntimeException JavaDoc("No message found for key [" + key + "]." +
78                     "\nError: " + ExceptionUtility.stackTraceToString(e));
79         }
80         return context.toEncodedString(message);
81     }
82
83     //Assertions
84

85     /**
86      * Assert title of current html page in conversation matches an expected value.
87      *
88      * @param title expected title value
89      */

90     public void assertTitleEquals(String JavaDoc title) {
91         Assert.assertEquals(title, dialog.getResponsePageTitle());
92     }
93
94     /**
95      * Assert title of current html page matches the value of a specified web resource.
96      *
97      * @param titleKey web resource key for title
98      */

99     public void assertTitleEqualsKey(String JavaDoc titleKey) {
100         Assert.assertEquals(getMessage(titleKey), dialog.getResponsePageTitle());
101     }
102
103     /**
104      * Assert that a web resource's value is present.
105      *
106      * @param key web resource name
107      */

108     public void assertKeyPresent(String JavaDoc key) {
109         assertTextPresent(getMessage(key));
110     }
111
112     /**
113      * Assert that supplied text is present.
114      *
115      * @param text
116      */

117     public void assertTextPresent(String JavaDoc text) {
118         if (!dialog.isTextInResponse(text))
119             Assert.fail("Expected text not found in response: [" + text + "]");
120     }
121
122     /**
123      * Assert that a web resource's value is not present.
124      *
125      * @param key web resource name
126      */

127     public void assertKeyNotPresent(String JavaDoc key) {
128         assertTextNotPresent(getMessage(key));
129     }
130
131     /**
132      * Assert that supplied text is not present.
133      *
134      * @param text
135      */

136     public void assertTextNotPresent(String JavaDoc text) {
137         if (dialog.isTextInResponse(text))
138             Assert.fail("Text found in response when not expected: [" + text + "]");
139     }
140
141     /**
142      * Assert that a table with a given summary or id value is present.
143      *
144      * @param tableSummaryOrId summary or id attribute value of table
145      */

146     public void assertTablePresent(String JavaDoc tableSummaryOrId) {
147         if (dialog.getWebTableBySummaryOrId(tableSummaryOrId) == null)
148             Assert.fail("Unable to locate table \"" + tableSummaryOrId + "\"");
149     }
150
151     /**
152      * Assert that a table with a given summary or id value is not present.
153      *
154      * @param tableSummaryOrId summary or id attribute value of table
155      */

156     public void assertTableNotPresent(String JavaDoc tableSummaryOrId) {
157         if (dialog.getWebTableBySummaryOrId(tableSummaryOrId) != null)
158             Assert.fail("Located table \"" + tableSummaryOrId + "\"");
159     }
160
161     /**
162      * Assert that the value of a given web resource is present in a specific table.
163      *
164      * @param tableSummaryOrId summary or id attribute value of table
165      * @param key web resource name
166      */

167     public void assertKeyInTable(String JavaDoc tableSummaryOrId, String JavaDoc key) {
168         assertTextInTable(tableSummaryOrId, getMessage(key));
169     }
170
171     /**
172      * Assert that supplied text is present in a specific table.
173      *
174      * @param tableSummaryOrId summary or id attribute value of table
175      * @param text
176      */

177     public void assertTextInTable(String JavaDoc tableSummaryOrId, String JavaDoc text) {
178         assertTablePresent(tableSummaryOrId);
179         Assert.assertTrue("Could not find: [" + text + "]" +
180                 "in table [" + tableSummaryOrId + "]",
181                 dialog.isTextInTable(tableSummaryOrId, text));
182     }
183
184     /**
185      * Assert that the values of a set of web resources are all present in a specific table.
186      *
187      * @param tableSummaryOrId summary or id attribute value of table
188      * @param keys Array of web resource names.
189      */

190     public void assertKeysInTable(String JavaDoc tableSummaryOrId, String JavaDoc[] keys) {
191         for (int i = 0; i < keys.length; i++) {
192             assertKeyInTable(tableSummaryOrId, keys[i]);
193         }
194     }
195
196     /**
197      * Assert that a set of text values are all present in a specific table.
198      *
199      * @param tableSummaryOrId summary or id attribute value of table
200      * @param text Array of expected text values.
201      */

202     public void assertTextInTable(String JavaDoc tableSummaryOrId, String JavaDoc[] text) {
203         for (int i = 0; i < text.length; i++) {
204             assertTextInTable(tableSummaryOrId, text[i]);
205         }
206     }
207
208     /**
209      * Assert that the value of a given web resource is not present in a specific table.
210      *
211      * @param tableSummaryOrId summary or id attribute value of table
212      * @param key web resource name
213      */

214     public void assertKeyNotInTable(String JavaDoc tableSummaryOrId, String JavaDoc key) {
215         assertTextNotInTable(tableSummaryOrId, getMessage(key));
216     }
217
218     /**
219      * Assert that supplied text is not present in a specific table.
220      *
221      * @param tableSummaryOrId summary or id attribute value of table
222      * @param text
223      */

224     public void assertTextNotInTable(String JavaDoc tableSummaryOrId, String JavaDoc text) {
225         assertTablePresent(tableSummaryOrId);
226         Assert.assertTrue("Found text: [" + text + "] in table [" +
227                 tableSummaryOrId + "]",
228                 !dialog.isTextInTable(tableSummaryOrId, text));
229     }
230
231     /**
232      * Assert that none of a set of text values are present in a specific table.
233      *
234      * @param tableSummaryOrId summary or id attribute value of table
235      * @param text Array of text values
236      */

237     public void assertTextNotInTable(String JavaDoc tableSummaryOrId, String JavaDoc[] text) {
238         for (int i = 0; i < text.length; i++) {
239             assertTextNotInTable(tableSummaryOrId, text[i]);
240         }
241     }
242
243     /**
244      * Assert that a specific table matches an ExpectedTable.
245      *
246      * @param tableSummaryOrId summary or id attribute value of table
247      * @param expectedTable represents expected values (colspan supported).
248      */

249     public void assertTableEquals(String JavaDoc tableSummaryOrId, ExpectedTable expectedTable) {
250         assertTableEquals(tableSummaryOrId, expectedTable.getExpectedStrings());
251     }
252
253     /**
254      * Assert that a specific table matches a matrix of supplied text values.
255      *
256      * @param tableSummaryOrId summary or id attribute value of table
257      * @param expectedCellValues double dimensional array of expected values
258      */

259     public void assertTableEquals(String JavaDoc tableSummaryOrId, String JavaDoc[][] expectedCellValues) {
260         assertTableRowsEqual(tableSummaryOrId, 0, expectedCellValues);
261     }
262
263     /**
264      * Assert that a range of rows for a specific table matches a matrix of supplied text values.
265      *
266      * @param tableSummaryOrId summary or id attribute value of table
267      * @param startRow index of start row for comparison
268      * @param expectedTable represents expected values (colspan supported).
269      */

270     public void assertTableRowsEqual(String JavaDoc tableSummaryOrId, int startRow, ExpectedTable expectedTable) {
271         assertTableRowsEqual(tableSummaryOrId, startRow, expectedTable.getExpectedStrings());
272     }
273
274     /**
275      * Assert that a range of rows for a specific table matches a matrix of supplied text values.
276      *
277      * @param tableSummaryOrId summary or id attribute value of table
278      * @param startRow index of start row for comparison
279      * @param expectedCellValues double dimensional array of expected values
280      */

281     public void assertTableRowsEqual(String JavaDoc tableSummaryOrId, int startRow, String JavaDoc[][] expectedCellValues) {
282         assertTablePresent(tableSummaryOrId);
283         String JavaDoc[][] sparseTableCellValues = dialog.getSparseTableBySummaryOrId(tableSummaryOrId);
284         if (expectedCellValues.length > (sparseTableCellValues.length - startRow))
285             Assert.fail("Expected rows [" + expectedCellValues.length + "] larger than actual rows in range being compared" +
286                     " [" + (sparseTableCellValues.length - startRow) + "].");
287         for (int i = 0; i < expectedCellValues.length; i++) {
288             String JavaDoc[] row = expectedCellValues[i];
289             for (int j = 0; j < row.length; j++) {
290                 if (row.length != sparseTableCellValues[i].length)
291                     Assert.fail("Unequal number of columns for row " + i + " of table " + tableSummaryOrId +
292                             ". Expected [" + row.length + "] found [" + sparseTableCellValues[i].length + "].");
293                 String JavaDoc expectedString = row[j];
294                 Assert.assertEquals("Expected " + tableSummaryOrId + " value at [" + i + "," + j + "] not found.",
295                         expectedString, context.toEncodedString(sparseTableCellValues[i + startRow][j].trim()));
296             }
297         }
298     }
299
300     /**
301      * Assert that a form input element with a given name is present.
302      *
303      * @param formElementName
304      */

305     public void assertFormElementPresent(String JavaDoc formElementName) {
306         assertFormPresent();
307         Assert.assertTrue("Did not find form element with name [" + formElementName + "].",
308                 dialog.hasFormParameterNamed(formElementName));
309     }
310
311     /**
312      * Assert that a form input element with a given name is not present.
313      *
314      * @param formElementName
315      */

316     public void assertFormElementNotPresent(String JavaDoc formElementName) {
317         assertFormPresent();
318         try {
319             Assert.assertTrue("Found form element with name [" + formElementName + "] when not expected.",
320                     !dialog.hasFormParameterNamed(formElementName));
321         } catch (UnableToSetFormException e) {
322             // assertFormControlNotPresent
323
}
324     }
325
326     /**
327      * Assert that a form input element with a given label is present.
328      *
329      * @param formElementLabel label preceding form element.
330      * @see #setFormElementWithLabel(String,String)
331      */

332     public void assertFormElementPresentWithLabel(String JavaDoc formElementLabel) {
333         Assert.assertTrue("Did not find form element with label [" + formElementLabel + "].",
334                 dialog.hasFormParameterLabeled(formElementLabel));
335     }
336
337     /**
338      * Assert that a form input element with a given label is not present.
339      *
340      * @param formElementLabel label preceding form element.
341      * @see #setFormElementWithLabel(String,String)
342      */

343     public void assertFormElementNotPresentWithLabel(String JavaDoc formElementLabel) {
344         Assert.assertFalse("Found form element with label [" + formElementLabel + "].",
345                 dialog.hasFormParameterLabeled(formElementLabel));
346     }
347
348     /**
349      * Assert that there is a form present.
350      *
351      */

352     public void assertFormPresent() {
353         Assert.assertTrue("No form present", dialog.hasForm());
354     }
355
356     /**
357      * Assert that there is a form with the specified name or id present.
358      * @param nameOrID
359      */

360     public void assertFormPresent(String JavaDoc nameOrID) {
361         Assert.assertTrue("No form present with name or id [" + nameOrID + "]", dialog.hasForm(nameOrID));
362     }
363
364     /**
365      * Assert that there is not a form present.
366      *
367      */

368     public void assertFormNotPresent() {
369         Assert.assertFalse("A form is present", dialog.hasForm());
370     }
371
372     /**
373      * Assert that there is not a form with the specified name or id present.
374      * @param nameOrID
375      */

376     public void assertFormNotPresent(String JavaDoc nameOrID) {
377         Assert.assertFalse("Form present with name or id [" + nameOrID + "]", dialog.hasForm(nameOrID));
378     }
379     
380     /**
381      * Assert that a specific form element has an expected value.
382      *
383      * @param formElementName
384      * @param expectedValue
385      */

386     public void assertFormElementEquals(String JavaDoc formElementName, String JavaDoc expectedValue) {
387         assertFormElementPresent(formElementName);
388         Assert.assertEquals(expectedValue, dialog.getFormParameterValue(formElementName));
389     }
390
391     /**
392      * Assert that a form element had no value / is empty.
393      *
394      * @param formElementName
395      */

396     public void assertFormElementEmpty(String JavaDoc formElementName) {
397         assertFormElementPresent(formElementName);
398         Assert.assertEquals("", dialog.getFormParameterValue(formElementName));
399     }
400
401     /**
402      * Assert that a specific checkbox is selected.
403      *
404      * @param checkBoxName
405      */

406     public void assertCheckboxSelected(String JavaDoc checkBoxName) {
407         assertFormElementPresent(checkBoxName);
408         Assert.assertEquals("on", dialog.getFormParameterValue(checkBoxName));
409     }
410
411     /**
412      * Assert that a specific checkbox is not selected.
413      *
414      * @param checkBoxName
415      */

416     public void assertCheckboxNotSelected(String JavaDoc checkBoxName) {
417         assertFormElementPresent(checkBoxName);
418         Assert.assertNull(dialog.getFormParameterValue(checkBoxName));
419     }
420
421     /**
422      * Assert that a specific option is present in a radio group.
423      *
424      * @param name radio group name.
425      * @param radioOption option to test for.
426      */

427     public void assertRadioOptionPresent(String JavaDoc name, String JavaDoc radioOption) {
428         assertFormElementPresent(name);
429         if (!dialog.hasRadioOption(name, radioOption))
430             Assert.fail("Unable to find option " + radioOption + " in radio group " + name);
431     }
432
433     /**
434      * Assert that a specific option is not present in a radio group.
435      *
436      * @param name radio group name.
437      * @param radioOption option to test for.
438      */

439     public void assertRadioOptionNotPresent(String JavaDoc name, String JavaDoc radioOption) {
440         assertFormElementPresent(name);
441         if (dialog.hasRadioOption(name, radioOption))
442             Assert.fail("Found option " + radioOption + " in radio group " + name);
443     }
444
445     /**
446      * Assert that a specific option is selected in a radio group.
447      *
448      * @param name radio group name.
449      * @param radioOption option to test for selection.
450      */

451     public void assertRadioOptionSelected(String JavaDoc name, String JavaDoc radioOption) {
452         assertFormElementPresent(name);
453         assertFormElementEquals(name, radioOption);
454     }
455
456     /**
457      * Assert that a specific option is not selected in a radio group.
458      *
459      * @param name radio group name.
460      * @param radioOption option to test for selection.
461      */

462     public void assertRadioOptionNotSelected(String JavaDoc name, String JavaDoc radioOption) {
463         assertFormElementPresent(name);
464         Assert.assertTrue("Radio option " + radioOption + " is not selected",
465                 !radioOption.equals(dialog.getFormParameterValue(name)));
466     }
467
468     /**
469      * Assert that the display values of a select element's options match a given array of strings.
470      *
471      * @param selectName name of the select element.
472      * @param expectedOptions expected display values for the select box.
473      */

474     public void assertOptionsEqual(String JavaDoc selectName, String JavaDoc[] expectedOptions) {
475         assertFormElementPresent(selectName);
476         assertArraysEqual(expectedOptions, dialog.getOptionsFor(selectName));
477     }
478
479     /**
480      * Assert that the display values of a select element's options do not match a given array of strings.
481      *
482      * @param selectName name of the select element.
483      * @param expectedOptions expected display values for the select box.
484      */

485     public void assertOptionsNotEqual(String JavaDoc selectName, String JavaDoc[] expectedOptions) {
486         assertFormElementPresent(selectName);
487         try {
488             assertOptionsEqual(selectName, expectedOptions);
489         } catch (AssertionFailedError e) {
490             return;
491         }
492         Assert.fail("Options not expected to be equal");
493     }
494
495     /**
496      * Assert that the values of a select element's options match a given array of strings.
497      *
498      * @param selectName name of the select element.
499      * @param expectedValues expected values for the select box.
500      */

501     public void assertOptionValuesEqual(String JavaDoc selectName, String JavaDoc[] expectedValues) {
502         assertFormElementPresent(selectName);
503         assertArraysEqual(expectedValues, dialog.getOptionValuesFor(selectName));
504
505     }
506
507     private void assertArraysEqual(String JavaDoc[] exptected, String JavaDoc[] returned) {
508         Assert.assertEquals("Arrays not same length", exptected.length, returned.length);
509         for (int i = 0; i < returned.length; i++) {
510             Assert.assertEquals("Elements " + i + "not equal", exptected[i], returned[i]);
511         }
512     }
513
514     /**
515      * Assert that the values of a select element's options do not match a given array of strings.
516      *
517      * @param selectName name of the select element.
518      * @param optionValues expected values for the select box.
519      */

520     public void assertOptionValuesNotEqual(String JavaDoc selectName, String JavaDoc[] optionValues) {
521         assertFormElementPresent(selectName);
522         try {
523             assertOptionValuesEqual(selectName, optionValues);
524         } catch (AssertionFailedError e) {
525             return;
526         }
527         Assert.fail("Values not expected to be equal");
528     }
529
530     /**
531      * Assert that the currently selected display value of a select box matches a given value.
532      *
533      * @param selectName name of the select element.
534      * @param option expected display value of the selected option.
535      */

536     public void assertOptionEquals(String JavaDoc selectName, String JavaDoc option) {
537         assertFormElementPresent(selectName);
538         Assert.assertEquals(option, dialog.getSelectedOption(selectName));
539     }
540
541     /**
542      * Assert that a submit button with a given name is present.
543      *
544      * @param buttonName
545      */

546     public void assertSubmitButtonPresent(String JavaDoc buttonName) {
547         assertFormPresent();
548         Assert.assertTrue("Submit Button [" + buttonName + "] not found.", dialog.hasSubmitButton(buttonName));
549     }
550
551     /**
552      * Assert that a submit button with a given name is not present.
553      *
554      * @param buttonName
555      */

556     public void assertSubmitButtonNotPresent(String JavaDoc buttonName) {
557         assertFormPresent();
558         Assert.assertFalse("Submit Button [" + buttonName + "] found.", dialog.hasSubmitButton(buttonName));
559     }
560
561     /**
562      * Assert that a submit button with a given name and value is present.
563      *
564      * @param buttonName
565      * @param expectedValue
566      */

567     public void assertSubmitButtonValue(String JavaDoc buttonName, String JavaDoc expectedValue) {
568         assertFormPresent();
569         assertSubmitButtonPresent(buttonName);
570         Assert.assertEquals(expectedValue, dialog.getSubmitButtonValue(buttonName));
571     }
572
573     /**
574      * Assert that a button with a given id is present.
575      *
576      * @param buttonId
577      */

578     public void assertButtonPresent(String JavaDoc buttonId) {
579         assertFormPresent();
580         Assert.assertTrue("Button [" + buttonId + "] not found.", dialog.hasButton(buttonId));
581     }
582
583     /**
584      * Assert that a button with a given id is not present.
585      *
586      * @param buttonId
587      */

588     public void assertButtonNotPresent(String JavaDoc buttonId) {
589         assertFormPresent();
590         Assert.assertFalse("Button [" + buttonId + "] found.", dialog.hasButton(buttonId));
591     }
592
593
594     /**
595      * Assert that a link with a given id is present in the response.
596      *
597      * @param linkId
598      */

599     public void assertLinkPresent(String JavaDoc linkId) {
600         Assert.assertTrue("Unable to find link with id [" + linkId + "]", dialog.isLinkPresent(linkId));
601     }
602
603     /**
604      * Assert that no link with the given id is present in the response.
605      *
606      * @param linkId
607      */

608     public void assertLinkNotPresent(String JavaDoc linkId) {
609         Assert.assertTrue("link with id [" + linkId + "] found in response", !dialog.isLinkPresent(linkId));
610     }
611
612     /**
613      * Assert that a link containing the supplied text is present.
614      *
615      * @param linkText
616      */

617     public void assertLinkPresentWithText(String JavaDoc linkText) {
618         Assert.assertTrue("Link with text [" + linkText + "] not found in response.", dialog.isLinkPresentWithText(linkText));
619     }
620
621     /**
622      * Assert that no link containing the supplied text is present.
623      *
624      * @param linkText
625      */

626     public void assertLinkNotPresentWithText(String JavaDoc linkText) {
627         Assert.assertTrue("Link with text [" + linkText + "] found in response.", !dialog.isLinkPresentWithText(linkText));
628     }
629
630     /**
631      * Assert that a link containing the supplied text is present.
632      *
633      * @param linkText
634      * @param index The 0-based index, when more than one link with the same
635      * text is expected.
636      */

637     public void assertLinkPresentWithText(String JavaDoc linkText, int index) {
638         Assert.assertTrue(
639             "Link with text ["
640                 + linkText
641                 + "] and index "
642                 + index
643                 + " not found in response.",
644             dialog.isLinkPresentWithText(linkText, index));
645     }
646
647     /**
648      * Assert that no link containing the supplied text is present.
649      *
650      * @param linkText
651      * @param index The 0-based index, when more than one link with the same
652      * text is expected.
653      */

654     public void assertLinkNotPresentWithText(String JavaDoc linkText, int index) {
655         Assert.assertTrue(
656             "Link with text ["
657                 + linkText
658                 + "] and index "
659                 + index
660                 + " found in response.",
661             !dialog.isLinkPresentWithText(linkText, index));
662     }
663
664     /**
665      * Assert that a link containing a specified image is present.
666      *
667      * @param imageFileName A suffix of the image's filename; for example, to match
668      * <tt>"images/my_icon.png"</tt>, you could just pass in
669      * <tt>"my_icon.png"</tt>.
670      */

671     public void assertLinkPresentWithImage(String JavaDoc imageFileName) {
672         Assert.assertTrue("Link with image file [" + imageFileName + "] not found in response.", dialog.isLinkPresentWithImage(imageFileName));
673     }
674
675     /**
676      * Assert that a link containing a specified image is not present.
677      *
678      * @param imageFileName A suffix of the image's filename; for example, to match
679      * <tt>"images/my_icon.png"</tt>, you could just pass in
680      * <tt>"my_icon.png"</tt>.
681      */

682     public void assertLinkNotPresentWithImage(String JavaDoc imageFileName) {
683         Assert.assertTrue("Link with image file [" + imageFileName + "] found in response.", !dialog.isLinkPresentWithImage(imageFileName));
684     }
685
686     /**
687      * Assert that an element with a given id is present.
688      *
689      * @param anID element id to test for.
690      */

691     public void assertElementPresent(String JavaDoc anID) {
692         Assert.assertNotNull("Unable to locate element with id \"" + anID + "\"", dialog.getElement(anID));
693     }
694
695     /**
696      * Assert that an element with a given id is not present.
697      *
698      * @param anID element id to test for.
699      */

700     public void assertElementNotPresent(String JavaDoc anID) {
701         Assert.assertNull("Located element with id \"" + anID + "\"", dialog.getElement(anID));
702     }
703
704     /**
705      * Assert that a given element contains specific text.
706      *
707      * @param elementID id of element to be inspected.
708      * @param text to check for.
709      */

710     public void assertTextInElement(String JavaDoc elementID, String JavaDoc text) {
711         Element JavaDoc element = dialog.getElement(elementID);
712         Assert.assertNotNull("Unable to locate element with id \"" + elementID + "\"", element);
713         Assert.assertTrue("Unable to locate [" + text + "] in element \"" + elementID + "\"", dialog.isTextInElement(element, text));
714     }
715     
716     public void assertTextNotInElement(String JavaDoc elementID, String JavaDoc text) {
717         assertElementPresent(elementID);
718         Element JavaDoc element = dialog.getElement(elementID);
719         Assert.assertNotNull("Unable to locate element with id \"" + elementID + "\"", element);
720         Assert.assertFalse("Text [" + text +"] found in element [" + elementID + "] when not expected", dialog.isTextInElement(element, text));
721     }
722
723     /**
724      * Assert that a window with the given name is open.
725      *
726      * @param windowName
727      */

728     public void assertWindowPresent(String JavaDoc windowName) {
729         Assert.assertNotNull("Unable to locate window [" + windowName + "].", dialog.getWindow(windowName));
730     }
731
732     /**
733      * Assert that a frame with the given name is present.
734      *
735      * @param frameName
736      */

737     public void assertFramePresent(String JavaDoc frameName) {
738         Assert.assertNotNull("Unable to locate frame [" + frameName + "].", dialog.getFrame(frameName));
739     }
740     
741     /**
742      * Checks to see if a cookie is present in the response.
743      * Contributed by Vivek Venugopalan.
744      *
745      * @param cookieName The cookie name
746      */

747     public void assertCookiePresent(String JavaDoc cookieName) {
748         Assert.assertTrue("Could not find Cookie : [" + cookieName + "]", dialog.hasCookie(cookieName));
749     }
750     
751     public void assertCookieValueEquals(String JavaDoc cookieName, String JavaDoc expectedValue) {
752         assertCookiePresent(cookieName);
753         Assert.assertEquals(expectedValue, dialog.getCookieValue(cookieName));
754     }
755     
756     public void dumpCookies() {
757         dumpCookies(System.out);
758     }
759     
760     public void dumpCookies(PrintStream JavaDoc stream) {
761         dialog.dumpCookies(stream);
762     }
763     
764
765 //Form interaction methods
766

767     /**
768      * Begin interaction with a specified form. If form interaction methods are called without
769      * explicitly calling this method first, jWebUnit will attempt to determine itself which form
770      * is being manipulated.
771      *
772      * It is not necessary to call this method if their is only one form on the current page.
773      *
774      * @param nameOrId name or id of the form to work with.
775      */

776     public void setWorkingForm(String JavaDoc nameOrId) {
777         dialog.setWorkingForm(nameOrId);
778     }
779
780     /**
781      * Set the value of a form input element.
782      *
783      * @param formElementName name of form element.
784      * @param value
785      */

786     public void setFormElement(String JavaDoc formElementName, String JavaDoc value) {
787         assertFormPresent();
788         assertFormElementPresent(formElementName);
789         dialog.setFormParameter(formElementName, value);
790     }
791
792     /**
793      * Set the value of a form input element. The element is identified by a
794      * preceding "label". For example, in "<code>Home Address : &lt;input
795      * type='text' name='home_addr' /&gt;</code>", "<code>Home Address</code>"
796      * could be used as a label. The label must appear within the associated
797      * <code>&lt;form&gt;</code> tag.
798      *
799      * @param formElementLabel label preceding form element.
800      * @param value
801      */

802     protected void setFormElementWithLabel(String JavaDoc formElementLabel,
803                                            String JavaDoc value) {
804         String JavaDoc name = dialog.getFormElementNameForLabel(formElementLabel);
805         Assert.assertNotNull("Did not find form element with label [" + formElementLabel + "].", name);
806         dialog.setFormParameter(name, value);
807     }
808
809     /**
810      * Select a specified checkbox.
811      *
812      * @param checkBoxName name of checkbox to be deselected.
813      */

814     public void checkCheckbox(String JavaDoc checkBoxName) {
815         assertFormElementPresent(checkBoxName);
816         dialog.setFormParameter(checkBoxName, "on");
817     }
818
819     public void checkCheckbox(String JavaDoc checkBoxName, String JavaDoc value) {
820         assertFormElementPresent(checkBoxName);
821         dialog.updateFormParameter(checkBoxName, value);
822     }
823
824     /**
825      * Deselect a specified checkbox.
826      *
827      * @param checkBoxName name of checkbox to be deselected.
828      */

829     public void uncheckCheckbox(String JavaDoc checkBoxName) {
830         assertFormElementPresent(checkBoxName);
831         dialog.removeFormParameter(checkBoxName);
832     }
833
834     public void uncheckCheckbox(String JavaDoc checkBoxName, String JavaDoc value) {
835         assertFormElementPresent(checkBoxName);
836         dialog.removeFormParameterWithValue(checkBoxName, value);
837     }
838
839     /**
840      * Select an option with a given display value in a select element.
841      *
842      * @param selectName name of select element.
843      * @param option display value of option to be selected.
844      */

845     public void selectOption(String JavaDoc selectName, String JavaDoc option) {
846         assertFormElementPresent(selectName);
847         dialog.selectOption(selectName, option);
848     }
849
850     //Form submission and link navigation methods
851

852     /**
853      * Submit form - default submit button will be used (unnamed submit button, or
854      * named button if there is only one on the form.
855      */

856     public void submit() {
857         assertFormPresent();
858         dialog.submit();
859     }
860
861     /**
862      * Submit form by pressing named button.
863      *
864      * @param buttonName name of button to submit form with.
865      */

866     public void submit(String JavaDoc buttonName) {
867         assertSubmitButtonPresent(buttonName);
868         dialog.submit(buttonName);
869     }
870
871     /**
872      * Reset the current form.
873      */

874     public void reset() {
875         dialog.reset();
876     }
877
878     /**
879      * Navigate by selection of a link containing given text.
880      *
881      * @param linkText
882      */

883     public void clickLinkWithText(String JavaDoc linkText) {
884         assertLinkPresentWithText(linkText);
885         dialog.clickLinkWithText(linkText);
886     }
887
888     /**
889      * Navigate by selection of a link containing given text.
890      *
891      * @param linkText
892      * @param index The 0-based index, when more than one link with the same
893      * text is expected.
894      */

895     public void clickLinkWithText(String JavaDoc linkText, int index) {
896         assertLinkPresentWithText(linkText, index);
897         dialog.clickLinkWithText(linkText, index);
898     }
899
900     /**
901      * Search for labelText in the document, then search forward until
902      * finding a link called linkText. Click it.
903      */

904     public void clickLinkWithTextAfterText(String JavaDoc linkText, String JavaDoc labelText) {
905         dialog.clickLinkWithTextAfterText(linkText, labelText);
906     }
907
908     /**
909      * Click the button with the given id.
910      *
911      * @param buttonId
912      */

913     public void clickButton(String JavaDoc buttonId) {
914         assertButtonPresent(buttonId);
915         dialog.clickButton(buttonId);
916     }
917
918     /**
919      * Navigate by selection of a link with a given image.
920      *
921      * @param imageFileName A suffix of the image's filename; for example, to match
922      * <tt>"images/my_icon.png"</tt>, you could just pass in
923      * <tt>"my_icon.png"</tt>.
924      */

925     public void clickLinkWithImage(String JavaDoc imageFileName) {
926         assertLinkPresentWithImage(imageFileName);
927         dialog.clickLinkWithImage(imageFileName);
928     }
929
930
931     /**
932      * Navigate by selection of a link with given id.
933      *
934      * @param linkId id of link
935      */

936     public void clickLink(String JavaDoc linkId) {
937         assertLinkPresent(linkId);
938         dialog.clickLink(linkId);
939     }
940
941 //Window and Frame Navigation Methods
942

943     /**
944      * Make a given window active (current response will be window's contents).
945      *
946      * @param windowName
947      */

948     public void gotoWindow(String JavaDoc windowName) {
949         assertWindowPresent(windowName);
950         dialog.gotoWindow(windowName);
951     }
952
953     /**
954      * Make the root window active.
955      */

956     public void gotoRootWindow() {
957         dialog.gotoRootWindow();
958     }
959
960     /**
961      * Make the named frame active (current response will be frame's contents).
962      *
963      * @param frameName
964      */

965     public void gotoFrame(String JavaDoc frameName) {
966         dialog.gotoFrame(frameName);
967     }
968
969     /**
970      * Patch sumbitted by Alex Chaffee.
971      */

972     public void gotoPage(String JavaDoc url) {
973         dialog.gotoPage(createUrl(url));
974     }
975
976 //Debug methods
977

978
979     /**
980      * Dump html of current response to System.out - for debugging purposes.
981      *
982      * @param stream
983      */

984     public void dumpResponse() {
985         dialog.dumpResponse();
986     }
987     
988     /**
989      * Dump html of current response to a specified stream - for debugging purposes.
990      *
991      * @param stream
992      */

993     public void dumpResponse(PrintStream JavaDoc stream) {
994         dialog.dumpResponse(stream);
995     }
996
997     /**
998      * Dump the table as the 2D array that is used for assertions - for debugging purposes.
999      *
1000     * @param tableNameOrId
1001     * @param stream
1002     */

1003    public void dumpTable(String JavaDoc tableNameOrId, PrintStream JavaDoc stream) {
1004        dialog.dumpTable(tableNameOrId, stream);
1005    }
1006
1007    /**
1008     * Dump the table as the 2D array that is used for assertions - for debugging purposes.
1009     *
1010     * @param tableNameOrId
1011     * @param table
1012     */

1013    public void dumpTable(String JavaDoc tableNameOrId, String JavaDoc[][] table) {
1014        dialog.dumpTable(tableNameOrId, table);
1015    }
1016
1017    /**
1018     * Dump the table as the 2D array that is used for assertions - for debugging purposes.
1019     *
1020     * @param tableNameOrId
1021     * @param table
1022     * @param stream
1023     */

1024    public void dumpTable(String JavaDoc tableNameOrId, String JavaDoc[][] table, PrintStream JavaDoc stream) {
1025        dialog.dumpTable(tableNameOrId, table, stream);
1026    }
1027
1028}
1029
Popular Tags