1 5 package net.sourceforge.jwebunit; 6 7 import java.io.PrintStream ; 8 import java.util.Locale ; 9 import java.util.ResourceBundle ; 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 ; 16 17 25 public class WebTester { 26 private HttpUnitDialog dialog; 27 private TestContext context = new TestContext(); 28 29 35 public HttpUnitDialog getDialog() { 36 return dialog; 37 } 38 39 44 public TestContext getTestContext() { 45 return context; 46 } 47 48 53 public void beginAt(String relativeURL) { 54 String url = createUrl(relativeURL); 55 dialog = new HttpUnitDialog(url, context); 56 } 57 58 private String createUrl(String suffix) { 59 suffix = suffix.startsWith("/") ? suffix.substring(1) : suffix; 60 return getTestContext().getBaseUrl() + suffix; 61 } 62 63 70 public String getMessage(String key) { 71 String message = ""; 72 Locale locale = context.getLocale(); 73 try { 74 message = ResourceBundle.getBundle(getTestContext().getResourceBundleName(), locale).getString(key); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 throw new RuntimeException ("No message found for key [" + key + "]." + 78 "\nError: " + ExceptionUtility.stackTraceToString(e)); 79 } 80 return context.toEncodedString(message); 81 } 82 83 85 90 public void assertTitleEquals(String title) { 91 Assert.assertEquals(title, dialog.getResponsePageTitle()); 92 } 93 94 99 public void assertTitleEqualsKey(String titleKey) { 100 Assert.assertEquals(getMessage(titleKey), dialog.getResponsePageTitle()); 101 } 102 103 108 public void assertKeyPresent(String key) { 109 assertTextPresent(getMessage(key)); 110 } 111 112 117 public void assertTextPresent(String text) { 118 if (!dialog.isTextInResponse(text)) 119 Assert.fail("Expected text not found in response: [" + text + "]"); 120 } 121 122 127 public void assertKeyNotPresent(String key) { 128 assertTextNotPresent(getMessage(key)); 129 } 130 131 136 public void assertTextNotPresent(String text) { 137 if (dialog.isTextInResponse(text)) 138 Assert.fail("Text found in response when not expected: [" + text + "]"); 139 } 140 141 146 public void assertTablePresent(String tableSummaryOrId) { 147 if (dialog.getWebTableBySummaryOrId(tableSummaryOrId) == null) 148 Assert.fail("Unable to locate table \"" + tableSummaryOrId + "\""); 149 } 150 151 156 public void assertTableNotPresent(String tableSummaryOrId) { 157 if (dialog.getWebTableBySummaryOrId(tableSummaryOrId) != null) 158 Assert.fail("Located table \"" + tableSummaryOrId + "\""); 159 } 160 161 167 public void assertKeyInTable(String tableSummaryOrId, String key) { 168 assertTextInTable(tableSummaryOrId, getMessage(key)); 169 } 170 171 177 public void assertTextInTable(String tableSummaryOrId, String text) { 178 assertTablePresent(tableSummaryOrId); 179 Assert.assertTrue("Could not find: [" + text + "]" + 180 "in table [" + tableSummaryOrId + "]", 181 dialog.isTextInTable(tableSummaryOrId, text)); 182 } 183 184 190 public void assertKeysInTable(String tableSummaryOrId, String [] keys) { 191 for (int i = 0; i < keys.length; i++) { 192 assertKeyInTable(tableSummaryOrId, keys[i]); 193 } 194 } 195 196 202 public void assertTextInTable(String tableSummaryOrId, String [] text) { 203 for (int i = 0; i < text.length; i++) { 204 assertTextInTable(tableSummaryOrId, text[i]); 205 } 206 } 207 208 214 public void assertKeyNotInTable(String tableSummaryOrId, String key) { 215 assertTextNotInTable(tableSummaryOrId, getMessage(key)); 216 } 217 218 224 public void assertTextNotInTable(String tableSummaryOrId, String text) { 225 assertTablePresent(tableSummaryOrId); 226 Assert.assertTrue("Found text: [" + text + "] in table [" + 227 tableSummaryOrId + "]", 228 !dialog.isTextInTable(tableSummaryOrId, text)); 229 } 230 231 237 public void assertTextNotInTable(String tableSummaryOrId, String [] text) { 238 for (int i = 0; i < text.length; i++) { 239 assertTextNotInTable(tableSummaryOrId, text[i]); 240 } 241 } 242 243 249 public void assertTableEquals(String tableSummaryOrId, ExpectedTable expectedTable) { 250 assertTableEquals(tableSummaryOrId, expectedTable.getExpectedStrings()); 251 } 252 253 259 public void assertTableEquals(String tableSummaryOrId, String [][] expectedCellValues) { 260 assertTableRowsEqual(tableSummaryOrId, 0, expectedCellValues); 261 } 262 263 270 public void assertTableRowsEqual(String tableSummaryOrId, int startRow, ExpectedTable expectedTable) { 271 assertTableRowsEqual(tableSummaryOrId, startRow, expectedTable.getExpectedStrings()); 272 } 273 274 281 public void assertTableRowsEqual(String tableSummaryOrId, int startRow, String [][] expectedCellValues) { 282 assertTablePresent(tableSummaryOrId); 283 String [][] 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 [] 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 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 305 public void assertFormElementPresent(String formElementName) { 306 assertFormPresent(); 307 Assert.assertTrue("Did not find form element with name [" + formElementName + "].", 308 dialog.hasFormParameterNamed(formElementName)); 309 } 310 311 316 public void assertFormElementNotPresent(String 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 } 324 } 325 326 332 public void assertFormElementPresentWithLabel(String formElementLabel) { 333 Assert.assertTrue("Did not find form element with label [" + formElementLabel + "].", 334 dialog.hasFormParameterLabeled(formElementLabel)); 335 } 336 337 343 public void assertFormElementNotPresentWithLabel(String formElementLabel) { 344 Assert.assertFalse("Found form element with label [" + formElementLabel + "].", 345 dialog.hasFormParameterLabeled(formElementLabel)); 346 } 347 348 352 public void assertFormPresent() { 353 Assert.assertTrue("No form present", dialog.hasForm()); 354 } 355 356 360 public void assertFormPresent(String nameOrID) { 361 Assert.assertTrue("No form present with name or id [" + nameOrID + "]", dialog.hasForm(nameOrID)); 362 } 363 364 368 public void assertFormNotPresent() { 369 Assert.assertFalse("A form is present", dialog.hasForm()); 370 } 371 372 376 public void assertFormNotPresent(String nameOrID) { 377 Assert.assertFalse("Form present with name or id [" + nameOrID + "]", dialog.hasForm(nameOrID)); 378 } 379 380 386 public void assertFormElementEquals(String formElementName, String expectedValue) { 387 assertFormElementPresent(formElementName); 388 Assert.assertEquals(expectedValue, dialog.getFormParameterValue(formElementName)); 389 } 390 391 396 public void assertFormElementEmpty(String formElementName) { 397 assertFormElementPresent(formElementName); 398 Assert.assertEquals("", dialog.getFormParameterValue(formElementName)); 399 } 400 401 406 public void assertCheckboxSelected(String checkBoxName) { 407 assertFormElementPresent(checkBoxName); 408 Assert.assertEquals("on", dialog.getFormParameterValue(checkBoxName)); 409 } 410 411 416 public void assertCheckboxNotSelected(String checkBoxName) { 417 assertFormElementPresent(checkBoxName); 418 Assert.assertNull(dialog.getFormParameterValue(checkBoxName)); 419 } 420 421 427 public void assertRadioOptionPresent(String name, String 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 439 public void assertRadioOptionNotPresent(String name, String radioOption) { 440 assertFormElementPresent(name); 441 if (dialog.hasRadioOption(name, radioOption)) 442 Assert.fail("Found option " + radioOption + " in radio group " + name); 443 } 444 445 451 public void assertRadioOptionSelected(String name, String radioOption) { 452 assertFormElementPresent(name); 453 assertFormElementEquals(name, radioOption); 454 } 455 456 462 public void assertRadioOptionNotSelected(String name, String radioOption) { 463 assertFormElementPresent(name); 464 Assert.assertTrue("Radio option " + radioOption + " is not selected", 465 !radioOption.equals(dialog.getFormParameterValue(name))); 466 } 467 468 474 public void assertOptionsEqual(String selectName, String [] expectedOptions) { 475 assertFormElementPresent(selectName); 476 assertArraysEqual(expectedOptions, dialog.getOptionsFor(selectName)); 477 } 478 479 485 public void assertOptionsNotEqual(String selectName, String [] 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 501 public void assertOptionValuesEqual(String selectName, String [] expectedValues) { 502 assertFormElementPresent(selectName); 503 assertArraysEqual(expectedValues, dialog.getOptionValuesFor(selectName)); 504 505 } 506 507 private void assertArraysEqual(String [] exptected, String [] 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 520 public void assertOptionValuesNotEqual(String selectName, String [] 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 536 public void assertOptionEquals(String selectName, String option) { 537 assertFormElementPresent(selectName); 538 Assert.assertEquals(option, dialog.getSelectedOption(selectName)); 539 } 540 541 546 public void assertSubmitButtonPresent(String buttonName) { 547 assertFormPresent(); 548 Assert.assertTrue("Submit Button [" + buttonName + "] not found.", dialog.hasSubmitButton(buttonName)); 549 } 550 551 556 public void assertSubmitButtonNotPresent(String buttonName) { 557 assertFormPresent(); 558 Assert.assertFalse("Submit Button [" + buttonName + "] found.", dialog.hasSubmitButton(buttonName)); 559 } 560 561 567 public void assertSubmitButtonValue(String buttonName, String expectedValue) { 568 assertFormPresent(); 569 assertSubmitButtonPresent(buttonName); 570 Assert.assertEquals(expectedValue, dialog.getSubmitButtonValue(buttonName)); 571 } 572 573 578 public void assertButtonPresent(String buttonId) { 579 assertFormPresent(); 580 Assert.assertTrue("Button [" + buttonId + "] not found.", dialog.hasButton(buttonId)); 581 } 582 583 588 public void assertButtonNotPresent(String buttonId) { 589 assertFormPresent(); 590 Assert.assertFalse("Button [" + buttonId + "] found.", dialog.hasButton(buttonId)); 591 } 592 593 594 599 public void assertLinkPresent(String linkId) { 600 Assert.assertTrue("Unable to find link with id [" + linkId + "]", dialog.isLinkPresent(linkId)); 601 } 602 603 608 public void assertLinkNotPresent(String linkId) { 609 Assert.assertTrue("link with id [" + linkId + "] found in response", !dialog.isLinkPresent(linkId)); 610 } 611 612 617 public void assertLinkPresentWithText(String linkText) { 618 Assert.assertTrue("Link with text [" + linkText + "] not found in response.", dialog.isLinkPresentWithText(linkText)); 619 } 620 621 626 public void assertLinkNotPresentWithText(String linkText) { 627 Assert.assertTrue("Link with text [" + linkText + "] found in response.", !dialog.isLinkPresentWithText(linkText)); 628 } 629 630 637 public void assertLinkPresentWithText(String 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 654 public void assertLinkNotPresentWithText(String 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 671 public void assertLinkPresentWithImage(String imageFileName) { 672 Assert.assertTrue("Link with image file [" + imageFileName + "] not found in response.", dialog.isLinkPresentWithImage(imageFileName)); 673 } 674 675 682 public void assertLinkNotPresentWithImage(String imageFileName) { 683 Assert.assertTrue("Link with image file [" + imageFileName + "] found in response.", !dialog.isLinkPresentWithImage(imageFileName)); 684 } 685 686 691 public void assertElementPresent(String anID) { 692 Assert.assertNotNull("Unable to locate element with id \"" + anID + "\"", dialog.getElement(anID)); 693 } 694 695 700 public void assertElementNotPresent(String anID) { 701 Assert.assertNull("Located element with id \"" + anID + "\"", dialog.getElement(anID)); 702 } 703 704 710 public void assertTextInElement(String elementID, String text) { 711 Element 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 elementID, String text) { 717 assertElementPresent(elementID); 718 Element 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 728 public void assertWindowPresent(String windowName) { 729 Assert.assertNotNull("Unable to locate window [" + windowName + "].", dialog.getWindow(windowName)); 730 } 731 732 737 public void assertFramePresent(String frameName) { 738 Assert.assertNotNull("Unable to locate frame [" + frameName + "].", dialog.getFrame(frameName)); 739 } 740 741 747 public void assertCookiePresent(String cookieName) { 748 Assert.assertTrue("Could not find Cookie : [" + cookieName + "]", dialog.hasCookie(cookieName)); 749 } 750 751 public void assertCookieValueEquals(String cookieName, String 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 stream) { 761 dialog.dumpCookies(stream); 762 } 763 764 765 767 776 public void setWorkingForm(String nameOrId) { 777 dialog.setWorkingForm(nameOrId); 778 } 779 780 786 public void setFormElement(String formElementName, String value) { 787 assertFormPresent(); 788 assertFormElementPresent(formElementName); 789 dialog.setFormParameter(formElementName, value); 790 } 791 792 802 protected void setFormElementWithLabel(String formElementLabel, 803 String value) { 804 String name = dialog.getFormElementNameForLabel(formElementLabel); 805 Assert.assertNotNull("Did not find form element with label [" + formElementLabel + "].", name); 806 dialog.setFormParameter(name, value); 807 } 808 809 814 public void checkCheckbox(String checkBoxName) { 815 assertFormElementPresent(checkBoxName); 816 dialog.setFormParameter(checkBoxName, "on"); 817 } 818 819 public void checkCheckbox(String checkBoxName, String value) { 820 assertFormElementPresent(checkBoxName); 821 dialog.updateFormParameter(checkBoxName, value); 822 } 823 824 829 public void uncheckCheckbox(String checkBoxName) { 830 assertFormElementPresent(checkBoxName); 831 dialog.removeFormParameter(checkBoxName); 832 } 833 834 public void uncheckCheckbox(String checkBoxName, String value) { 835 assertFormElementPresent(checkBoxName); 836 dialog.removeFormParameterWithValue(checkBoxName, value); 837 } 838 839 845 public void selectOption(String selectName, String option) { 846 assertFormElementPresent(selectName); 847 dialog.selectOption(selectName, option); 848 } 849 850 852 856 public void submit() { 857 assertFormPresent(); 858 dialog.submit(); 859 } 860 861 866 public void submit(String buttonName) { 867 assertSubmitButtonPresent(buttonName); 868 dialog.submit(buttonName); 869 } 870 871 874 public void reset() { 875 dialog.reset(); 876 } 877 878 883 public void clickLinkWithText(String linkText) { 884 assertLinkPresentWithText(linkText); 885 dialog.clickLinkWithText(linkText); 886 } 887 888 895 public void clickLinkWithText(String linkText, int index) { 896 assertLinkPresentWithText(linkText, index); 897 dialog.clickLinkWithText(linkText, index); 898 } 899 900 904 public void clickLinkWithTextAfterText(String linkText, String labelText) { 905 dialog.clickLinkWithTextAfterText(linkText, labelText); 906 } 907 908 913 public void clickButton(String buttonId) { 914 assertButtonPresent(buttonId); 915 dialog.clickButton(buttonId); 916 } 917 918 925 public void clickLinkWithImage(String imageFileName) { 926 assertLinkPresentWithImage(imageFileName); 927 dialog.clickLinkWithImage(imageFileName); 928 } 929 930 931 936 public void clickLink(String linkId) { 937 assertLinkPresent(linkId); 938 dialog.clickLink(linkId); 939 } 940 941 943 948 public void gotoWindow(String windowName) { 949 assertWindowPresent(windowName); 950 dialog.gotoWindow(windowName); 951 } 952 953 956 public void gotoRootWindow() { 957 dialog.gotoRootWindow(); 958 } 959 960 965 public void gotoFrame(String frameName) { 966 dialog.gotoFrame(frameName); 967 } 968 969 972 public void gotoPage(String url) { 973 dialog.gotoPage(createUrl(url)); 974 } 975 976 978 979 984 public void dumpResponse() { 985 dialog.dumpResponse(); 986 } 987 988 993 public void dumpResponse(PrintStream stream) { 994 dialog.dumpResponse(stream); 995 } 996 997 1003 public void dumpTable(String tableNameOrId, PrintStream stream) { 1004 dialog.dumpTable(tableNameOrId, stream); 1005 } 1006 1007 1013 public void dumpTable(String tableNameOrId, String [][] table) { 1014 dialog.dumpTable(tableNameOrId, table); 1015 } 1016 1017 1024 public void dumpTable(String tableNameOrId, String [][] table, PrintStream stream) { 1025 dialog.dumpTable(tableNameOrId, table, stream); 1026 } 1027 1028} 1029 | Popular Tags |