1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.web.test;
17
18 import java.io.IOException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.jasper.tagplugins.jstl.core.Url;
25 import org.apache.log4j.Logger;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.kuali.rice.kns.UserSession;
29 import org.kuali.rice.kns.service.DocumentService;
30 import org.kuali.rice.kns.service.KNSServiceLocator;
31 import org.kuali.rice.kns.util.GlobalVariables;
32 import org.kuali.rice.test.web.HtmlUnitUtil;
33
34 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
35 import com.gargoylesoftware.htmlunit.Page;
36 import com.gargoylesoftware.htmlunit.WebClient;
37 import com.gargoylesoftware.htmlunit.html.BaseFrame;
38 import com.gargoylesoftware.htmlunit.html.ClickableElement;
39 import com.gargoylesoftware.htmlunit.html.DomNode;
40 import com.gargoylesoftware.htmlunit.html.FrameWindow;
41 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
42 import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
43 import com.gargoylesoftware.htmlunit.html.HtmlElement;
44 import com.gargoylesoftware.htmlunit.html.HtmlFileInput;
45 import com.gargoylesoftware.htmlunit.html.HtmlForm;
46 import com.gargoylesoftware.htmlunit.html.HtmlHiddenInput;
47 import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
48 import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame;
49 import com.gargoylesoftware.htmlunit.html.HtmlListItem;
50 import com.gargoylesoftware.htmlunit.html.HtmlOption;
51 import com.gargoylesoftware.htmlunit.html.HtmlPage;
52 import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
53 import com.gargoylesoftware.htmlunit.html.HtmlSelect;
54 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
55 import com.gargoylesoftware.htmlunit.html.HtmlTable;
56 import com.gargoylesoftware.htmlunit.html.HtmlTableBody;
57 import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
58 import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
59 import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
60 import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
61
62
63
64
65
66 public abstract class WebTestBase extends ServerTestBase {
67 private static final Logger LOG = Logger.getLogger(WebTestBase.class);
68
69 protected static String HELP_PAGE_TITLE = "Kuali :: Kuali Help";
70 protected static String USER_NETWORK_ID = "admin";
71
72 protected WebClient webClient = null;
73 protected DocumentService documentService = null;
74
75 private HtmlPage portalPage;
76
77
78
79
80
81
82 @Before
83 public void setUp() throws Exception {
84 super.setUp();
85 GlobalVariables.setUserSession(new UserSession(USER_NETWORK_ID));
86 documentService = KNSServiceLocator.getDocumentService();
87 webClient = new WebClient();
88
89 setPortalPage(buildPageFromUrl(HtmlUnitUtil.BASE_URL, HTML_PAGE_TITLE_TEXT));
90
91 }
92
93
94
95
96
97
98 @After
99 public void tearDown() throws Exception {
100 super.tearDown();
101 GlobalVariables.setUserSession(null);
102 documentService = null;
103 webClient = null;
104 }
105
106
107
108
109
110
111
112 protected final void setPortalPage(HtmlPage portalPage) {
113 this.portalPage = portalPage;
114 }
115
116
117
118
119
120
121
122 protected final HtmlPage getPortalPage() throws IOException {
123 return this.portalPage;
124 }
125
126
127
128
129
130
131
132
133
134
135 protected final HtmlPage buildPageFromUrl(String url, String title) throws IOException {
136 return buildPageFromUrl(new URL(url), title);
137 }
138
139
140
141
142
143
144
145
146
147
148 protected final HtmlPage buildPageFromUrl(URL url, String title) throws IOException {
149 HtmlPage retval = (HtmlPage) webClient.getPage(url);
150 assertEquals(title, retval.getTitleText());
151 return retval;
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 protected final HtmlPage clickOn(HtmlPage page, String id) throws IOException {
175 return clickOn(page, id, null);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 protected final HtmlPage clickOn(HtmlPage page, String id, String nextPageTitle) throws IOException {
202 HtmlElement element = getElement(page, id);
203 assertTrue(id +" not found",element != null);
204 assertTrue(element instanceof ClickableElement);
205
206 return clickOn((ClickableElement) element, nextPageTitle);
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220 protected final HtmlPage clickOn(HtmlElement element) throws IOException {
221 return clickOn(element, null);
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 protected final HtmlPage clickOn(HtmlElement element, String nextPageTitle) throws IOException {
240 assertTrue(element instanceof ClickableElement);
241
242 ClickableElement clickable = (ClickableElement) element;
243 Page nextPage = clickable.click();
244
245 assertTrue(nextPage != null);
246 assertTrue(nextPage instanceof HtmlPage);
247
248 HtmlPage htmlNextPage = (HtmlPage) nextPage;
249
250 htmlNextPage = checkForLoginPage(htmlNextPage);
251
252 if (nextPageTitle != null) {
253 assertEquals(nextPageTitle, htmlNextPage.getTitleText());
254 }
255
256 return htmlNextPage;
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 protected final HtmlPage clickOnLookup(HtmlPage page, String tag) throws IOException {
279 HtmlImageInput element = getLookup(page, tag);
280 assertTrue(element != null);
281
282 return clickOn(element);
283 }
284
285
286
287
288
289
290 protected final void assertContains(HtmlPage page, String text) {
291 assertTrue(page.asText().contains(text));
292 }
293
294
295
296
297
298
299 protected final void assertDoesNotContain(HtmlPage page, String text) {
300 assertTrue(!page.asText().contains(text));
301 }
302
303
304
305
306
307
308 protected final void assertContains(HtmlElement element, String text) {
309 assertTrue(element.asText().contains(text));
310 }
311
312
313
314
315
316
317 protected final void assertDoesNotContain(HtmlElement element, String text) {
318 assertTrue(!element.asText().contains(text));
319 }
320
321
322
323
324
325
326
327 protected final void assertSelectOptionsSize(HtmlPage page, String elementId, int size) {
328 HtmlElement element = page.getHtmlElementById(elementId);
329 assertTrue(element != null);
330
331 if (element instanceof HtmlSelect) {
332 HtmlSelect selectField = (HtmlSelect) element;
333 assertEquals(selectField.getOptionSize(), size);
334 }
335 else {
336 assertTrue("Not a Select Field", false);
337 }
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 protected final HtmlPage lookup(HtmlPage page, String tag) throws IOException {
365 return lookup(page, tag, null, null);
366 }
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396 protected final HtmlPage lookup(HtmlPage page, String tag, String searchFieldId, String searchValue) throws IOException {
397
398 HtmlPage lookupPage = clickOnLookup(page, tag);
399
400 if (searchFieldId != null) {
401 assertTrue(searchValue != null);
402 setFieldValue(lookupPage, searchFieldId, searchValue);
403 }
404
405
406 HtmlImageInput searchBtn = (HtmlImageInput) getElement(lookupPage, "methodToCall.search", "search", "search");
407 HtmlPage resultsPage = (HtmlPage) searchBtn.click();
408
409 HtmlTable table = (HtmlTable) getElement(resultsPage, "row");
410 assertTrue("No data to return", table != null);
411
412 HtmlTableBody body = (HtmlTableBody) table.getBodies().get(0);
413 List rows = body.getRows();
414
415 HtmlTableRow row = (HtmlTableRow) rows.get(0);
416 List cells = row.getCells();
417 HtmlTableCell cell = (HtmlTableCell) cells.get(cells.size() - 1);
418 HtmlAnchor anchor = (HtmlAnchor) getFirstChild(cell);
419 page = (HtmlPage) anchor.click();
420
421 return page;
422 }
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444 protected final HtmlPage multiLookup(HtmlPage page, String tag) throws IOException {
445 return multiLookup(page, tag, null, null);
446 }
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475 protected final HtmlPage multiLookup(HtmlPage page, String tag, String searchFieldId, String searchValue) throws IOException {
476 HtmlPage lookupPage = clickOnLookup(page, tag);
477
478 if (searchFieldId != null) {
479 assertTrue(searchValue != null);
480 setFieldValue(lookupPage, searchFieldId, searchValue);
481 }
482
483
484 HtmlImageInput searchBtn = (HtmlImageInput) getElement(lookupPage, "methodToCall.search", "search", "search");
485 HtmlPage resultsPage = (HtmlPage) searchBtn.click();
486
487 HtmlImageInput selectAllBtn = (HtmlImageInput) getElement(resultsPage, "methodToCall.selectAll.(::;false;::).x", null, null);
488 HtmlPage selectedPage = (HtmlPage) selectAllBtn.click();
489
490 HtmlImageInput returnAllBtn = (HtmlImageInput) getElement(selectedPage, "methodToCall.prepareToReturnSelectedResults.x", null, null);
491 HtmlPage returnPage = (HtmlPage) returnAllBtn.click();
492
493 return returnPage;
494 }
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512 protected final void setFieldValue(HtmlPage page, String fieldId, String fieldValue) {
513 HtmlElement element = getElement(page, fieldId);
514 assertTrue(element != null);
515
516 if (element instanceof HtmlTextInput) {
517 HtmlTextInput textField = (HtmlTextInput) element;
518 textField.setValueAttribute(fieldValue);
519 }
520 else if (element instanceof HtmlTextArea) {
521 HtmlTextArea textAreaField = (HtmlTextArea) element;
522 textAreaField.setText(fieldValue);
523 }
524 else if (element instanceof HtmlHiddenInput) {
525 HtmlHiddenInput hiddenField = (HtmlHiddenInput) element;
526 hiddenField.setValueAttribute(fieldValue);
527 }
528 else if (element instanceof HtmlSelect) {
529 HtmlSelect selectField = (HtmlSelect) element;
530 selectField.setSelectedAttribute(fieldValue, true);
531 }
532 else if (element instanceof HtmlCheckBoxInput) {
533 HtmlCheckBoxInput checkboxField = (HtmlCheckBoxInput) element;
534 if (fieldValue.equals("on")) {
535 checkboxField.setChecked(true);
536 }
537 else if (fieldValue.equals("off")) {
538 checkboxField.setChecked(false);
539 }
540 else {
541 assertTrue("Invalid checkbox value", false);
542 }
543 }
544 else if (element instanceof HtmlFileInput) {
545 HtmlFileInput fileInputField = (HtmlFileInput) element;
546 fileInputField.setValueAttribute(fieldValue);
547 }
548 else if (element instanceof HtmlRadioButtonInput) {
549 List<HtmlElement> elements = getAllElementsByName(page, fieldId, false);
550 for (HtmlElement child : elements) {
551 assertTrue(child instanceof HtmlRadioButtonInput);
552 HtmlRadioButtonInput radioBtn = (HtmlRadioButtonInput) child;
553 if (radioBtn.getValueAttribute().equals(fieldValue)) {
554 radioBtn.setChecked(true);
555 break;
556 }
557 }
558 }
559 else {
560 assertTrue("Unknown control field", false);
561 }
562 }
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579 protected final String getFieldValue(HtmlPage page, String fieldId) {
580 String fieldValue = null;
581
582 HtmlElement element = getElement(page, fieldId);
583 assertTrue(fieldId + " not found", element != null);
584
585 if (element instanceof HtmlTextInput) {
586 HtmlTextInput textField = (HtmlTextInput) element;
587 fieldValue = textField.getValueAttribute();
588 }
589 else if (element instanceof HtmlTextArea) {
590 HtmlTextArea textAreaField = (HtmlTextArea) element;
591 fieldValue = textAreaField.getText();
592 }
593 else if (element instanceof HtmlHiddenInput) {
594 HtmlHiddenInput hiddenField = (HtmlHiddenInput) element;
595 fieldValue = hiddenField.getValueAttribute();
596 }
597 else if (element instanceof HtmlSelect) {
598 HtmlSelect selectField = (HtmlSelect) element;
599 fieldValue = ((HtmlOption) selectField.getSelectedOptions().get(0)).getValueAttribute();
600 }
601 else if (element instanceof HtmlCheckBoxInput) {
602 HtmlCheckBoxInput checkboxField = (HtmlCheckBoxInput) element;
603 fieldValue = checkboxField.isChecked() ? "on" : "off";
604 }
605 else if (element instanceof HtmlRadioButtonInput) {
606 List<HtmlElement> elements = getAllElementsByName(page, fieldId, false);
607 for (HtmlElement child : elements) {
608 assertTrue(child instanceof HtmlRadioButtonInput);
609 HtmlRadioButtonInput radioBtn = (HtmlRadioButtonInput) child;
610 if (radioBtn.isChecked()) {
611 fieldValue = radioBtn.getValueAttribute();
612 break;
613 }
614 }
615 }
616 else {
617 assertTrue("Unknown control field", false);
618 }
619
620 return fieldValue;
621 }
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638 protected final String getDefaultFieldValue(HtmlPage page, String fieldId) {
639 String fieldValue = null;
640
641 HtmlElement element = getElement(page, fieldId);
642 assertTrue(element != null);
643
644 if (element instanceof HtmlTextInput) {
645 HtmlTextInput textField = (HtmlTextInput) element;
646 fieldValue = textField.getDefaultValue();
647 }
648 else if (element instanceof HtmlTextArea) {
649 HtmlTextArea textAreaField = (HtmlTextArea) element;
650 fieldValue = textAreaField.getDefaultValue();
651 }
652 else if (element instanceof HtmlHiddenInput) {
653 HtmlHiddenInput hiddenField = (HtmlHiddenInput) element;
654 fieldValue = hiddenField.getDefaultValue();
655 }
656 else if (element instanceof HtmlSelect) {
657 HtmlSelect selectField = (HtmlSelect) element;
658 fieldValue = selectField.getDefaultValue();
659 }
660 else if (element instanceof HtmlCheckBoxInput) {
661 HtmlCheckBoxInput checkboxField = (HtmlCheckBoxInput) element;
662 fieldValue = checkboxField.isDefaultChecked() ? "on" : "off";
663 }
664 else if (element instanceof HtmlRadioButtonInput) {
665 List<HtmlElement> elements = getAllElementsByName(page, fieldId, false);
666 for (HtmlElement child : elements) {
667 assertTrue(child instanceof HtmlRadioButtonInput);
668 HtmlRadioButtonInput radioBtn = (HtmlRadioButtonInput) child;
669 if (radioBtn.isDefaultChecked()) {
670 fieldValue = radioBtn.getValueAttribute();
671 break;
672 }
673 }
674 }
675 else {
676 assertTrue("Unknown control field", false);
677 }
678
679 return fieldValue;
680 }
681
682
683
684
685
686
687
688
689
690
691
692
693 protected final void checkHelpLinks(HtmlPage page) throws IOException {
694 List<HtmlAnchor> anchors = findHelpLinks(page);
695 for (HtmlAnchor anchor : anchors) {
696 HtmlPage helpPage = (HtmlPage) anchor.click();
697 assertTrue(HELP_PAGE_TITLE.equals(helpPage.getTitleText()));
698 }
699 }
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732 protected final HtmlPage checkExpandedTextArea(HtmlPage page, String id, String text1, String text2) throws IOException {
733 boolean javascriptEnabled = webClient.isJavaScriptEnabled();
734
735 webClient.setJavaScriptEnabled(false);
736
737 setFieldValue(page, id, text1);
738
739 HtmlElement field = getElement(page, id);
740 assertTrue(field != null);
741
742 ClickableElement btn = (ClickableElement) this.getNextSibling(field);
743 assertTrue(btn != null);
744
745 HtmlPage textPage = clickOn(btn);
746
747 assertEquals(getFieldValue(textPage, id), text1);
748
749 setFieldValue(textPage, id, text2);
750 HtmlPage returnPage = clickOn(textPage, "save");
751
752 assertEquals(getFieldValue(returnPage, id), text2);
753
754 webClient.setJavaScriptEnabled(javascriptEnabled);
755
756 return returnPage;
757 }
758
759
760
761
762
763
764
765
766
767
768
769 protected final List<String> getErrors(HtmlPage page, String panelId) {
770 List<String> errors = new ArrayList<String>();
771
772 HtmlElement panelDiv = getElement(page, panelId);
773 assertTrue(panelDiv != null);
774
775 HtmlElement errorDiv = getElementByClass(panelDiv, "error");
776 if (errorDiv != null) {
777 Iterator iterator = errorDiv.getAllHtmlChildElements();
778 while (iterator.hasNext()) {
779 HtmlElement child = (HtmlElement) iterator.next();
780 if (child instanceof HtmlListItem) {
781 HtmlListItem li = (HtmlListItem) child;
782 errors.add(li.asText());
783 }
784 }
785 }
786
787 return errors;
788 }
789
790
791
792
793
794
795
796
797 protected final boolean containsError(List<String> errors, String text) {
798 for (String error : errors) {
799 if (error.contains(text)) {
800 return true;
801 }
802 }
803 return false;
804 }
805
806
807
808
809
810
811
812
813
814
815 private HtmlPage checkForLoginPage(HtmlPage page) throws IOException {
816 if (page.getTitleText().equals("Login")) {
817 HtmlForm form = (HtmlForm) page.getForms().get(0);
818 setFieldValue(page, "__login_user", USER_NETWORK_ID);
819 HtmlSubmitInput loginBtn = (HtmlSubmitInput) form.getInputByValue("Login");
820 page = (HtmlPage) loginBtn.click();
821 if (page.getTitleText().equals("Login")) {
822 page = (HtmlPage) loginBtn.click();
823 }
824 }
825 return page;
826 }
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846 protected final HtmlElement getElement(HtmlPage page, String name, String value, String title) {
847 HtmlElement element = getElement(page.getDocumentElement(), name, value, title);
848
849 if (element == null) {
850 List<HtmlPage> innerPages = getInnerPages(page);
851 for (HtmlPage innerPage : innerPages) {
852 element = getElement(innerPage, name, value, title);
853 if (element != null) break;
854 }
855 }
856
857 return element;
858 }
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874 private HtmlElement getElement(HtmlElement element, String name, String value, String title) {
875 Iterator iterator = element.getAllHtmlChildElements();
876 while (iterator.hasNext()) {
877 HtmlElement child = (HtmlElement) iterator.next();
878 String nameValue = child.getAttributeValue("name");
879 String valueValue = child.getAttributeValue("value");
880 String titleValue = child.getAttributeValue("title");
881 if ((name == null || name.equals(nameValue)) &&
882 (value == null || value.equals(valueValue)) &&
883 (title == null || title.equals(titleValue))) {
884 return child;
885 }
886 }
887 return null;
888 }
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904 protected final HtmlElement getElement(HtmlPage page, String id) {
905 HtmlElement element = getElementById(page, id);
906 if (element == null) {
907 element = getElementByName(page, id);
908 if (element == null) {
909 element = getElementByTitle(page, id);
910 }
911 }
912 return element;
913 }
914
915
916
917
918
919
920
921
922
923
924
925
926
927 protected final HtmlElement getElementById(HtmlPage page, String id) {
928 HtmlElement element = null;
929 try {
930 element = page.getHtmlElementById(id);
931 } catch (ElementNotFoundException ex) {
932 List<HtmlPage> innerPages = getInnerPages(page);
933 for (HtmlPage innerPage : innerPages) {
934 element = getElementById(innerPage, id);
935 if (element != null) break;
936 }
937 }
938 return element;
939 }
940
941
942
943
944
945
946
947
948
949
950
951
952
953 protected final HtmlElement getElementByName(HtmlPage page, String name) {
954 return getElementByName(page, name, false);
955 }
956
957
958
959
960
961
962
963
964
965 protected final HtmlElement getElementByName(HtmlElement element, String name) {
966 return getElementByName(element, name, false);
967 }
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982 protected final HtmlElement getElementByName(HtmlPage page, String name, boolean startsWith) {
983 HtmlElement element = getElementByName(page.getDocumentElement(), name, startsWith);
984
985 if (element == null) {
986 List<HtmlPage> innerPages = getInnerPages(page);
987 for (HtmlPage innerPage : innerPages) {
988 element = getElementByName(innerPage, name, startsWith);
989 if (element != null) break;
990 }
991 }
992
993 return element;
994 }
995
996
997
998
999
1000
1001
1002
1003
1004
1005 protected final HtmlElement getElementByName(HtmlElement element, String name, boolean startsWith) {
1006 Iterator iterator = element.getAllHtmlChildElements();
1007 while (iterator.hasNext()) {
1008 HtmlElement e = (HtmlElement) iterator.next();
1009 String value = e.getAttributeValue("name");
1010 if (!startsWith && name.equals(value)) {
1011 return e;
1012 } else if (startsWith && value != null && value.startsWith(name)) {
1013 return e;
1014 }
1015 }
1016 return null;
1017 }
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 protected final HtmlElement getElementByTitle(HtmlPage page, String title) {
1032 HtmlElement element = getElementByTitle(page.getDocumentElement(), title);
1033
1034 if (element == null) {
1035 List<HtmlPage> innerPages = getInnerPages(page);
1036 for (HtmlPage innerPage : innerPages) {
1037 element = getElementByTitle(innerPage, title);
1038 if (element != null) break;
1039 }
1040 }
1041 return element;
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 protected final HtmlElement getElementByTitle(HtmlElement element, String title) {
1053 Iterator iterator = element.getAllHtmlChildElements();
1054 while (iterator.hasNext()) {
1055 HtmlElement e = (HtmlElement) iterator.next();
1056 String value = e.getAttributeValue("title");
1057 if (title.equals(value)) {
1058 return e;
1059 }
1060 }
1061 return null;
1062 }
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072 protected final HtmlElement getElementByClass(HtmlElement element, String classname) {
1073 Iterator iterator = element.getAllHtmlChildElements();
1074 while (iterator.hasNext()) {
1075 HtmlElement e = (HtmlElement) iterator.next();
1076 String value = e.getAttributeValue("class");
1077 if (classname.equals(value)) {
1078 return e;
1079 }
1080 }
1081 return null;
1082 }
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100 protected final HtmlImageInput getLookup(HtmlPage page, String tag) {
1101 HtmlImageInput element = getLookup(page.getDocumentElement(), tag);
1102
1103 List<HtmlPage> innerPages = getInnerPages(page);
1104 for (Iterator<HtmlPage> page_it = innerPages.iterator(); page_it.hasNext() && element == null;) {
1105 HtmlPage innerPage = page_it.next();
1106 element = getLookup(innerPage, tag);
1107 }
1108
1109 return element;
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124 private final HtmlImageInput getLookup(HtmlElement element, String tag) {
1125 for (Iterator<HtmlElement> iterator = element.getAllHtmlChildElements(); iterator.hasNext();) {
1126 HtmlElement e = iterator.next();
1127 if (e instanceof HtmlImageInput) {
1128 String name = e.getAttributeValue("name");
1129 LOG.info("Found name attribute " + name);
1130 if (name != null && name.startsWith("methodToCall.performLookup") && name.contains(tag)) {
1131 return (HtmlImageInput) e;
1132 }
1133 }
1134 }
1135 return null;
1136 }
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148 protected final HtmlTable getTable(HtmlPage page, String id) {
1149 HtmlTable table = null;
1150 HtmlElement element = getElement(page, id);
1151 assertTrue(element != null);
1152
1153 if (element instanceof HtmlTable) {
1154 table = (HtmlTable) element;
1155 }
1156 else {
1157 Iterator iterator = element.getAllHtmlChildElements();
1158 while (iterator.hasNext()) {
1159 HtmlElement e = (HtmlElement) iterator.next();
1160 if (e instanceof HtmlTable) {
1161 table = (HtmlTable) e;
1162 break;
1163 }
1164 }
1165 }
1166 return table;
1167 }
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177 protected final HtmlAnchor getAnchor(HtmlPage page, String tag) {
1178 HtmlAnchor element = getAnchor(page.getDocumentElement(), tag);
1179
1180 if (element == null) {
1181 List<HtmlPage> innerPages = getInnerPages(page);
1182 for (HtmlPage innerPage : innerPages) {
1183 element = getAnchor(innerPage, tag);
1184 if (element != null) break;
1185 }
1186 }
1187
1188 return element;
1189 }
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199 private HtmlAnchor getAnchor(HtmlElement element, String tag) {
1200 Iterator iterator = element.getAllHtmlChildElements();
1201 while (iterator.hasNext()) {
1202 HtmlElement e = (HtmlElement) iterator.next();
1203 if (e instanceof HtmlAnchor) {
1204 String href = e.getAttributeValue("href");
1205 if (href != null && href.contains(tag)) {
1206 return (HtmlAnchor) e;
1207 }
1208 }
1209 }
1210 return null;
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223 protected final List<HtmlAnchor> findHelpLinks(HtmlPage page) {
1224 List <HtmlAnchor> helpLinks = findHelpLinks(page.getDocumentElement());
1225
1226 List<HtmlPage> innerPages = getInnerPages(page);
1227 for (HtmlPage innerPage : innerPages) {
1228 helpLinks.addAll(findHelpLinks(innerPage));
1229 }
1230
1231 return helpLinks;
1232 }
1233
1234
1235
1236
1237
1238
1239
1240
1241 private List<HtmlAnchor> findHelpLinks(HtmlElement element) {
1242 List<HtmlAnchor> helpLinks = new ArrayList<HtmlAnchor>();
1243 Iterator iterator = element.getAllHtmlChildElements();
1244 while (iterator.hasNext()) {
1245 HtmlElement e = (HtmlElement) iterator.next();
1246 if (e instanceof HtmlAnchor) {
1247 String target = e.getAttributeValue("target");
1248 if (target != null && target.equals("helpWindow")) {
1249 helpLinks.add((HtmlAnchor) e);
1250 }
1251 }
1252 }
1253 return helpLinks;
1254 }
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269 protected final List<HtmlElement> getAllElementsByName(HtmlPage page, String name, boolean startsWith) {
1270 List<HtmlElement> elements = getAllElementsByName(page.getDocumentElement(), name, startsWith);
1271
1272 List<HtmlPage> innerPages = getInnerPages(page);
1273 for (HtmlPage innerPage : innerPages) {
1274 elements.addAll(getAllElementsByName(innerPage, name, startsWith));
1275 }
1276
1277 return elements;
1278 }
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289 protected final List<HtmlElement> getAllElementsByName(HtmlElement element, String name, boolean startsWith) {
1290 List<HtmlElement> elements = new ArrayList<HtmlElement>();
1291 Iterator iterator = element.getAllHtmlChildElements();
1292 while (iterator.hasNext()) {
1293 HtmlElement e = (HtmlElement) iterator.next();
1294 String value = e.getAttributeValue("name");
1295 if (!startsWith && name.equals(value)) {
1296 elements.add(e);
1297 } else if (startsWith && value != null && value.startsWith(name)) {
1298 elements.add(e);
1299 }
1300 }
1301 return elements;
1302 }
1303
1304
1305
1306
1307
1308
1309
1310
1311 protected final List<HtmlPage> getInnerPages(HtmlPage page) {
1312 List<HtmlPage> innerPages = new ArrayList<HtmlPage>();
1313
1314 List frames = page.getFrames();
1315 for (int i = 0; i < frames.size(); i++) {
1316 FrameWindow frame = (FrameWindow) frames.get(i);
1317 BaseFrame baseFrame = frame.getFrameElement();
1318 if (baseFrame instanceof HtmlInlineFrame) {
1319 HtmlInlineFrame iframe = (HtmlInlineFrame) baseFrame;
1320 Page epage = iframe.getEnclosedPage();
1321 if (epage instanceof HtmlPage) {
1322 innerPages.add((HtmlPage) epage);
1323 }
1324 }
1325 }
1326 return innerPages;
1327 }
1328
1329
1330
1331
1332
1333
1334
1335 protected final HtmlElement getFirstChild(HtmlElement element) {
1336 HtmlElement firstChild = null;
1337 Iterator iterator = element.getChildElementsIterator();
1338 if (iterator.hasNext()) {
1339 firstChild = (HtmlElement) iterator.next();
1340 }
1341 return firstChild;
1342 }
1343
1344
1345
1346
1347
1348
1349
1350 protected final HtmlElement getNextSibling(HtmlElement element) {
1351 HtmlElement sibling = null;
1352 DomNode node = element.getParentNode();
1353 if (node instanceof HtmlElement) {
1354 HtmlElement parent = (HtmlElement) node;
1355 Iterator iterator = parent.getChildElementsIterator();
1356 while (iterator.hasNext()) {
1357 HtmlElement e = (HtmlElement) iterator.next();
1358 if (e == element) {
1359 if (iterator.hasNext()) {
1360 sibling = (HtmlElement) iterator.next();
1361 }
1362 break;
1363 }
1364 }
1365 }
1366 return sibling;
1367 }
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379 protected final HtmlPage docSearch(String docNbr) throws IOException {
1380 HtmlPage docPage = null;
1381 HtmlPage portalPage = getPortalPage();
1382 HtmlPage docSearchPage = clickOn(portalPage, "Document Search");
1383
1384 setFieldValue(docSearchPage, "criteria.routeHeaderId", docNbr);
1385 docSearchPage = clickOn(docSearchPage, "methodToCall.doDocSearch");
1386 if (docSearchPage.asText().contains("Nothing found to display.")) {
1387 docPage = null;
1388 } else {
1389 HtmlAnchor hyperlink = getAnchor(docSearchPage, docNbr);
1390 assertNotNull(hyperlink);
1391 docPage = clickOn(hyperlink);
1392 }
1393 return docPage;
1394 }
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404 protected final String getDocNbr(HtmlPage page) {
1405 HtmlTable table = getTable(page, "headerarea");
1406 HtmlTableRow row = table.getRow(0);
1407 HtmlTableCell cell = row.getCell(1);
1408 return cell.asText().trim();
1409 }
1410
1411
1412
1413
1414
1415
1416
1417
1418 protected final HtmlPage saveDoc(HtmlPage page) throws IOException {
1419 return clickOn(page, "save");
1420 }
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430 protected final HtmlPage closeDoc(HtmlPage page) throws IOException {
1431 HtmlPage nextPage = clickOn(page, "close");
1432 if (nextPage.asText().contains("Would you like to save this document before you close it")) {
1433 nextPage = clickOn(nextPage, "methodToCall.processAnswer.button1");
1434 }
1435 return nextPage;
1436 }
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449 protected final String saveAndCloseDoc(HtmlPage page) throws IOException {
1450 String docNbr = getDocNbr(page);
1451 HtmlPage savedPage = saveDoc(page);
1452 closeDoc(savedPage);
1453 return docNbr;
1454 }
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466 protected final HtmlPage saveAndSearchDoc(HtmlPage docPage) throws Exception {
1467 String docNbr = saveAndCloseDoc(docPage);
1468 docPage = docSearch(docNbr);
1469 assertNotNull(docPage);
1470 return docPage;
1471 }
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482 protected final List<String> getSelectOptions(HtmlPage page, String id) {
1483 List<String> options = new ArrayList<String>();
1484
1485 HtmlElement element = getElement(page, id);
1486 assertNotNull(element);
1487 assertTrue(element instanceof HtmlSelect);
1488
1489 HtmlSelect select = (HtmlSelect) element;
1490 for (int i = 0; i < select.getOptionSize(); i++) {
1491 HtmlOption option = select.getOption(i);
1492 options.add(option.asText().trim());
1493 }
1494
1495 return options;
1496 }
1497
1498
1499
1500
1501
1502
1503
1504
1505 public void testHelpLinks() throws Exception {
1506 checkHelpLinks(getDefaultDocumentPage());
1507 }
1508
1509
1510
1511
1512
1513
1514
1515 protected HtmlPage getDefaultDocumentPage() throws Exception {
1516 return null;
1517 }
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527 protected void selectAnyOption(HtmlPage page, String id) {
1528 HtmlElement element = getElement(page, id);
1529 assertTrue(element instanceof HtmlSelect);
1530
1531 HtmlSelect selectField = (HtmlSelect) element;
1532 List<HtmlOption> options = selectField.getOptions();
1533 for (HtmlOption option : options) {
1534 String value = option.getValueAttribute();
1535 if (!value.equals("")) {
1536 selectField.setSelectedAttribute(value, true);
1537 break;
1538 }
1539 }
1540 }
1541 }