1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.junit;
20
21 import static org.hamcrest.CoreMatchers.equalTo;
22 import static org.hamcrest.CoreMatchers.not;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import java.awt.Image;
31 import java.awt.image.BufferedImage;
32 import java.io.BufferedOutputStream;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.io.PrintStream;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41 import java.text.MessageFormat;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Locale;
45 import java.util.Map;
46 import java.util.ResourceBundle;
47
48 import javax.imageio.ImageIO;
49 import javax.servlet.http.Cookie;
50
51 import net.sourceforge.jwebunit.api.HttpHeader;
52 import net.sourceforge.jwebunit.api.IElement;
53 import net.sourceforge.jwebunit.api.ITestingEngine;
54 import net.sourceforge.jwebunit.exception.ExpectedJavascriptAlertException;
55 import net.sourceforge.jwebunit.exception.ExpectedJavascriptConfirmException;
56 import net.sourceforge.jwebunit.exception.ExpectedJavascriptPromptException;
57 import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
58 import net.sourceforge.jwebunit.exception.UnableToSetFormException;
59 import net.sourceforge.jwebunit.html.Table;
60 import net.sourceforge.jwebunit.javascript.JavascriptAlert;
61 import net.sourceforge.jwebunit.javascript.JavascriptConfirm;
62 import net.sourceforge.jwebunit.javascript.JavascriptPrompt;
63 import net.sourceforge.jwebunit.util.TestContext;
64 import net.sourceforge.jwebunit.util.TestingEngineRegistry;
65
66 import org.apache.regexp.RE;
67 import org.apache.regexp.RESyntaxException;
68
69
70
71
72
73
74
75
76
77
78 public class WebTester {
79 private ITestingEngine testingEngine = null;
80
81 private TestContext testContext = null;
82
83
84
85
86 private String testingEngineKey = null;
87
88
89
90
91
92
93
94
95
96
97 public ITestingEngine getDialog() {
98 return getTestingEngine();
99 }
100
101
102
103
104
105
106 public void setBaseUrl(String url) {
107 getTestContext().setBaseUrl(url);
108 }
109
110
111
112
113
114
115 public void setBaseUrl(URL url) {
116 getTestContext().setBaseUrl(url);
117 }
118
119
120
121
122
123
124 public ITestingEngine getTestingEngine() {
125 if (testingEngine == null) {
126
127 testingEngine = initializeDialog();
128 }
129 return testingEngine;
130 }
131
132
133
134
135
136 protected ITestingEngine initializeDialog() {
137 ITestingEngine theIJWebUnitDialog = null;
138 String theTestingEngineKey = getTestingEngineKey();
139 Class<?> theClass;
140 try {
141 theClass = TestingEngineRegistry
142 .getTestingEngineClass(theTestingEngineKey);
143 } catch (ClassNotFoundException e1) {
144 throw new RuntimeException(e1);
145 }
146 try {
147 theIJWebUnitDialog = (ITestingEngine) theClass.newInstance();
148 } catch (InstantiationException e) {
149 e.printStackTrace();
150 throw new RuntimeException(
151 "Can't Instantiate Testing Engine with class [" + theClass
152 + "] with key [" + theTestingEngineKey + "].", e);
153 } catch (IllegalAccessException e) {
154 e.printStackTrace();
155 throw new RuntimeException("IllegalAccessException using class ["
156 + theClass + "] with key [" + theTestingEngineKey + "].", e);
157 }
158
159 return theIJWebUnitDialog;
160 }
161
162
163
164
165 public void closeBrowser() {
166 try {
167 getTestingEngine().closeBrowser();
168 } catch (ExpectedJavascriptAlertException e) {
169 fail("You previously tell that alert with message ["
170 + e.getAlertMessage()
171 + "] was expected, but nothing appeared.");
172 } catch (ExpectedJavascriptConfirmException e) {
173 fail("You previously tell that confirm with message ["
174 + e.getConfirmMessage()
175 + "] was expected, but nothing appeared.");
176 } catch (ExpectedJavascriptPromptException e) {
177 fail("You previously tell that prompt with message ["
178 + e.getPromptMessage()
179 + "] was expected, but nothing appeared.");
180 }
181 }
182
183
184
185
186 public void closeWindow() {
187 getTestingEngine().closeWindow();
188 }
189
190
191
192
193
194
195 public void setDialog(ITestingEngine aIJWebUnitDialog) {
196 testingEngine = aIJWebUnitDialog;
197 }
198
199
200
201
202
203
204 public TestContext getTestContext() {
205 if (testContext == null) {
206
207 testContext = new TestContext();
208 }
209 return testContext;
210 }
211
212
213
214
215
216
217
218
219
220 public void setTestContext(TestContext aTestContext) {
221 testContext = aTestContext;
222 }
223
224
225
226
227
228
229
230
231
232 public void beginAt(String aRelativeURL) throws TestingEngineResponseException {
233 try {
234 getTestingEngine().beginAt(createUrl(aRelativeURL, getTestContext().getBaseUrl()), testContext);
235 } catch (MalformedURLException e) {
236 fail(e.getLocalizedMessage());
237 }
238
239 }
240
241
242
243
244
245
246
247
248
249 @Deprecated
250 private URL createUrl(String url, URL baseURL) throws MalformedURLException {
251 if (url.startsWith("http://") || url.startsWith("https://")
252 || url.startsWith("file://")) {
253 return new URL(url);
254 } else if (url.startsWith("www.")) {
255 return new URL("http://" + url);
256 } else {
257 url = url.startsWith("/") ? url.substring(1) : url;
258 return new URL(baseURL, url);
259 }
260 }
261
262
263
264
265
266
267
268
269 private URL createUrlFixed(String url, URL baseURL) throws MalformedURLException {
270 if (url.startsWith("http://") || url.startsWith("https://") //Absolute URL
271 || url.startsWith("file://")) {
272 return new URL(url);
273 } else if (url.startsWith("www.")) {
274 return new URL("http://" + url);
275 } else {
276 return new URL(baseURL, url);
277 }
278 }
279
280
281
282
283
284
285
286
287 public String getMessage(String key) {
288 String message = "";
289 Locale locale = testContext.getLocale();
290 try {
291 message = ResourceBundle.getBundle(
292 getTestContext().getResourceBundleName(), locale)
293 .getString(key);
294 } catch (Exception e) {
295 e.printStackTrace();
296 throw new RuntimeException("No message found for key [" + key
297 + "].", e);
298 }
299 return message;
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313 public String getMessage(String key, Object[] args) {
314 String message = getMessage(key);
315
316
317
318 MessageFormat format = new MessageFormat(message, getTestContext().getLocale());
319
320 return format.format(args);
321 }
322
323
324
325
326
327
328
329
330 public void assertResponseCode(int status) {
331 assertEquals( status, getTestingEngine().getServerResponseCode() );
332 }
333
334
335
336
337
338
339
340
341 public void assertResponseCodeBetween(int lower, int higher) {
342 assertTrue( getTestingEngine().getServerResponseCode() >= lower && getTestingEngine().getServerResponseCode() <= higher );
343 }
344
345
346
347
348
349
350
351 public void setIgnoreFailingStatusCodes(boolean ignore) {
352 getTestingEngine().setIgnoreFailingStatusCodes(ignore);
353 }
354
355
356
357
358
359
360 public void assertHeaderPresent(String name) {
361 assertFalse( "header '" + name + "' not present", getTestingEngine().getHeader(name) == null );
362 }
363
364
365
366
367
368
369 public void assertHeaderNotPresent(String name) {
370 assertTrue( "header '" + name + "' present", getTestingEngine().getHeader(name) == null );
371 }
372
373
374
375
376
377
378
379 public void assertHeaderEquals(String name, String value) {
380 assertEquals( value, getTestingEngine().getHeader(name) );
381 }
382
383
384
385
386
387
388
389 public void assertHeaderMatches(String name, String regexp) {
390 assertMatch("Unable to match [" + regexp + "] in header [" + name + "]", regexp, getTestingEngine().getHeader(name));
391 }
392
393
394
395
396
397
398
399 public String getHeader(String name) {
400 return getTestingEngine().getHeader(name);
401 }
402
403
404
405
406
407
408
409 @Deprecated
410 public Map<String, String> getAllHeaders() {
411 return getTestingEngine().getAllHeaders();
412 }
413
414
415
416
417
418
419 public List<HttpHeader> getResponseHeaders() {
420 return getTestingEngine().getResponseHeaders();
421 }
422
423
424
425
426
427
428
429
430 public void assertTitleEquals(String title) {
431 assertEquals(title, getTestingEngine().getPageTitle());
432 }
433
434
435
436
437
438
439
440
441
442 @Deprecated
443 public void assertTitleNotSame(String title) {
444 assertTitleNotEquals(title);
445 }
446
447
448
449
450
451
452
453
454 public void assertTitleNotEquals(String title) {
455 assertThat(title, not(equalTo(getTestingEngine().getPageTitle())));
456 }
457
458
459
460
461
462
463 public void assertTitleMatch(String regexp) {
464 assertMatch("Unable to match [" + regexp + "] in title", regexp, getTestingEngine().getPageTitle());
465 }
466
467
468
469
470
471
472
473
474 public void assertTitleEqualsKey(String titleKey) {
475 assertEquals(getMessage(titleKey), getTestingEngine().getPageTitle());
476 }
477
478
479
480
481
482
483
484 public void assertTitleEqualsKey(String titleKey, Object[] args) {
485 assertEquals(getMessage(titleKey, args), getTestingEngine().getPageTitle());
486 }
487
488
489
490
491
492
493
494 public void assertKeyPresent(String key) {
495 assertTextPresent(getMessage(key));
496 }
497
498
499
500
501
502
503
504 public void assertKeyPresent(String key, Object[] args) {
505 assertTextPresent(getMessage(key, args));
506 }
507
508
509
510
511
512
513 public void assertTextPresent(String text) {
514 if (!(getTestingEngine().getPageText().contains(text)))
515 fail("Expected text not found in current page: [" + text
516 + "]\n Page content was: ["
517 + getTestingEngine().getPageText() + "]");
518 }
519
520
521
522
523
524
525 public void assertMatch(String regexp) {
526 RE re = getRE(regexp);
527 if (!re.match(getTestingEngine().getPageText()))
528 fail("Expected rexexp not matched in response: [" + regexp
529 + "]");
530 }
531
532
533
534
535
536
537
538 public void assertMatch(String regexp, String text) {
539 assertMatch("Expected rexexp '" + regexp + "' not matched in text '" + text + "'", regexp, text);
540 }
541
542
543
544
545
546
547
548 public void assertNotMatch(String regexp, String text) {
549 assertNotMatch("Expected rexexp '" + regexp + "' matched in text '" + text + "'", regexp, text);
550 }
551
552
553
554
555
556
557
558
559 public void assertMatch(String message, String regexp, String text) {
560 RE re = getRE(regexp);
561 if (!re.match(text))
562 fail(message);
563 }
564
565
566
567
568
569
570
571 public void assertNotMatch(String message, String regexp, String text) {
572 RE re = getRE(regexp);
573 if (re.match(text))
574 fail(message);
575 }
576
577
578
579
580
581
582 public void assertKeyNotPresent(String key) {
583 assertTextNotPresent(getMessage(key));
584 }
585
586
587
588
589
590
591
592 public void assertKeyNotPresent(String key, Object[] args) {
593 assertTextNotPresent(getMessage(key, args));
594 }
595
596
597
598
599
600
601 public void assertTextNotPresent(String text) {
602 if (getTestingEngine().getPageText().contains(text))
603 fail("Text found in response when not expected: [" + text
604 + "]");
605 }
606
607
608
609
610
611
612 public void assertNoMatch(String regexp) {
613 assertNotMatch("Regexp matched in response when not expected: [" + regexp + "]",
614 regexp,
615 getTestingEngine().getPageText());
616 }
617
618
619
620
621
622
623 public Table getTable(String tableSummaryNameOrId) {
624 return getTestingEngine().getTable(tableSummaryNameOrId);
625 }
626
627
628
629
630
631
632 public void assertTablePresent(String tableSummaryNameOrId) {
633 if (!getTestingEngine().hasTable(tableSummaryNameOrId))
634 fail("Unable to locate table \"" + tableSummaryNameOrId
635 + "\"");
636 }
637
638
639
640
641
642
643 public void assertTableNotPresent(String tableSummaryNameOrId) {
644 if (getTestingEngine().hasTable(tableSummaryNameOrId))
645 fail("Located table \"" + tableSummaryNameOrId + "\"");
646 }
647
648
649
650
651
652
653
654 public void assertKeyInTable(String tableSummaryOrId, String key) {
655 assertTextInTable(tableSummaryOrId, getMessage(key));
656 }
657
658
659
660
661
662
663
664
665
666
667 public void assertKeyInTable(String tableSummaryOrId, String key, Object[] args) {
668 assertTextInTable(tableSummaryOrId, getMessage(key, args));
669 }
670
671
672
673
674
675
676
677
678 public void assertTextInTable(String tableSummaryNameOrId, String text) {
679 assertTablePresent(tableSummaryNameOrId);
680 assertTrue("Could not find: [" + text + "]" + "in table ["
681 + tableSummaryNameOrId + "]", getTestingEngine().getTable(
682 tableSummaryNameOrId).hasText(text));
683 }
684
685
686
687
688
689
690
691 public void assertMatchInTable(String tableSummaryNameOrId, String regexp) {
692 assertTablePresent(tableSummaryNameOrId);
693 assertTrue("Could not match: [" + regexp + "]" + "in table ["
694 + tableSummaryNameOrId + "]", getTestingEngine().getTable(
695 tableSummaryNameOrId).hasMatch(regexp));
696 }
697
698
699
700
701
702
703
704 public void assertKeysInTable(String tableSummaryOrId, String[] keys) {
705 for (int i = 0; i < keys.length; i++) {
706 assertKeyInTable(tableSummaryOrId, keys[i]);
707 }
708 }
709
710
711
712
713
714
715
716
717
718
719 public void assertKeysInTable(String tableSummaryOrId, String[] keys, Object[][] args) {
720 for (int i = 0; i < keys.length; i++) {
721 assertKeyInTable(tableSummaryOrId, keys[i], args[i]);
722 }
723 }
724
725
726
727
728
729
730
731
732
733 public void assertTextInTable(String tableSummaryOrId, String[] text) {
734 for (int i = 0; i < text.length; i++) {
735 assertTextInTable(tableSummaryOrId, text[i]);
736 }
737 }
738
739
740
741
742
743
744
745 public void assertMatchInTable(String tableSummaryOrId, String[] regexp) {
746 for (int i = 0; i < regexp.length; i++) {
747 assertMatchInTable(tableSummaryOrId, regexp[i]);
748 }
749 }
750
751
752
753
754
755
756
757 public void assertKeyNotInTable(String tableSummaryOrId, String key) {
758 assertTextNotInTable(tableSummaryOrId, getMessage(key));
759 }
760
761
762
763
764
765
766
767 public void assertTextNotInTable(String tableSummaryNameOrId, String text) {
768 assertTablePresent(tableSummaryNameOrId);
769 assertTrue("Found text: [" + text + "] in table ["
770 + tableSummaryNameOrId + "]", !getTestingEngine().getTable(
771 tableSummaryNameOrId).hasText(text));
772 }
773
774
775
776
777
778
779
780 public void assertTextNotInTable(String tableSummaryNameOrId, String[] text) {
781 for (int i = 0; i < text.length; i++) {
782 assertTextNotInTable(tableSummaryNameOrId, text[i]);
783 }
784 }
785
786
787
788
789
790
791
792 public void assertNoMatchInTable(String tableSummaryNameOrId, String regexp) {
793 assertTablePresent(tableSummaryNameOrId);
794 assertTrue("Found regexp: [" + regexp + "] in table ["
795 + tableSummaryNameOrId + "]", !getTestingEngine().getTable(
796 tableSummaryNameOrId).hasMatch(regexp));
797 }
798
799
800
801
802
803
804
805 public void assertNoMatchInTable(String tableSummaryNameOrId,
806 String[] regexp) {
807 for (int i = 0; i < regexp.length; i++) {
808 assertNoMatchInTable(tableSummaryNameOrId, regexp[i]);
809 }
810 }
811
812
813
814
815
816
817
818 public void assertTableEquals(String tableSummaryNameOrId,
819 Table expectedTable) {
820 getTestingEngine().getTable(tableSummaryNameOrId).assertEquals(
821 expectedTable);
822 }
823
824
825
826
827
828
829
830 public void assertTableEquals(String tableSummaryNameOrId,
831 String[][] expectedCellValues) {
832 getTestingEngine().getTable(tableSummaryNameOrId).assertEquals(
833 new Table(expectedCellValues));
834 }
835
836
837
838
839
840
841
842
843 public void assertTableRowsEqual(String tableSummaryNameOrId, int startRow,
844 Table expectedTable) {
845 getTestingEngine().getTable(tableSummaryNameOrId).assertSubTableEquals(
846 startRow, expectedTable);
847 }
848
849
850
851
852
853
854
855
856 public void assertTableRowsEqual(String tableSummaryNameOrId, int startRow,
857 String[][] expectedTable) {
858 getTestingEngine().getTable(tableSummaryNameOrId).assertSubTableEquals(
859 startRow, new Table(expectedTable));
860 }
861
862
863
864
865
866
867
868 public void assertTableRowCountEquals(String tableSummaryNameOrId,
869 int expectedRowCount) {
870 assertTablePresent(tableSummaryNameOrId);
871 int actualRowCount = getTestingEngine().getTable(tableSummaryNameOrId)
872 .getRowCount();
873 assertTrue("Expected row count was " + expectedRowCount
874 + " but actual row count is " + actualRowCount,
875 actualRowCount == expectedRowCount);
876 }
877
878
879
880
881
882
883
884 public void assertTableMatch(String tableSummaryOrId, Table expectedTable) {
885 getTestingEngine().getTable(tableSummaryOrId)
886 .assertMatch(expectedTable);
887 }
888
889
890
891
892
893
894
895 public void assertTableMatch(String tableSummaryOrId,
896 String[][] expectedCellValues) {
897 getTestingEngine().getTable(tableSummaryOrId).assertMatch(
898 new Table(expectedCellValues));
899 }
900
901
902
903
904
905
906
907
908 public void assertTableRowsMatch(String tableSummaryOrId, int startRow,
909 Table expectedTable) {
910 getTestingEngine().getTable(tableSummaryOrId).assertSubTableMatch(
911 startRow, expectedTable);
912 }
913
914
915
916
917
918
919
920
921 public void assertTableRowsMatch(String tableSummaryOrId, int startRow,
922 String[][] expectedTable) {
923 getTestingEngine().getTable(tableSummaryOrId).assertSubTableMatch(
924 startRow, new Table(expectedTable));
925 }
926
927
928
929
930
931
932 public void assertFormElementPresent(String formElementName) {
933 assertTrue("Did not find form element with name ["
934 + formElementName + "].", getTestingEngine()
935 .hasFormParameterNamed(formElementName));
936 }
937
938
939
940
941
942
943 public void assertFormElementNotPresent(String formElementName) {
944 try {
945 assertTrue("Found form element with name ["
946 + formElementName + "] when not expected.", !getTestingEngine()
947 .hasFormParameterNamed(formElementName));
948 } catch (UnableToSetFormException e) {
949
950 }
951 }
952
953
954
955
956
957
958 public void assertCheckboxPresent(String checkboxName) {
959 assertTrue("Did not find form checkbox with name ["
960 + checkboxName + "].", getTestingEngine().hasElementByXPath(
961 "//input[lower-case(@type)='checkbox' and @name='" + checkboxName + "']"));
962 }
963
964
965
966
967
968
969
970 public void assertCheckboxPresent(String checkboxName, String checkboxValue) {
971 assertTrue("Did not find form checkbox with name ["
972 + checkboxName + "] and value [" + checkboxValue + "].",
973 getTestingEngine().hasElementByXPath(
974 "//input[lower-case(@type)='checkbox' and @name='" + checkboxName
975 + "' and @value='" + checkboxValue + "']"));
976 }
977
978
979
980
981
982
983 public void assertCheckboxNotPresent(String checkboxName) {
984 assertFalse("Found form checkbox with name [" + checkboxName
985 + "] when not expected.", getTestingEngine().hasElementByXPath(
986 "//input[lower-case(@type)='checkbox' and @name='" + checkboxName + "']"));
987 }
988
989
990
991
992
993
994
995 public void assertCheckboxNotPresent(String checkboxName,
996 String checkboxValue) {
997 assertFalse("Found form checkbox with name [" + checkboxName
998 + "] and value [" + checkboxValue + "] when not expected.",
999 getTestingEngine().hasElementByXPath(
1000 "//input[lower-case(@type)='checkbox' and @name='" + checkboxName
1001 + "' and @value='" + checkboxValue + "']"));
1002 }
1003
1004
1005
1006
1007
1008 public void assertFormPresent() {
1009 assertTrue("No form present", getTestingEngine().hasForm());
1010 }
1011
1012
1013
1014
1015
1016
1017 public void assertFormPresent(String nameOrID) {
1018 assertTrue("No form present with name or id [" + nameOrID + "]",
1019 getTestingEngine().hasForm(nameOrID));
1020 }
1021
1022
1023
1024
1025
1026
1027
1028 public void assertFormPresent(String nameOrID, int index) {
1029 assertTrue("No form present with name or id [" + nameOrID + "] at index " + index,
1030 getTestingEngine().hasForm(nameOrID, index));
1031 }
1032
1033
1034
1035
1036
1037 public void assertFormNotPresent() {
1038 assertFalse("A form is present", getTestingEngine().hasForm());
1039 }
1040
1041
1042
1043
1044
1045
1046 public void assertFormNotPresent(String nameOrID) {
1047 assertFalse("Form present with name or id [" + nameOrID + "]",
1048 getTestingEngine().hasForm(nameOrID));
1049 }
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059 public void assertFormElementEquals(String formElementName,
1060 String expectedValue) {
1061 assertFormElementPresent(formElementName);
1062 assertEquals(expectedValue, getTestingEngine()
1063 .getElementAttributByXPath(
1064 "//input[@name='" + formElementName + "']", "value"));
1065 }
1066
1067
1068
1069
1070
1071
1072
1073 public void assertFormElementMatch(String formElementName, String regexp) {
1074
1075 assertFormElementPresent(formElementName);
1076 RE re = null;
1077 try {
1078 re = new RE(regexp, RE.MATCH_SINGLELINE);
1079 } catch (RESyntaxException e) {
1080 fail(e.toString());
1081 }
1082 assertTrue("Unable to match [" + regexp + "] in form element \""
1083 + formElementName + "\"", re.match(getTestingEngine()
1084 .getElementAttributByXPath(
1085 "//input[@name='" + formElementName + "']", "value")));
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096 public void assertFormElementEmpty(String formElementName) {
1097 assertFormElementPresent(formElementName);
1098 assertEquals("", getTestingEngine().getElementAttributByXPath(
1099 "//input[@name='" + formElementName + "']", "value"));
1100 }
1101
1102
1103
1104
1105
1106
1107
1108
1109 public void assertTextFieldEquals(String formElementName,
1110 String expectedValue) {
1111 assertFormElementPresent(formElementName);
1112 assertEquals(expectedValue, getTestingEngine()
1113 .getTextFieldValue(formElementName));
1114 }
1115
1116
1117
1118
1119
1120
1121
1122
1123 public void assertHiddenFieldPresent(String formElementName,
1124 String expectedValue) {
1125 assertFormElementPresent(formElementName);
1126 assertEquals(expectedValue, getTestingEngine()
1127 .getHiddenFieldValue(formElementName));
1128 }
1129
1130
1131
1132
1133
1134
1135 public void assertCheckboxSelected(String checkBoxName) {
1136 assertCheckboxPresent(checkBoxName);
1137 if (!getTestingEngine().isCheckboxSelected(checkBoxName)) {
1138 fail("Checkbox with name [" + checkBoxName
1139 + "] was not found selected.");
1140 }
1141 }
1142
1143
1144
1145
1146
1147
1148
1149 public void assertCheckboxSelected(String checkBoxName, String checkBoxValue) {
1150 assertCheckboxPresent(checkBoxName, checkBoxValue);
1151 if (!getTestingEngine().isCheckboxSelected(checkBoxName, checkBoxValue)) {
1152 fail("Checkbox with name [" + checkBoxName + "] and value ["
1153 + checkBoxValue + "] was not found selected.");
1154 }
1155 }
1156
1157
1158
1159
1160
1161
1162 public void assertCheckboxNotSelected(String checkBoxName) {
1163 assertCheckboxPresent(checkBoxName);
1164 if (getTestingEngine().isCheckboxSelected(checkBoxName)) {
1165 fail("Checkbox with name [" + checkBoxName
1166 + "] was found selected.");
1167 }
1168 }
1169
1170
1171
1172
1173
1174
1175
1176 public void assertCheckboxNotSelected(String checkBoxName,
1177 String checkBoxValue) {
1178 assertCheckboxPresent(checkBoxName, checkBoxValue);
1179 if (getTestingEngine().isCheckboxSelected(checkBoxName, checkBoxValue)) {
1180 fail("Checkbox with name [" + checkBoxName + "] and value ["
1181 + checkBoxValue + "] was found selected.");
1182 }
1183 }
1184
1185
1186
1187
1188
1189
1190
1191 public void assertRadioOptionPresent(String name, String radioOption) {
1192 assertFormElementPresent(name);
1193 if (!getTestingEngine().hasRadioOption(name, radioOption)) {
1194 fail("Unable to find option [" + radioOption
1195 + "] in radio group [" + name + "]");
1196 }
1197 }
1198
1199
1200
1201
1202
1203
1204
1205 public void assertRadioOptionNotPresent(String name, String radioOption) {
1206 assertFormElementPresent(name);
1207 if (getTestingEngine().hasRadioOption(name, radioOption))
1208 fail("Found option [" + radioOption + "] in radio group ["
1209 + name + "]");
1210 }
1211
1212
1213
1214
1215
1216
1217
1218 public void assertRadioOptionSelected(String name, String radioOption) {
1219 assertRadioOptionPresent(name, radioOption);
1220 assertEquals(radioOption, getTestingEngine()
1221 .getSelectedRadio(name));
1222 }
1223
1224
1225
1226
1227
1228
1229
1230 public void assertRadioOptionNotSelected(String name, String radioOption) {
1231 assertRadioOptionPresent(name, radioOption);
1232 assertFalse("Radio option [" + radioOption + "] is selected.",
1233 radioOption.equals(getTestingEngine()
1234 .getSelectedRadio(name)));
1235 }
1236
1237
1238
1239
1240
1241
1242
1243 public void assertSelectOptionsPresent(String selectName,
1244 String[] optionLabels) {
1245 assertFormElementPresent(selectName);
1246 for (int i = 0; i < optionLabels.length; i++)
1247 assertTrue("Option [" + optionLabels[i]
1248 + "] not found in select element " + selectName,
1249 getTestingEngine().hasSelectOption(selectName,
1250 optionLabels[i]));
1251 }
1252
1253
1254
1255
1256
1257
1258
1259 public void assertSelectOptionPresent(String selectName, String optionLabel) {
1260 assertSelectOptionsPresent(selectName, new String[] { optionLabel });
1261 }
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271 public void assertSelectOptionsPresent(String selectName, int index,
1272 String[] optionLabels) {
1273 assertFormElementPresent(selectName);
1274 for (int i = 0; i < optionLabels.length; i++)
1275 assertTrue("Option [" + optionLabels[i]
1276 + "] not found in select element " + selectName,
1277 getTestingEngine().hasSelectOption(selectName, index,
1278 optionLabels[i]));
1279 }
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289 public void assertSelectOptionPresent(String selectName, int index, String optionLabel) {
1290 assertSelectOptionsPresent(selectName, index, new String[] { optionLabel });
1291 }
1292
1293
1294
1295
1296
1297
1298
1299
1300 public void assertSelectOptionValuesPresent(String selectName,
1301 String[] optionValues) {
1302 assertFormElementPresent(selectName);
1303 for (int i = 0; i < optionValues.length; i++)
1304 assertTrue("Option [" + optionValues[i]
1305 + "] not found in select element " + selectName,
1306 getTestingEngine().hasSelectOptionValue(selectName,
1307 optionValues[i]));
1308 }
1309
1310
1311
1312
1313
1314
1315
1316 public void assertSelectOptionValuePresent(String selectName,
1317 String optionValue) {
1318 assertSelectOptionValuesPresent(selectName,
1319 new String[] { optionValue });
1320 }
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330 public void assertSelectOptionValuesPresent(String selectName,
1331 int index,
1332 String[] optionValues) {
1333 assertFormElementPresent(selectName);
1334 for (int i = 0; i < optionValues.length; i++)
1335 assertTrue("Option [" + optionValues[i]
1336 + "] not found in select element " + selectName,
1337 getTestingEngine().hasSelectOptionValue(selectName,
1338 index,
1339 optionValues[i]));
1340 }
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350 public void assertSelectOptionValuePresent(String selectName,
1351 int index,
1352 String optionValue) {
1353 assertSelectOptionValuesPresent(selectName, index,
1354 new String[] { optionValue });
1355 }
1356
1357
1358
1359
1360
1361
1362
1363 public void assertSelectOptionValueNotPresent(String selectName,
1364 String optionValue) {
1365 try {
1366 assertSelectOptionValuePresent(selectName, optionValue);
1367 } catch (AssertionError e) {
1368 return;
1369 }
1370 fail("Option value" + optionValue + " found in select element "
1371 + selectName + " when not expected.");
1372 }
1373
1374
1375
1376
1377
1378
1379
1380 public void assertSelectOptionNotPresent(String selectName,
1381 String optionLabel) {
1382 try {
1383 assertSelectOptionPresent(selectName, optionLabel);
1384 } catch (AssertionError e) {
1385 return;
1386 }
1387 fail("Option " + optionLabel + " found in select element "
1388 + selectName + " when not expected.");
1389 }
1390
1391
1392
1393
1394
1395
1396
1397 public void assertSelectOptionValueNotPresent(String selectName,
1398 int index, String optionValue) {
1399 try {
1400 assertSelectOptionValuePresent(selectName, index, optionValue);
1401 } catch (AssertionError e) {
1402 return;
1403 }
1404 fail("Option value" + optionValue + " found in select element "
1405 + selectName + " when not expected.");
1406 }
1407
1408
1409
1410
1411
1412
1413
1414 public void assertSelectOptionNotPresent(String selectName,
1415 int index, String optionLabel) {
1416 try {
1417 assertSelectOptionPresent(selectName, index, optionLabel);
1418 } catch (AssertionError e) {
1419 return;
1420 }
1421 fail("Option " + optionLabel + " found in select element "
1422 + selectName + " when not expected.");
1423 }
1424
1425
1426
1427
1428
1429
1430
1431 public void assertSelectOptionsEqual(String selectName,
1432 String[] expectedOptions) {
1433 assertFormElementPresent(selectName);
1434 assertArraysEqual(expectedOptions, getOptionsFor(selectName));
1435 }
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446 public void assertSelectOptionsEqual(String selectName, int index,
1447 String[] expectedOptions) {
1448 assertFormElementPresent(selectName);
1449 assertArraysEqual(expectedOptions, getOptionsFor(selectName, index));
1450 }
1451
1452
1453
1454
1455
1456
1457
1458
1459 public void assertSelectOptionsNotEqual(String selectName,
1460 String[] expectedOptions) {
1461 assertFormElementPresent(selectName);
1462 try {
1463 assertSelectOptionsEqual(selectName, expectedOptions);
1464 } catch (AssertionError e) {
1465 return;
1466 }
1467 fail("Options not expected to be equal");
1468 }
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479 public void assertSelectOptionsNotEqual(String selectName, int index,
1480 String[] expectedOptions) {
1481 assertFormElementPresent(selectName);
1482 try {
1483 assertSelectOptionsEqual(selectName, index, expectedOptions);
1484 } catch (AssertionError e) {
1485 return;
1486 }
1487 fail("Options not expected to be equal");
1488 }
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500 public void assertSelectOptionValuesEqual(String selectName, int index,
1501 String[] expectedValues) {
1502 assertFormElementPresent(selectName);
1503 assertArraysEqual(expectedValues, getTestingEngine()
1504 .getSelectOptionValues(selectName, index));
1505
1506 }
1507
1508
1509
1510
1511
1512
1513
1514 public void assertSelectOptionValuesEqual(String selectName,
1515 String[] expectedValues) {
1516 assertFormElementPresent(selectName);
1517 assertArraysEqual(expectedValues, getTestingEngine()
1518 .getSelectOptionValues(selectName));
1519
1520 }
1521
1522
1523
1524
1525
1526
1527
1528
1529 public void assertSelectOptionValuesNotEqual(String selectName,
1530 String[] optionValues) {
1531 assertFormElementPresent(selectName);
1532 try {
1533 assertSelectOptionValuesEqual(selectName, optionValues);
1534 } catch (AssertionError e) {
1535 return;
1536 }
1537 fail("Values not expected to be equal");
1538 }
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549 public void assertSelectOptionValuesNotEqual(String selectName, int index,
1550 String[] optionValues) {
1551 assertFormElementPresent(selectName);
1552 try {
1553 assertSelectOptionValuesEqual(selectName, index, optionValues);
1554 } catch (AssertionError e) {
1555 return;
1556 }
1557 fail("Values not expected to be equal");
1558 }
1559
1560
1561
1562
1563
1564
1565
1566
1567 public void assertSelectedOptionsEqual(String selectName, String[] labels) {
1568 assertFormElementPresent(selectName);
1569 assertEquals(labels.length, getTestingEngine()
1570 .getSelectedOptions(selectName).length);
1571 for (int i = 0; i < labels.length; i++)
1572 assertEquals(labels[i],
1573 getTestingEngine()
1574 .getSelectOptionLabelForValue(
1575 selectName,
1576 getTestingEngine().getSelectedOptions(
1577 selectName)[i]));
1578 }
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588 public void assertSelectedOptionsEqual(String selectName, int index, String[] labels) {
1589 assertFormElementPresent(selectName);
1590 assertEquals(labels.length, getTestingEngine()
1591 .getSelectedOptions(selectName, index).length);
1592 for (int i = 0; i < labels.length; i++)
1593 assertEquals(labels[i],
1594 getTestingEngine()
1595 .getSelectOptionLabelForValue(
1596 selectName, index,
1597 getTestingEngine().getSelectedOptions(
1598 selectName, index)[i]));
1599 }
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609 public void assertSelectedOptionEquals(String selectName, String optionLabel) {
1610 assertSelectedOptionsEqual(selectName, new String[] { optionLabel });
1611 }
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621 public void assertSelectedOptionEquals(String selectName, int index, String option) {
1622 assertSelectedOptionsEqual(selectName, index, new String[] { option });
1623 }
1624
1625
1626
1627
1628
1629
1630
1631
1632 public void assertSelectedOptionValuesEqual(String selectName,
1633 String[] values) {
1634 assertFormElementPresent(selectName);
1635 assertEquals(values.length, getTestingEngine()
1636 .getSelectedOptions(selectName).length);
1637 for (int i = 0; i < values.length; i++)
1638 assertEquals(values[i], getTestingEngine()
1639 .getSelectedOptions(selectName)[i]);
1640 }
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651 public void assertSelectedOptionValuesEqual(String selectName,
1652 int index, String[] values) {
1653 assertFormElementPresent(selectName);
1654 assertEquals(values.length, getTestingEngine()
1655 .getSelectedOptions(selectName, index).length);
1656 for (int i = 0; i < values.length; i++)
1657 assertEquals(values[i], getTestingEngine()
1658 .getSelectedOptions(selectName, index)[i]);
1659 }
1660
1661
1662
1663
1664
1665
1666
1667
1668 public void assertSelectedOptionValueEquals(String selectName, String value) {
1669 assertSelectedOptionValuesEqual(selectName, new String[] { value });
1670 }
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680 public void assertSelectedOptionValueEquals(String selectName, int index, String value) {
1681 assertSelectedOptionValuesEqual(selectName, index, new String[] { value });
1682 }
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692 public void assertSelectedOptionsMatch(String selectName, String[] regexps) {
1693 assertFormElementPresent(selectName);
1694 assertEquals(regexps.length, getTestingEngine()
1695 .getSelectedOptions(selectName).length);
1696 for (int i = 0; i < regexps.length; i++) {
1697 RE re = getRE(regexps[i]);
1698 assertTrue("Unable to match [" + regexps[i]
1699 + "] in option \""
1700 + getTestingEngine().getSelectedOptions(selectName)[i]
1701 + "\"", re.match(getTestingEngine().getSelectedOptions(
1702 selectName)[i]));
1703 }
1704 }
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714 public void assertSelectedOptionsMatch(String selectName, int index, String[] regexps) {
1715 assertFormElementPresent(selectName);
1716 assertEquals(regexps.length, getTestingEngine()
1717 .getSelectedOptions(selectName, index).length);
1718 for (int i = 0; i < regexps.length; i++) {
1719 RE re = getRE(regexps[i]);
1720 assertTrue("Unable to match [" + regexps[i]
1721 + "] in option \""
1722 + getTestingEngine().getSelectedOptions(selectName, index)[i]
1723 + "\" at index " + index, re.match(getTestingEngine().getSelectedOptions(
1724 selectName, index)[i]));
1725 }
1726 }
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736 public void assertSelectedOptionMatches(String selectName, String regexp) {
1737 assertSelectedOptionsMatch(selectName, new String[] { regexp });
1738 }
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748 public void assertSelectedOptionMatches(String selectName, int index, String regexp) {
1749 assertSelectedOptionsMatch(selectName, index, new String[] { regexp });
1750 }
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762 public void assertSubmitButtonPresent() {
1763 assertTrue("no submit button found.", getTestingEngine()
1764 .hasSubmitButton());
1765 }
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778 public void assertSubmitButtonPresent(String buttonName) {
1779 assertTrue("Submit Button [" + buttonName + "] not found.",
1780 getTestingEngine().hasSubmitButton(buttonName));
1781 }
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794 public void assertSubmitButtonNotPresent() {
1795 assertFalse("Submit Button found.", getTestingEngine()
1796 .hasSubmitButton());
1797 }
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810 public void assertSubmitButtonNotPresent(String buttonName) {
1811 assertFalse("Submit Button [" + buttonName + "] found.",
1812 getTestingEngine().hasSubmitButton(buttonName));
1813 }
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827 public void assertSubmitButtonPresent(String buttonName, String buttonValue) {
1828 assertTrue("Submit Button [" + buttonName + "] with value ["
1829 + buttonValue + "] not found.", getTestingEngine()
1830 .hasSubmitButton(buttonName, buttonValue));
1831 }
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841 public void assertResetButtonPresent() {
1842 assertTrue("no reset button found.", getTestingEngine()
1843 .hasResetButton());
1844 }
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855 public void assertResetButtonPresent(String buttonName) {
1856 assertTrue("Reset Button [" + buttonName + "] not found.",
1857 getTestingEngine().hasResetButton(buttonName));
1858 }
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870 public void assertResetButtonNotPresent() {
1871 assertFalse("Reset Button found.", getTestingEngine()
1872 .hasResetButton());
1873 }
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885 public void assertResetButtonNotPresent(String buttonName) {
1886 assertFalse("Reset Button [" + buttonName + "] found.",
1887 getTestingEngine().hasResetButton(buttonName));
1888 }
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900 public void assertButtonPresent(String buttonId) {
1901 assertTrue("Button [" + buttonId + "] not found.", getTestingEngine()
1902 .hasButton(buttonId));
1903 }
1904
1905
1906
1907
1908
1909
1910 public void assertButtonPresentWithText(String text) {
1911 assertTrue("Did not find button with text [" + text + "].",
1912 getTestingEngine().hasButtonWithText(text));
1913 }
1914
1915
1916
1917
1918
1919
1920 public void assertButtonNotPresentWithText(String text) {
1921 assertFalse("Found button with text [" + text + "].",
1922 getTestingEngine().hasButtonWithText(text));
1923 }
1924
1925
1926
1927
1928
1929
1930 public void assertButtonNotPresent(String buttonId) {
1931 assertFalse(
1932 "Button [" + buttonId + "] found when not expected.",
1933 getTestingEngine().hasButton(buttonId));
1934 }
1935
1936
1937
1938
1939
1940
1941 public void assertLinkPresent(String linkId) {
1942 assertTrue("Unable to find link with id [" + linkId + "]",
1943 getTestingEngine().hasLink(linkId));
1944 }
1945
1946
1947
1948
1949
1950
1951 public void assertLinkNotPresent(String linkId) {
1952 assertTrue("link with id [" + linkId + "] found in response",
1953 !getTestingEngine().hasLink(linkId));
1954 }
1955
1956
1957
1958
1959
1960
1961 public void assertLinkPresentWithText(String linkText) {
1962 assertTrue("Link with text [" + linkText
1963 + "] not found in response.", getTestingEngine()
1964 .hasLinkWithText(linkText, 0));
1965 }
1966
1967
1968
1969
1970
1971
1972 public void assertLinkNotPresentWithText(String linkText) {
1973 assertTrue("Link with text [" + linkText
1974 + "] found in response.", !getTestingEngine().hasLinkWithText(
1975 linkText, 0));
1976 }
1977
1978
1979
1980
1981
1982
1983
1984 public void assertLinkPresentWithText(String linkText, int index) {
1985 assertTrue("Link with text [" + linkText + "] and index ["
1986 + index + "] not found in response.", getTestingEngine()
1987 .hasLinkWithText(linkText, index));
1988 }
1989
1990
1991
1992
1993
1994
1995
1996 public void assertLinkNotPresentWithText(String linkText, int index) {
1997 assertTrue("Link with text [" + linkText + "] and index "
1998 + index + " found in response.", !getTestingEngine()
1999 .hasLinkWithText(linkText, index));
2000 }
2001
2002
2003
2004
2005
2006
2007
2008
2009 public void assertLinkPresentWithExactText(String linkText) {
2010 assertTrue("Link with Exact text [" + linkText
2011 + "] not found in response.", getTestingEngine()
2012 .hasLinkWithExactText(linkText, 0));
2013 }
2014
2015
2016
2017
2018
2019
2020 public void assertLinkNotPresentWithExactText(String linkText) {
2021 assertTrue("Link with Exact text [" + linkText
2022 + "] found in response.", !getTestingEngine()
2023 .hasLinkWithExactText(linkText, 0));
2024 }
2025
2026
2027
2028
2029
2030
2031
2032 public void assertLinkPresentWithExactText(String linkText, int index) {
2033 assertTrue("Link with Exact text [" + linkText + "] and index ["
2034 + index + "] not found in response.", getTestingEngine()
2035 .hasLinkWithExactText(linkText, index));
2036 }
2037
2038
2039
2040
2041
2042
2043
2044 public void assertLinkNotPresentWithExactText(String linkText, int index) {
2045 assertTrue("Link with Exact text [" + linkText + "] and index "
2046 + index + " found in response.", !getTestingEngine()
2047 .hasLinkWithExactText(linkText, index));
2048 }
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058 public void assertLinkPresentWithImage(String imageFileName) {
2059 assertTrue("Link with image file [" + imageFileName
2060 + "] not found in response.", getTestingEngine()
2061 .hasLinkWithImage(imageFileName, 0));
2062 }
2063
2064
2065
2066
2067
2068
2069
2070
2071 public void assertLinkPresentWithImage(String imageFileName, int index) {
2072 assertTrue("Link with image file [" + imageFileName
2073 + "] and index " + index + " not found in response.", getTestingEngine()
2074 .hasLinkWithImage(imageFileName, index));
2075 }
2076
2077
2078
2079
2080
2081
2082
2083 public void assertLinkNotPresentWithImage(String imageFileName) {
2084 assertFalse("Link with image file [" + imageFileName
2085 + "] found in response.", getTestingEngine().hasLinkWithImage(
2086 imageFileName, 0));
2087 }
2088
2089
2090
2091
2092
2093
2094
2095
2096 public void assertLinkNotPresentWithImage(String imageFileName, int index) {
2097 assertFalse("Link with image file [" + imageFileName
2098 + "] and index " + index + " found in response.",
2099 getTestingEngine().hasLinkWithImage(imageFileName, index));
2100 }
2101
2102
2103
2104
2105
2106
2107 public void assertElementPresent(String anID) {
2108 assertTrue("Unable to locate element with id \"" + anID + "\"",
2109 getTestingEngine().hasElement(anID));
2110 }
2111
2112
2113
2114
2115
2116
2117 public void assertElementNotPresent(String anID) {
2118 assertFalse("Located element with id \"" + anID + "\"",
2119 getTestingEngine().hasElement(anID));
2120 }
2121
2122
2123
2124
2125
2126
2127 public void assertElementPresentByXPath(String xpath) {
2128 assertTrue("Unable to locate element with xpath \"" + xpath
2129 + "\"", getTestingEngine().hasElementByXPath(xpath));
2130 }
2131
2132
2133
2134
2135
2136
2137 public void assertElementNotPresentByXPath(String xpath) {
2138 assertFalse("Located element with xpath \"" + xpath + "\"",
2139 getTestingEngine().hasElementByXPath(xpath));
2140 }
2141
2142
2143
2144
2145 public List<String> getComments() {
2146 return getTestingEngine().getComments();
2147 }
2148
2149
2150
2151
2152
2153
2154 public void assertCommentPresent(String comment) {
2155 assertTrue("Comment present: '" + comment + "'", getComments().contains(comment.trim()));
2156 }
2157
2158
2159
2160
2161
2162
2163 public void assertCommentNotPresent(String comment) {
2164 assertFalse("Comment not present: '" + comment + "'", getComments().contains(comment.trim()));
2165 }
2166
2167
2168
2169
2170
2171
2172
2173 public void assertTextInElement(String elementID, String text) {
2174 assertTrue("Unable to locate element with id \"" + elementID
2175 + "\"", getTestingEngine().hasElement(elementID));
2176 assertTrue("Unable to locate [" + text + "] in element \""
2177 + elementID + "\"", getTestingEngine()
2178 .isTextInElement(elementID, text));
2179 }
2180
2181 public void assertTextNotInElement(String elementID, String text) {
2182 assertElementPresent(elementID);
2183 assertTrue("Unable to locate element with id \"" + elementID
2184 + "\"", getTestingEngine().hasElement(elementID));
2185 assertFalse("Text [" + text + "] found in element [" + elementID
2186 + "] when not expected", getTestingEngine().isTextInElement(
2187 elementID, text));
2188 }
2189
2190
2191
2192
2193
2194
2195
2196 public void assertMatchInElement(String elementID, String regexp) {
2197 assertTrue("Unable to locate element with id \"" + elementID
2198 + "\"", getTestingEngine().hasElement(elementID));
2199 assertTrue("Unable to match [" + regexp + "] in element \""
2200 + elementID + "\"", getTestingEngine().isMatchInElement(
2201 elementID, regexp));
2202 }
2203
2204
2205
2206
2207
2208
2209
2210 public void assertNoMatchInElement(String elementID, String regexp) {
2211 assertElementPresent(elementID);
2212 assertTrue("Unable to locate element with id \"" + elementID
2213 + "\"", getTestingEngine().hasElement(elementID));
2214 assertFalse("Regexp [" + regexp + "] matched in element ["
2215 + elementID + "] when not expected", getTestingEngine()
2216 .isMatchInElement(elementID, regexp));
2217 }
2218
2219
2220
2221
2222
2223
2224 public void assertWindowPresent(String windowName) {
2225 assertTrue("Unable to locate window [" + windowName + "].",
2226 getTestingEngine().hasWindow(windowName));
2227 }
2228
2229
2230
2231
2232
2233
2234 public void assertWindowPresent(int windowID) {
2235 assertTrue("There is no window with index [" + windowID + "].",
2236 getTestingEngine().getWindowCount() > windowID);
2237 }
2238
2239
2240
2241
2242
2243
2244 public void assertWindowPresentWithTitle(String title) {
2245 assertTrue(
2246 "Unable to locate window with title [" + title + "].",
2247 getTestingEngine().hasWindowByTitle(title));
2248 }
2249
2250
2251
2252
2253
2254
2255 public void assertWindowCountEquals(int windowCount) {
2256 assertTrue("Window count is "
2257 + getTestingEngine().getWindowCount() + " but "
2258 + windowCount + " was expected.", getTestingEngine()
2259 .getWindowCount() == windowCount);
2260 }
2261
2262
2263
2264
2265
2266
2267 public void assertFramePresent(String frameNameOrId) {
2268 assertTrue("Unable to locate frame with name or ID ["
2269 + frameNameOrId + "].", getTestingEngine().hasFrame(
2270 frameNameOrId));
2271 }
2272
2273
2274
2275
2276
2277
2278 public void assertCookiePresent(String cookieName) {
2279 List<?> cookies = getTestingEngine().getCookies();
2280 for (Iterator<?> i = cookies.iterator(); i.hasNext();) {
2281 if (((Cookie) i.next()).getName().equals(cookieName)) {
2282 return;
2283 }
2284 }
2285 fail("Could not find Cookie with name [" + cookieName + "]");
2286 }
2287
2288
2289
2290
2291
2292
2293
2294 public void assertCookieValueEquals(String cookieName, String expectedValue) {
2295 assertCookiePresent(cookieName);
2296 List<?> cookies = getTestingEngine().getCookies();
2297 for (Iterator<?> i = cookies.iterator(); i.hasNext();) {
2298 Cookie c = (Cookie) i.next();
2299 if (c.getName().equals(cookieName) && c.getValue().equals(expectedValue)) {
2300 return;
2301 }
2302 }
2303 fail("Could not find cookie with name [" + cookieName + "] and value [" + expectedValue + "]");
2304 }
2305
2306
2307
2308
2309
2310
2311
2312 public void assertCookieValueMatch(String cookieName, String regexp) {
2313 assertCookiePresent(cookieName);
2314 RE re = null;
2315 try {
2316 re = new RE(regexp, RE.MATCH_SINGLELINE);
2317 } catch (RESyntaxException e) {
2318 fail(e.getMessage());
2319 }
2320 List<?> cookies = getTestingEngine().getCookies();
2321 for (Iterator<?> i = cookies.iterator(); i.hasNext();) {
2322 Cookie c = (Cookie) i.next();
2323 if (c.getName().equals(cookieName) &&
2324 re.match(c.getValue())) {
2325 return;
2326 }
2327 }
2328 fail("Could not find cookie with name [" + cookieName + "] with value matching [" + regexp + "]");
2329 }
2330
2331
2332
2333
2334
2335
2336 public String getFormElementValue(String formElementName) {
2337 assertFormElementPresent(formElementName);
2338 return getTestingEngine().getElementAttributByXPath(
2339 "//input[@name='" + formElementName + "']", "value");
2340 }
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350 public void setWorkingForm(int index) {
2351 getTestingEngine().setWorkingForm(index);
2352 }
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362 public void setWorkingForm(String nameOrId) {
2363 assertFormPresent(nameOrId);
2364 getTestingEngine().setWorkingForm(nameOrId, 0);
2365 }
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376 public void setWorkingForm(String nameOrId, int index) {
2377 assertFormPresent(nameOrId, index);
2378 getTestingEngine().setWorkingForm(nameOrId, index);
2379 }
2380
2381
2382
2383
2384
2385
2386
2387 public void setTextField(String inputName, String value) {
2388 assertFormElementPresent(inputName);
2389 getTestingEngine().setTextField(inputName, value);
2390 }
2391
2392
2393
2394
2395
2396
2397
2398 public void setHiddenField(String inputName, String value) {
2399 assertFormElementPresent(inputName);
2400 getTestingEngine().setHiddenField(inputName, value);
2401 }
2402
2403
2404
2405
2406
2407
2408 public void checkCheckbox(String checkBoxName) {
2409 assertCheckboxPresent(checkBoxName);
2410 getTestingEngine().checkCheckbox(checkBoxName);
2411 }
2412
2413
2414
2415
2416
2417
2418
2419 public void checkCheckbox(String checkBoxName, String value) {
2420 assertCheckboxPresent(checkBoxName);
2421 getTestingEngine().checkCheckbox(checkBoxName, value);
2422 }
2423
2424
2425
2426
2427
2428
2429 public void uncheckCheckbox(String checkBoxName) {
2430 assertFormElementPresent(checkBoxName);
2431 getTestingEngine().uncheckCheckbox(checkBoxName);
2432 }
2433
2434
2435
2436
2437
2438
2439
2440 public void uncheckCheckbox(String checkBoxName, String value) {
2441 assertFormElementPresent(checkBoxName);
2442 getTestingEngine().uncheckCheckbox(checkBoxName, value);
2443 }
2444
2445
2446
2447
2448
2449
2450
2451 public void selectOptions(String selectName, String[] labels) {
2452 assertSelectOptionsPresent(selectName, labels);
2453 selectOptionsByLabel(selectName, labels);
2454 }
2455
2456
2457
2458
2459
2460
2461
2462 public void selectOption(String selectName, String label) {
2463 selectOptions(selectName, new String[] { label });
2464 }
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474 public void selectOption(String selectName, int index, String label) {
2475 selectOptions(selectName, index, new String[] { label });
2476 }
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486 public void selectOptions(String selectName, int index, String[] labels) {
2487 assertSelectOptionsPresent(selectName, index, labels);
2488 selectOptionsByLabel(selectName, index, labels);
2489 }
2490
2491
2492
2493
2494
2495
2496
2497
2498 public void selectOptionsByValues(String selectName, String[] values) {
2499 assertSelectOptionValuesPresent(selectName, values);
2500 getTestingEngine().selectOptions(selectName, values);
2501 }
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511 public void selectOptionByValue(String selectName, String value) {
2512 selectOptionsByValues(selectName, new String[] { value });
2513 }
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523 public void selectOptionsByValues(String selectName, int index, String[] values) {
2524 assertSelectOptionValuesPresent(selectName, index, values);
2525 getTestingEngine().selectOptions(selectName, index, values);
2526 }
2527
2528
2529
2530
2531
2532
2533
2534 public void selectOptionByValue(String selectName, int index, String value) {
2535 selectOptionsByValues(selectName, index, new String[] { value });
2536 }
2537
2538
2539
2540
2541
2542
2543
2544
2545 public void submit() {
2546 assertSubmitButtonPresent();
2547 getTestingEngine().submit();
2548 }
2549
2550
2551
2552
2553
2554
2555 public void submit(String buttonName) {
2556 assertSubmitButtonPresent(buttonName);
2557 getTestingEngine().submit(buttonName);
2558 }
2559
2560
2561
2562
2563
2564
2565
2566
2567 public void submit(String buttonName, String buttonValue) {
2568 assertSubmitButtonPresent(buttonName, buttonValue);
2569 getTestingEngine().submit(buttonName, buttonValue);
2570 }
2571
2572
2573
2574
2575
2576 public void reset() {
2577 assertResetButtonPresent();
2578 getTestingEngine().reset();
2579 }
2580
2581
2582
2583
2584
2585
2586 public void clickLinkWithText(String linkText) {
2587 assertLinkPresentWithText(linkText);
2588 getTestingEngine().clickLinkWithText(linkText, 0);
2589 }
2590
2591
2592
2593
2594
2595
2596
2597 public void clickLinkWithText(String linkText, int index) {
2598 assertLinkPresentWithText(linkText, index);
2599 getTestingEngine().clickLinkWithText(linkText, index);
2600 }
2601
2602
2603
2604
2605
2606
2607 public void clickLinkWithExactText(String linkText) {
2608 assertLinkPresentWithExactText(linkText);
2609 getTestingEngine().clickLinkWithExactText(linkText, 0);
2610 }
2611
2612
2613
2614
2615
2616
2617
2618 public void clickLinkWithExactText(String linkText, int index) {
2619 assertLinkPresentWithExactText(linkText, index);
2620 getTestingEngine().clickLinkWithExactText(linkText, index);
2621 }
2622
2623
2624
2625
2626
2627
2628 public void clickButton(String buttonId) {
2629 assertButtonPresent(buttonId);
2630 getTestingEngine().clickButton(buttonId);
2631 }
2632
2633
2634
2635
2636
2637
2638 public void clickButtonWithText(String buttonValueText) {
2639 assertButtonPresentWithText(buttonValueText);
2640 getTestingEngine().clickButtonWithText(buttonValueText);
2641 }
2642
2643
2644
2645
2646
2647
2648
2649 public void clickLinkWithImage(String imageFileName) {
2650 assertLinkPresentWithImage(imageFileName);
2651 getTestingEngine().clickLinkWithImage(imageFileName, 0);
2652 }
2653
2654
2655
2656
2657
2658
2659
2660
2661 public void clickLinkWithImage(String imageFileName, int index) {
2662 assertLinkPresentWithImage(imageFileName, index);
2663 getTestingEngine().clickLinkWithImage(imageFileName, index);
2664 }
2665
2666
2667
2668
2669
2670
2671 public void clickLink(String linkId) {
2672 assertLinkPresent(linkId);
2673 getTestingEngine().clickLink(linkId);
2674 }
2675
2676
2677
2678
2679
2680
2681
2682 public void clickRadioOption(String radioGroup, String radioOption) {
2683 assertRadioOptionPresent(radioGroup, radioOption);
2684 getTestingEngine().clickRadioOption(radioGroup, radioOption);
2685 }
2686
2687
2688
2689
2690
2691
2692 public void clickElementByXPath(String xpath) {
2693 assertElementPresentByXPath(xpath);
2694 getTestingEngine().clickElementByXPath(xpath);
2695 }
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706 public String getElementAttributeByXPath(String xpath, String attribute) {
2707 assertElementPresentByXPath(xpath);
2708 return getTestingEngine().getElementAttributByXPath(xpath, attribute);
2709 }
2710
2711
2712
2713
2714 public String getElementAttributByXPath(String xpath, String attribute) {
2715 return getElementAttributeByXPath(xpath, attribute);
2716 }
2717
2718
2719
2720
2721
2722
2723 public String getElementTextByXPath(String xpath){
2724 assertElementPresentByXPath(xpath);
2725 return getTestingEngine().getElementTextByXPath(xpath);
2726 }
2727
2728
2729
2730
2731
2732
2733
2734
2735 public IElement getElementByXPath(String xpath) {
2736 assertElementPresentByXPath(xpath);
2737 return getTestingEngine().getElementByXPath(xpath);
2738 }
2739
2740
2741
2742
2743
2744
2745
2746
2747 public boolean hasElementByXPath(String xpath) {
2748 return getTestingEngine().getElementByXPath(xpath) != null;
2749 }
2750
2751
2752
2753
2754
2755
2756
2757
2758 public IElement getElementById(String id) {
2759 assertElementPresent(id);
2760 return getTestingEngine().getElementByID(id);
2761 }
2762
2763
2764
2765
2766
2767
2768
2769 public boolean hasElementById(String id) {
2770 return getTestingEngine().getElementByID(id) != null;
2771 }
2772
2773
2774
2775
2776
2777
2778
2779 public List<IElement> getElementsByXPath(String xpath) {
2780 return getTestingEngine().getElementsByXPath(xpath);
2781 }
2782
2783
2784
2785
2786
2787
2788
2789 public boolean hasElementsByXPath(String xpath) {
2790 List<IElement> list = getTestingEngine().getElementsByXPath(xpath);
2791 return list != null && !list.isEmpty();
2792 }
2793
2794
2795
2796
2797
2798 public void assertLabelPresent(String id) {
2799 assertNotNull("No label found with id [" + id + "]", getLabel(id));
2800 }
2801
2802
2803
2804
2805
2806
2807
2808 private IElement getLabel(String id) {
2809
2810 for (IElement e : getTestingEngine().getElementsByXPath("//label")) {
2811 if (id.equals(e.getAttribute("id")))
2812 return e;
2813 }
2814 return null;
2815 }
2816
2817
2818
2819
2820
2821
2822
2823
2824 private IElement getElementWithText(String elementName, String text) {
2825 for (IElement e : getTestingEngine().getElementsByXPath("//" + elementName)) {
2826 if (elementName.equals(e.getName()) && text.equals(e.getTextContent())) {
2827 return e;
2828 }
2829 }
2830 return null;
2831 }
2832
2833
2834
2835
2836 public void assertLabelMatches(String regexp) {
2837
2838 RE re = null;
2839 try {
2840 re = new RE(regexp, RE.MATCH_SINGLELINE);
2841 } catch (RESyntaxException e) {
2842 fail(e.toString());
2843 }
2844
2845
2846 for (IElement e : getTestingEngine().getElementsByXPath("//label")) {
2847 if (e.getName().equals("label") && re.match( e.getTextContent() ))
2848 return;
2849 }
2850 fail("No label found with text matching [" + regexp + "]");
2851 }
2852
2853
2854
2855
2856
2857
2858
2859
2860 public List<IElement> getFieldsForLabel(IElement label) {
2861 List<IElement> fields = new java.util.ArrayList<IElement>();
2862
2863 if (label.getAttribute("for") != null) {
2864 IElement e = getTestingEngine().getElementByID(label.getAttribute("for"));
2865 if (e != null)
2866 fields.add(e);
2867 }
2868
2869
2870 if (fields.isEmpty()) {
2871
2872 for (IElement e : label.getChildren()) {
2873 if (e.getName().equals("input") || e.getName().equals("textarea") || e.getName().equals("select")) {
2874 fields.add(e);
2875 }
2876 }
2877 }
2878
2879 return fields;
2880 }
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890 private void assertLabeledFieldEquals(String identifier, IElement label, String fieldText) {
2891 String value = getLabeledFieldValue(identifier, label);
2892 assertEquals("unexpected value of field for label [" + identifier + "]", fieldText, value == null ? "" : value);
2893 }
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903 public String getLabeledFieldValue(String identifier, IElement label) {
2904 List<IElement> fields = getFieldsForLabel(label);
2905
2906 assertFalse("No field found for label [" + identifier + "]", fields.isEmpty());
2907 String value = null;
2908
2909 for (IElement field : fields) {
2910 if (value != null)
2911 break;
2912 if (field == null)
2913 throw new RuntimeException("unexpected null field");
2914
2915 if ("input".equals(field.getName())) {
2916 if (field.getAttribute("type") != null) {
2917 if (field.getAttribute("type").toLowerCase().equals("checkbox")) {
2918 if (field.getAttribute("checked") != null) {
2919 value = field.getAttribute("value");
2920 }
2921 } else if (field.getAttribute("type").toLowerCase().equals("radio")) {
2922 if (field.getAttribute("checked") != null) {
2923 value = field.getAttribute("value");
2924 }
2925 } else {
2926
2927 value = field.getAttribute("value");
2928 }
2929 } else {
2930
2931 value = field.getAttribute("value");
2932 }
2933 } else if ("textarea".equals(field.getName())) {
2934 value = field.getTextContent();
2935 } else if ("select".equals(field.getName())) {
2936
2937 for (IElement child : field.getChildren()) {
2938 if (child.getName().equals("option") && child.getAttribute("selected") != null) {
2939 value = child.getAttribute("value");
2940 break;
2941 }
2942 if (child.getName().equals("optgroup")) {
2943 for (IElement subchild : child.getChildren()) {
2944 if (subchild.getName().equals("option") && subchild.getAttribute("selected") != null) {
2945 value = child.getAttribute("value");
2946 break;
2947 }
2948 }
2949 }
2950 }
2951 } else {
2952 throw new RuntimeException("Unexpected field type " + field.getName());
2953 }
2954 }
2955
2956 return value;
2957 }
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968 public void assertLabeledFieldEquals(String id, String fieldText) {
2969 IElement label = getLabel(id);
2970 assertNotNull("no label for id [" + id + "] found", label);
2971
2972 assertLabeledFieldEquals(id, label, fieldText);
2973 }
2974
2975 public void setLabeledFormElementField(String id, String value) {
2976 IElement label = getLabel(id);
2977 assertNotNull("no label for id [" + id + "] found", label);
2978
2979 List<IElement> fields = getFieldsForLabel(label);
2980 assertFalse("there should be at least one element referenced for label [" + id + "]", fields.size()==0);
2981
2982
2983 for (IElement field : fields) {
2984 if (field == null)
2985 throw new RuntimeException("unexpected null field");
2986
2987 if ("input".equals(field.getName())) {
2988 if (field.getAttribute("type") != null) {
2989 if (field.getAttribute("type").toLowerCase().equals("checkbox")) {
2990 if (value.equals(field.getAttribute("value"))) {
2991 field.setAttribute("checked");
2992 return;
2993 }
2994 } else if (field.getAttribute("type").toLowerCase().equals("radio")) {
2995 if (value.equals(field.getAttribute("value"))) {
2996 field.setAttribute("checked");
2997 return;
2998 }
2999 } else {
3000
3001 field.setAttribute("value", value);
3002 return;
3003 }
3004 } else {
3005
3006 field.setAttribute("value", value);
3007 return;
3008 }
3009 } else if ("textarea".equals(field.getName())) {
3010 field.setTextContent(value);
3011 return;
3012 } else if ("select".equals(field.getName())) {
3013
3014 for (IElement children : field.getChildren()) {
3015
3016 if (children.getName().equals("option") && value.equals(children.getAttribute("value"))) {
3017 children.setAttribute("selected");
3018 return;
3019 }
3020 }
3021 } else {
3022 throw new RuntimeException("Unexpected field type " + field.getName());
3023 }
3024 }
3025
3026 fail("could not find any fields for label [" + id + "] to set.");
3027 }
3028
3029
3030
3031
3032
3033
3034
3035
3036 public void gotoWindow(String windowName) {
3037 assertWindowPresent(windowName);
3038 getTestingEngine().gotoWindow(windowName);
3039 }
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049 @Deprecated
3050 public void gotoWindow(int windowID) {
3051 assertWindowPresent(windowID);
3052 getTestingEngine().gotoWindow(windowID);
3053 }
3054
3055
3056
3057
3058 public void gotoRootWindow() {
3059 getTestingEngine().gotoRootWindow();
3060 }
3061
3062
3063
3064
3065
3066
3067 public void gotoWindowByTitle(String title) {
3068 assertWindowPresentWithTitle(title);
3069 getTestingEngine().gotoWindowByTitle(title);
3070 }
3071
3072
3073
3074
3075
3076
3077 public void gotoFrame(String frameNameOrId) {
3078 getTestingEngine().gotoFrame(frameNameOrId);
3079 }
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089 public void gotoPage(String url) throws TestingEngineResponseException {
3090 try {
3091 getTestingEngine().gotoPage(createUrl(url, getTestContext().getBaseUrl()));
3092 } catch (MalformedURLException e) {
3093 fail(e.getLocalizedMessage());
3094 }
3095 }
3096
3097
3098
3099
3100
3101 public void dumpCookies() {
3102 List<?> cookies = getTestingEngine().getCookies();
3103 for (Iterator<?> i = cookies.iterator(); i.hasNext();) {
3104 Cookie c = (Cookie) i.next();
3105 System.out.println("Name=" + c.getName() + "; Value="
3106 + c.getValue() + "; Domain=" + c.getDomain() + "; Comment="
3107 + c.getComment() + "; MaxAge=" + c.getMaxAge() + "; Path="
3108 + c.getPath() + "; Version=" + c.getVersion());
3109 }
3110 }
3111
3112
3113
3114
3115
3116
3117 public String getPageSource() {
3118 return getTestingEngine().getPageSource();
3119 }
3120
3121
3122
3123
3124
3125
3126 public String getServerResponse() {
3127 return getTestingEngine().getServerResponse();
3128 }
3129
3130
3131
3132
3133
3134 public String getServeurResponse() {
3135 return getServerResponse();
3136 }
3137
3138
3139
3140
3141
3142
3143 public void saveAs(File f) {
3144 InputStream in = getTestingEngine().getInputStream();
3145 int c=0;
3146 try {
3147 f.createNewFile();
3148 OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
3149 while ((c=in.read()) != -1) out.write(c);
3150 in.close();
3151 out.close();
3152 } catch (IOException e) {
3153 throw new RuntimeException("Error when writing to file", e);
3154 }
3155
3156 }
3157
3158
3159
3160
3161
3162
3163 public void assertDownloadedFileEquals(URL expected) {
3164 try {
3165 File tmp = File.createTempFile("jwebunit", null);
3166 tmp.deleteOnExit();
3167 saveAs(tmp);
3168 assertTrue("Files are not binary equals.", areFilesEqual(
3169 expected, tmp.toURI().toURL()));
3170 } catch (IOException e) {
3171 e.printStackTrace();
3172 fail(e.toString());
3173 }
3174 }
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184 public void dumpHtml() {
3185 dumpHtml(System.out);
3186 }
3187
3188
3189
3190
3191
3192
3193
3194 public void dumpHtml(PrintStream stream) {
3195 stream.println(getTestingEngine().getPageSource());
3196 }
3197
3198
3199
3200
3201
3202
3203
3204 public void dumpTable(String tableNameOrId) {
3205 dumpTable(tableNameOrId, System.out);
3206 }
3207
3208
3209
3210
3211
3212
3213
3214
3215 public void dumpTable(String tableNameOrId, PrintStream stream) {
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227 }
3228
3229
3230
3231
3232
3233
3234 public void setScriptingEnabled(boolean value) {
3235 getTestingEngine().setScriptingEnabled(value);
3236 }
3237
3238
3239
3240
3241
3242
3243
3244 public void setTestingEngineKey(String testingEngineKey) {
3245 this.testingEngineKey = testingEngineKey;
3246 testingEngine = null;
3247 }
3248
3249
3250
3251
3252
3253
3254
3255 public String getTestingEngineKey() {
3256 if (testingEngineKey == null) {
3257
3258 String key = TestingEngineRegistry.getFirstAvailable();
3259 if (key != null) {
3260 setTestingEngineKey(key);
3261 } else {
3262 throw new RuntimeException(
3263 "TestingEngineRegistry contains no testing engine. Check you put at least one plugin in the classpath.");
3264 }
3265 }
3266 return testingEngineKey;
3267 }
3268
3269 private RE getRE(String regexp) {
3270 RE re = null;
3271 try {
3272 re = new RE(regexp, RE.MATCH_SINGLELINE);
3273 } catch (RESyntaxException e) {
3274 fail(e.toString());
3275 }
3276 return re;
3277 }
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304 private String[] getOptionsFor(String selectName) {
3305 String[] values = getTestingEngine().getSelectOptionValues(selectName);
3306 String[] result = new String[values.length];
3307 for (int i = 0; i < values.length; i++) {
3308 result[i] = getTestingEngine().getSelectOptionLabelForValue(
3309 selectName, values[i]);
3310 }
3311 return result;
3312 }
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322 private String[] getOptionsFor(String selectName, int index) {
3323 String[] values = getTestingEngine().getSelectOptionValues(selectName, index);
3324 String[] result = new String[values.length];
3325 for (int i = 0; i < values.length; i++) {
3326 result[i] = getTestingEngine().getSelectOptionLabelForValue(
3327 selectName, index, values[i]);
3328 }
3329 return result;
3330 }
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340 private void selectOptionsByLabel(String selectName, String[] labels) {
3341 String[] values = new String[labels.length];
3342 for (int i = 0; i < values.length; i++) {
3343 values[i] = getTestingEngine().getSelectOptionValueForLabel(
3344 selectName, labels[i]);
3345 }
3346 getTestingEngine().selectOptions(selectName, values);
3347 }
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357 private void selectOptionsByLabel(String selectName, int index, String[] labels) {
3358 String[] values = new String[labels.length];
3359 for (int i = 0; i < values.length; i++) {
3360 values[i] = getTestingEngine().getSelectOptionValueForLabel(
3361 selectName, index, labels[i]);
3362 }
3363 getTestingEngine().selectOptions(selectName, index, values);
3364 }
3365
3366
3367 private void assertArraysEqual(String[] exptected, String[] returned) {
3368 assertEquals("Arrays not same length", exptected.length,
3369 returned.length);
3370 for (int i = 0; i < returned.length; i++) {
3371 assertEquals("Elements " + i + "not equal", exptected[i],
3372 returned[i]);
3373 }
3374 }
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384 public void setFormElement(String formElementName, String value) {
3385 assertFormElementPresent(formElementName);
3386 getTestingEngine().setTextField(formElementName, value);
3387 }
3388
3389
3390
3391
3392
3393
3394 public void setExpectedJavaScriptAlert(String message) {
3395 try {
3396 getTestingEngine().setExpectedJavaScriptAlert(
3397 new JavascriptAlert[] { new JavascriptAlert(message) });
3398 } catch (ExpectedJavascriptAlertException e) {
3399 fail("You previously tell that alert with message ["
3400 + e.getAlertMessage()
3401 + "] was expected, but nothing appeared.");
3402 }
3403 }
3404
3405
3406
3407
3408
3409
3410 public void setExpectedJavaScriptAlert(String[] messages) {
3411 JavascriptAlert[] alerts = new JavascriptAlert[messages.length];
3412 for (int i = 0; i < messages.length; i++) {
3413 alerts[i] = new JavascriptAlert(messages[i]);
3414 }
3415 try {
3416 getTestingEngine().setExpectedJavaScriptAlert(alerts);
3417 } catch (ExpectedJavascriptAlertException e) {
3418 fail("You previously tell that alert with message ["
3419 + e.getAlertMessage()
3420 + "] was expected, but nothing appeared.");
3421 }
3422 }
3423
3424
3425
3426
3427
3428
3429
3430 public void setExpectedJavaScriptConfirm(String message, boolean action) {
3431 try {
3432 getTestingEngine().setExpectedJavaScriptConfirm(
3433 new JavascriptConfirm[] { new JavascriptConfirm(message,
3434 action) });
3435 } catch (ExpectedJavascriptConfirmException e) {
3436 fail("You previously tell that confirm with message ["
3437 + e.getConfirmMessage()
3438 + "] was expected, but nothing appeared.");
3439 }
3440 }
3441
3442
3443
3444
3445
3446
3447
3448 public void setExpectedJavaScriptConfirm(String[] messages,
3449 boolean[] actions) {
3450 assertEquals(
3451 "You should give the same number of messages and actions",
3452 messages.length, actions.length);
3453 JavascriptConfirm[] confirms = new JavascriptConfirm[messages.length];
3454 for (int i = 0; i < messages.length; i++) {
3455 confirms[i] = new JavascriptConfirm(messages[i], actions[i]);
3456 }
3457 try {
3458 getTestingEngine().setExpectedJavaScriptConfirm(confirms);
3459 } catch (ExpectedJavascriptConfirmException e) {
3460 fail("You previously tell that confirm with message ["
3461 + e.getConfirmMessage()
3462 + "] was expected, but nothing appeared.");
3463 }
3464 }
3465
3466
3467
3468
3469
3470
3471
3472 public void setExpectedJavaScriptPrompt(String message, String input) {
3473 try {
3474 getTestingEngine().setExpectedJavaScriptPrompt(
3475 new JavascriptPrompt[] { new JavascriptPrompt(message,
3476 input) });
3477 } catch (ExpectedJavascriptPromptException e) {
3478 fail("You previously tell that prompt with message ["
3479 + e.getPromptMessage()
3480 + "] was expected, but nothing appeared.");
3481 }
3482 }
3483
3484
3485
3486
3487
3488
3489
3490 public void setExpectedJavaScriptPrompt(String[] messages, String[] inputs) {
3491 assertEquals(
3492 "You should give the same number of messages and inputs",
3493 messages.length, inputs.length);
3494 JavascriptPrompt[] prompts = new JavascriptPrompt[messages.length];
3495 for (int i = 0; i < messages.length; i++) {
3496 prompts[i] = new JavascriptPrompt(messages[i], inputs[i]);
3497 }
3498 try {
3499 getTestingEngine().setExpectedJavaScriptPrompt(prompts);
3500 } catch (ExpectedJavascriptPromptException e) {
3501 fail("You previously tell that prompt with message ["
3502 + e.getPromptMessage()
3503 + "] was expected, but nothing appeared.");
3504 }
3505 }
3506
3507
3508
3509
3510
3511
3512 public void assertImagePresent(String imageSrc, String imageAlt) {
3513 String xpath = "//img[@src=\"" + imageSrc + "\"";
3514 if (imageAlt!= null) {
3515 xpath += " and @alt=\"" + imageAlt + "\"";
3516 }
3517 xpath += "]";
3518 assertElementPresentByXPath(xpath);
3519 }
3520
3521
3522
3523
3524
3525
3526 public void assertImagePresentPartial(String partialImageSrc, String partialImageAlt) {
3527 String xpath = "//img[contains(@src, \"" + partialImageSrc + "\")";
3528 if (partialImageAlt!= null) {
3529 xpath += " and contains(@alt, \"" + partialImageAlt + "\")";
3530 }
3531 xpath += "]";
3532 assertElementPresentByXPath(xpath);
3533 }
3534
3535
3536
3537
3538 public void assertImageValid(String imageSrc, String imageAlt) {
3539 validateImage(imageSrc, imageAlt, null);
3540 }
3541
3542
3543
3544
3545
3546
3547
3548 public void assertImageValidAndStore(String imageSrc, String imageAlt,
3549 File out) {
3550 validateImage(imageSrc, imageAlt, out);
3551 }
3552
3553
3554
3555
3556 public Image getImage(String imageSrc, String imageAlt) {
3557 return validateImage(imageSrc, imageAlt, null);
3558 }
3559
3560
3561
3562
3563
3564
3565
3566
3567 public void setTimeout(int milli) {
3568 getTestingEngine().setTimeout(milli);
3569 }
3570
3571 private Image validateImage(String imageSrc, String imageAlt, File out) {
3572 assertImagePresent(imageSrc, imageAlt);
3573 URL imageUrl = null;
3574 try {
3575 imageUrl = createUrlFixed(imageSrc, getTestingEngine().getPageURL());
3576 } catch (MalformedURLException e1) {
3577 fail(e1.getLocalizedMessage());
3578 }
3579 try {
3580 final InputStream imgStream = getTestingEngine().getInputStream(imageUrl);
3581 final BufferedImage img = ImageIO.read(imgStream);
3582 if (img == null) {
3583 fail("Could not load image from " + imageUrl);
3584 }
3585 if (out != null) {
3586 ImageIO.write(img, "png", out);
3587 }
3588 return img;
3589 } catch (IOException e) {
3590 fail("Could not load or save image from " + imageUrl);
3591 } catch (TestingEngineResponseException e) {
3592 fail("The server returns the code " + e.getHttpStatusCode());
3593 }
3594 throw new IllegalStateException();
3595 }
3596
3597 protected boolean areFilesEqual(URL f1, URL f2) throws IOException {
3598
3599 InputStream i1 = f1.openStream();
3600 InputStream i2 = f2.openStream();
3601 int b1, b2;
3602 do {
3603 b1 = i1.read();
3604 b2 = i2.read();
3605 } while (b1 == b2 && b1 != -1 && b2 != -1);
3606 i1.close();
3607 i2.close();
3608
3609 return (b1 == -1) && (b2 == -1);
3610 }
3611
3612 }