1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.htmlunit.html.DomElement;
22 import org.htmlunit.html.HtmlAnchor;
23 import org.htmlunit.html.HtmlElement;
24 import org.htmlunit.html.HtmlInput;
25 import org.htmlunit.html.HtmlPage;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public final class WebAssert {
48
49
50
51
52 private WebAssert() {
53
54 }
55
56
57
58
59
60
61
62
63
64 public static void assertTitleEquals(final HtmlPage page, final String title) {
65 final String s = page.getTitleText();
66 if (!title.equals(s)) {
67 final String msg = "Page title '" + s + "' does not match expected title '" + title + "'.";
68 throw new AssertionError(msg);
69 }
70 }
71
72
73
74
75
76
77
78
79
80 public static void assertTitleContains(final HtmlPage page, final String titlePortion) {
81 final String s = page.getTitleText();
82 if (!s.contains(titlePortion)) {
83 final String msg = "Page title '" + s + "' does not contain the expected substring '" + titlePortion + "'.";
84 throw new AssertionError(msg);
85 }
86 }
87
88
89
90
91
92
93
94
95
96 public static void assertTitleMatches(final HtmlPage page, final String regex) {
97 final String s = page.getTitleText();
98 if (!s.matches(regex)) {
99 final String msg = "Page title '" + s + "' does not match the expected regular expression '" + regex + "'.";
100 throw new AssertionError(msg);
101 }
102 }
103
104
105
106
107
108
109
110
111
112 public static void assertElementPresent(final HtmlPage page, final String id) {
113 try {
114 page.getHtmlElementById(id);
115 }
116 catch (final ElementNotFoundException e) {
117 final String msg = "Expected element with ID '" + id + "' was not found on the page.";
118 throw new AssertionError(msg, e);
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public static void assertElementPresentByXPath(final HtmlPage page, final String xpath) {
137 final List<?> elements = page.getByXPath(xpath);
138 if (elements.isEmpty()) {
139 final String msg = "No elements found matching the XPath expression '" + xpath + "'.";
140 throw new AssertionError(msg);
141 }
142 }
143
144
145
146
147
148
149
150
151
152 public static void assertElementNotPresent(final HtmlPage page, final String id) {
153 try {
154 page.getHtmlElementById(id);
155 }
156 catch (final ElementNotFoundException e) {
157 return;
158 }
159 final String msg = "Found unexpected element with ID '" + id + "' on the page.";
160 throw new AssertionError(msg);
161 }
162
163
164
165
166
167
168
169
170
171 public static void assertElementNotPresentByXPath(final HtmlPage page, final String xpath) {
172 final List<?> elements = page.getByXPath(xpath);
173 if (!elements.isEmpty()) {
174 final String msg = "Found " + elements.size()
175 + " unexpected element(s) matching the XPath expression '"
176 + xpath + "'.";
177 throw new AssertionError(msg);
178 }
179 }
180
181
182
183
184
185
186
187
188
189 public static void assertTextPresent(final HtmlPage page, final String text) {
190 if (!page.asNormalizedText().contains(text)) {
191 final String msg = "Expected text '" + text + "' was not found on the page.";
192 throw new AssertionError(msg);
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207 public static void assertTextPresentInElement(final HtmlPage page, final String text, final String id) {
208 try {
209 final HtmlElement element = page.getHtmlElementById(id);
210 if (!element.asNormalizedText().contains(text)) {
211 final String msg = "Element with ID '" + id + "' does not contain the expected text '" + text + "'.";
212 throw new AssertionError(msg);
213 }
214 }
215 catch (final ElementNotFoundException e) {
216 final String msg = "Cannot verify text content: element with ID '" + id + "' was not found on the page.";
217 throw new AssertionError(msg, e);
218 }
219 }
220
221
222
223
224
225
226
227
228
229 public static void assertTextNotPresent(final HtmlPage page, final String text) {
230 if (page.asNormalizedText().contains(text)) {
231 final String msg = "Found unexpected text '" + text + "' on the page.";
232 throw new AssertionError(msg);
233 }
234 }
235
236
237
238
239
240
241
242
243
244 public static void assertTextNotPresentInElement(final HtmlPage page, final String text, final String id) {
245 try {
246 final HtmlElement element = page.getHtmlElementById(id);
247 if (element.asNormalizedText().contains(text)) {
248 final String msg = "Element with ID '" + id + "' contains unexpected text '" + text + "'.";
249 throw new AssertionError(msg);
250 }
251 }
252 catch (final ElementNotFoundException e) {
253 final String msg = "Cannot verify text content: element with ID '" + id + "' was not found on the page.";
254 throw new AssertionError(msg);
255 }
256 }
257
258
259
260
261
262
263
264
265
266
267 public static void assertLinkPresent(final HtmlPage page, final String id) {
268 try {
269 page.getDocumentElement().getOneHtmlElementByAttribute("a", DomElement.ID_ATTRIBUTE, id);
270 }
271 catch (final ElementNotFoundException e) {
272 final String msg = "Expected link with ID '" + id + "' was not found on the page.";
273 throw new AssertionError(msg, e);
274 }
275 }
276
277
278
279
280
281
282
283
284
285
286 public static void assertLinkNotPresent(final HtmlPage page, final String id) {
287 try {
288 page.getDocumentElement().getOneHtmlElementByAttribute("a", DomElement.ID_ATTRIBUTE, id);
289 final String msg = "Found unexpected link with ID '" + id + "' on the page.";
290 throw new AssertionError(msg);
291 }
292 catch (final ElementNotFoundException expected) {
293
294 }
295 }
296
297
298
299
300
301
302
303
304 public static void assertLinkPresentWithText(final HtmlPage page, final String text) {
305 boolean found = false;
306 for (final HtmlAnchor a : page.getAnchors()) {
307 if (a.asNormalizedText().contains(text)) {
308 found = true;
309 break;
310 }
311 }
312 if (!found) {
313 final String msg = "Expected link containing text '" + text + "' was not found on the page.";
314 throw new AssertionError(msg);
315 }
316 }
317
318
319
320
321
322
323
324
325 public static void assertLinkNotPresentWithText(final HtmlPage page, final String text) {
326 boolean found = false;
327 for (final HtmlAnchor a : page.getAnchors()) {
328 if (a.asNormalizedText().contains(text)) {
329 found = true;
330 break;
331 }
332 }
333 if (found) {
334 final String msg = "Found unexpected link containing text '" + text + "' on the page.";
335 throw new AssertionError(msg);
336 }
337 }
338
339
340
341
342
343
344
345
346
347 public static void assertFormPresent(final HtmlPage page, final String name) {
348 try {
349 page.getFormByName(name);
350 }
351 catch (final ElementNotFoundException e) {
352 final String msg = "Expected form with name '" + name + "' was not found on the page.";
353 throw new AssertionError(msg, e);
354 }
355 }
356
357
358
359
360
361
362
363
364
365 public static void assertFormNotPresent(final HtmlPage page, final String name) {
366 try {
367 page.getFormByName(name);
368 }
369 catch (final ElementNotFoundException e) {
370 return;
371 }
372 final String msg = "Found unexpected form with name '" + name + "' on the page.";
373 throw new AssertionError(msg);
374 }
375
376
377
378
379
380
381
382
383
384
385 public static void assertInputPresent(final HtmlPage page, final String name) {
386 final String xpath = "//input[@name='" + name + "']";
387 final List<?> list = page.getByXPath(xpath);
388 if (list.isEmpty()) {
389 throw new AssertionError("Expected input element with name '" + name + "' was not found on the page.");
390 }
391 }
392
393
394
395
396
397
398
399
400
401 public static void assertInputNotPresent(final HtmlPage page, final String name) {
402 final String xpath = "//input[@name='" + name + "']";
403 final List<?> list = page.getByXPath(xpath);
404 if (!list.isEmpty()) {
405 throw new AssertionError("Found unexpected input element with name '" + name + "' on the page.");
406 }
407 }
408
409
410
411
412
413
414
415
416
417 public static void assertInputContainsValue(final HtmlPage page, final String name, final String value) {
418 final String xpath = "//input[@name='" + name + "']";
419 final List<?> list = page.getByXPath(xpath);
420 if (list.isEmpty()) {
421 throw new AssertionError("Expected input element with name '" + name + "' was not found on the page.");
422 }
423 final HtmlInput input = (HtmlInput) list.get(0);
424 final String s = input.getValue();
425 if (!s.equals(value)) {
426 throw new AssertionError("Input element '" + name + "' has value '" + s
427 + "' but expected '" + value + "'.");
428 }
429 }
430
431
432
433
434
435
436
437
438
439 public static void assertInputDoesNotContainValue(final HtmlPage page, final String name, final String value) {
440 final String xpath = "//input[@name='" + name + "']";
441 final List<?> list = page.getByXPath(xpath);
442 if (list.isEmpty()) {
443 throw new AssertionError("Expected input element with name '" + name + "' was not found on the page.");
444 }
445 final HtmlInput input = (HtmlInput) list.get(0);
446 final String s = input.getValue();
447 if (s.equals(value)) {
448 throw new AssertionError("Input element '" + name + "' has unexpected value '" + s + "'.");
449 }
450 }
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467 public static void assertAllTabIndexAttributesSet(final HtmlPage page) {
468 final List<String> tags =
469 Arrays.asList("a", "area", "button", "input", "object", "select", "textarea");
470
471 for (final String tag : tags) {
472 for (final HtmlElement element : page.getDocumentElement().getStaticElementsByTagName(tag)) {
473 final Short tabIndex = element.getTabIndex();
474 if (tabIndex == null || HtmlElement.TAB_INDEX_OUT_OF_BOUNDS.equals(tabIndex)) {
475 final String s = element.getAttributeDirect("tabindex");
476 throw new AssertionError("Invalid tabindex value '" + s + "' found on element.");
477 }
478 }
479 }
480 }
481
482
483
484
485
486
487
488
489
490
491
492 public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) {
493 final List<String> list = new ArrayList<>();
494 for (final HtmlElement element : page.getHtmlElementDescendants()) {
495 final String key = element.getAttributeDirect("accesskey");
496 if (key != null && !key.isEmpty()) {
497 if (list.contains(key)) {
498 throw new AssertionError("Duplicate access key '" + key + "' found on the page.");
499 }
500 list.add(key);
501 }
502 }
503 }
504
505
506
507
508
509
510
511
512 public static void assertAllIdAttributesUnique(final HtmlPage page) {
513 final List<String> list = new ArrayList<>();
514 for (final HtmlElement element : page.getHtmlElementDescendants()) {
515 final String id = element.getId();
516 if (id != null && !id.isEmpty()) {
517 if (list.contains(id)) {
518 throw new AssertionError("Duplicate element ID '" + id + "' found on the page.");
519 }
520 list.add(id);
521 }
522 }
523 }
524
525
526
527
528
529
530
531
532
533 public static void notNull(final String description, final Object object) {
534 if (object == null) {
535 throw new NullPointerException(description);
536 }
537 }
538 }