1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import org.htmlunit.html.HtmlPage;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21
22
23
24
25
26
27 @RunWith(BrowserRunner.class)
28 public class WebAssertTest extends SimpleWebTestCase {
29
30
31
32
33 @Test
34 public void assertTitleEquals() throws Exception {
35 final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
36 final HtmlPage page = loadPage(html);
37
38 WebAssert.assertTitleEquals(page, "foo");
39
40 boolean caught = false;
41 try {
42 WebAssert.assertTitleEquals(page, "bar");
43 }
44 catch (final AssertionError e) {
45 caught = true;
46 }
47 assertTrue(caught);
48 }
49
50
51
52
53 @Test
54 public void assertTitleContains() throws Exception {
55 final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
56 final HtmlPage page = loadPage(html);
57
58 WebAssert.assertTitleContains(page, "o");
59
60 boolean caught = false;
61 try {
62 WebAssert.assertTitleContains(page, "a");
63 }
64 catch (final AssertionError e) {
65 caught = true;
66 }
67 assertTrue(caught);
68 }
69
70
71
72
73 @Test
74 public void assertTitleMatches() throws Exception {
75 final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
76 final HtmlPage page = loadPage(html);
77
78 WebAssert.assertTitleMatches(page, "f..");
79
80 boolean caught = false;
81 try {
82 WebAssert.assertTitleMatches(page, "b..");
83 }
84 catch (final AssertionError e) {
85 caught = true;
86 }
87 assertTrue(caught);
88 }
89
90
91
92
93 @Test
94 public void assertElementPresent() throws Exception {
95 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
96 final HtmlPage page = loadPage(html);
97
98 WebAssert.assertElementPresent(page, "a");
99
100 boolean caught = false;
101 try {
102 WebAssert.assertElementPresent(page, "b");
103 }
104 catch (final AssertionError e) {
105 caught = true;
106 }
107 assertTrue(caught);
108 }
109
110
111
112
113 @Test
114 public void assertElementPresentByXPath() throws Exception {
115 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
116 final HtmlPage page = loadPage(html);
117
118 WebAssert.assertElementPresentByXPath(page, "html/body/div");
119
120 boolean caught = false;
121 try {
122 WebAssert.assertElementPresentByXPath(page, "ul");
123 }
124 catch (final AssertionError e) {
125 caught = true;
126 }
127 assertTrue(caught);
128 }
129
130
131
132
133 @Test
134 public void assertElementNotPresent() throws Exception {
135 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
136 final HtmlPage page = loadPage(html);
137
138 WebAssert.assertElementNotPresent(page, "b");
139
140 boolean caught = false;
141 try {
142 WebAssert.assertElementNotPresent(page, "a");
143 }
144 catch (final AssertionError e) {
145 caught = true;
146 }
147 assertTrue(caught);
148 }
149
150
151
152
153 @Test
154 public void assertElementNotPresentByXPath() throws Exception {
155 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
156 final HtmlPage page = loadPage(html);
157
158 WebAssert.assertElementNotPresentByXPath(page, "ul");
159
160 boolean caught = false;
161 try {
162 WebAssert.assertElementNotPresentByXPath(page, "html/body/div");
163 }
164 catch (final AssertionError e) {
165 caught = true;
166 }
167 assertTrue(caught);
168 }
169
170
171
172
173 @Test
174 public void assertTextPresent() throws Exception {
175 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
176 final HtmlPage page = loadPage(html);
177
178 WebAssert.assertTextPresent(page, "bar");
179
180 boolean caught = false;
181 try {
182 WebAssert.assertTextPresent(page, "baz");
183 }
184 catch (final AssertionError e) {
185 caught = true;
186 }
187 assertTrue(caught);
188 }
189
190
191
192
193 @Test
194 public void assertTextPresentInElement() throws Exception {
195 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
196 final HtmlPage page = loadPage(html);
197
198 WebAssert.assertTextPresentInElement(page, "bar", "a");
199
200 boolean caught = false;
201 try {
202 WebAssert.assertTextPresentInElement(page, "baz", "a");
203 }
204 catch (final AssertionError e) {
205 caught = true;
206 }
207 assertTrue(caught);
208
209 caught = false;
210 try {
211 WebAssert.assertTextPresentInElement(page, "bar", "b");
212 }
213 catch (final AssertionError e) {
214 caught = true;
215 }
216 assertTrue(caught);
217 }
218
219
220
221
222 @Test
223 public void assertTextNotPresent() throws Exception {
224 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
225 final HtmlPage page = loadPage(html);
226
227 WebAssert.assertTextNotPresent(page, "baz");
228
229 boolean caught = false;
230 try {
231 WebAssert.assertTextNotPresent(page, "bar");
232 }
233 catch (final AssertionError e) {
234 caught = true;
235 }
236 assertTrue(caught);
237 }
238
239
240
241
242 @Test
243 public void assertTextNotPresentInElement() throws Exception {
244 final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
245 final HtmlPage page = loadPage(html);
246
247 WebAssert.assertTextNotPresentInElement(page, "baz", "a");
248
249 boolean caught = false;
250 try {
251 WebAssert.assertTextNotPresentInElement(page, "bar", "a");
252 }
253 catch (final AssertionError e) {
254 caught = true;
255 }
256 assertTrue(caught);
257
258 caught = false;
259 try {
260 WebAssert.assertTextNotPresentInElement(page, "bar", "b");
261 }
262 catch (final AssertionError e) {
263 caught = true;
264 }
265 assertTrue(caught);
266 }
267
268
269
270
271 @Test
272 public void assertLinkPresent() throws Exception {
273 final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
274 final HtmlPage page = loadPage(html);
275
276 WebAssert.assertLinkPresent(page, "x");
277
278 boolean caught = false;
279 try {
280 WebAssert.assertLinkPresent(page, "z");
281 }
282 catch (final AssertionError e) {
283 caught = true;
284 }
285 assertTrue(caught);
286 }
287
288
289
290
291 @Test
292 public void assertLinkNotPresent() throws Exception {
293 final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
294 final HtmlPage page = loadPage(html);
295
296 WebAssert.assertLinkNotPresent(page, "z");
297
298 boolean caught = false;
299 try {
300 WebAssert.assertLinkNotPresent(page, "x");
301 }
302 catch (final AssertionError e) {
303 caught = true;
304 }
305 assertTrue(caught);
306 }
307
308
309
310
311 @Test
312 public void assertLinkPresentWithText() throws Exception {
313 final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
314 final HtmlPage page = loadPage(html);
315
316 WebAssert.assertLinkPresentWithText(page, "r");
317
318 boolean caught = false;
319 try {
320 WebAssert.assertLinkPresentWithText(page, "x");
321 }
322 catch (final AssertionError e) {
323 caught = true;
324 }
325 assertTrue(caught);
326 }
327
328
329
330
331 @Test
332 public void assertLinkNotPresentWithText() throws Exception {
333 final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
334 final HtmlPage page = loadPage(html);
335
336 WebAssert.assertLinkNotPresentWithText(page, "x");
337
338 boolean caught = false;
339 try {
340 WebAssert.assertLinkNotPresentWithText(page, "r");
341 }
342 catch (final AssertionError e) {
343 caught = true;
344 }
345 assertTrue(caught);
346 }
347
348
349
350
351 @Test
352 public void assertFormPresent() throws Exception {
353 final String html = DOCTYPE_HTML + "<html><body><form name='f'>bar</form></body></html>";
354 final HtmlPage page = loadPage(html);
355
356 WebAssert.assertFormPresent(page, "f");
357
358 boolean caught = false;
359 try {
360 WebAssert.assertFormPresent(page, "x");
361 }
362 catch (final AssertionError e) {
363 caught = true;
364 }
365 assertTrue(caught);
366 }
367
368
369
370
371 @Test
372 public void assertFormNotPresent() throws Exception {
373 final String html = DOCTYPE_HTML + "<html><body><form name='f'>bar</form></body></html>";
374 final HtmlPage page = loadPage(html);
375
376 WebAssert.assertFormNotPresent(page, "x");
377
378 boolean caught = false;
379 try {
380 WebAssert.assertFormNotPresent(page, "f");
381 }
382 catch (final AssertionError e) {
383 caught = true;
384 }
385 assertTrue(caught);
386 }
387
388
389
390
391 @Test
392 public void assertInputPresent() throws Exception {
393 final String html = DOCTYPE_HTML
394 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
395 final HtmlPage page = loadPage(html);
396
397 WebAssert.assertInputPresent(page, "i");
398
399 boolean caught = false;
400 try {
401 WebAssert.assertInputPresent(page, "q");
402 }
403 catch (final AssertionError e) {
404 caught = true;
405 }
406 assertTrue(caught);
407 }
408
409
410
411
412 @Test
413 public void assertInputNotPresent() throws Exception {
414 final String html = DOCTYPE_HTML
415 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
416 final HtmlPage page = loadPage(html);
417
418 WebAssert.assertInputNotPresent(page, "q");
419
420 boolean caught = false;
421 try {
422 WebAssert.assertInputNotPresent(page, "i");
423 }
424 catch (final AssertionError e) {
425 caught = true;
426 }
427 assertTrue(caught);
428 }
429
430
431
432
433 @Test
434 public void assertInputContainsValue() throws Exception {
435 final String html = DOCTYPE_HTML
436 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
437 final HtmlPage page = loadPage(html);
438
439 WebAssert.assertInputContainsValue(page, "i", "x");
440
441 boolean caught = false;
442 try {
443 WebAssert.assertInputContainsValue(page, "i", "z");
444 }
445 catch (final AssertionError e) {
446 caught = true;
447 }
448 assertTrue(caught);
449
450 caught = false;
451 try {
452 WebAssert.assertInputContainsValue(page, "q", "x");
453 }
454 catch (final AssertionError e) {
455 caught = true;
456 }
457 assertTrue(caught);
458 }
459
460
461
462
463 @Test
464 public void assertInputDoesNotContainValue() throws Exception {
465 final String html = DOCTYPE_HTML
466 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
467 final HtmlPage page = loadPage(html);
468
469 WebAssert.assertInputDoesNotContainValue(page, "i", "z");
470
471 boolean caught = false;
472 try {
473 WebAssert.assertInputDoesNotContainValue(page, "i", "x");
474 }
475 catch (final AssertionError e) {
476 caught = true;
477 }
478 assertTrue(caught);
479
480 caught = false;
481 try {
482 WebAssert.assertInputDoesNotContainValue(page, "q", "x");
483 }
484 catch (final AssertionError e) {
485 caught = true;
486 }
487 assertTrue(caught);
488 }
489
490
491
492
493 @Test
494 public void assertAllTabIndexAttributesSet() throws Exception {
495 final String html1 = DOCTYPE_HTML + "<html><body><a href='#' tabindex='1'>foo</a></body></html>";
496 final HtmlPage page1 = loadPage(html1);
497
498 WebAssert.assertAllTabIndexAttributesSet(page1);
499
500 final String html2 = DOCTYPE_HTML + "<html><body><a href='#'>foo</a></body></html>";
501 final HtmlPage page2 = loadPage(html2);
502
503 boolean caught = false;
504 try {
505 WebAssert.assertAllTabIndexAttributesSet(page2);
506 }
507 catch (final AssertionError e) {
508 caught = true;
509 }
510 assertTrue(caught);
511
512 final String html3 = DOCTYPE_HTML + "<html><body><a href='#' tabindex='x'>foo</a></body></html>";
513 final HtmlPage page3 = loadPage(html3);
514
515 caught = false;
516 try {
517 WebAssert.assertAllTabIndexAttributesSet(page3);
518 }
519 catch (final AssertionError e) {
520 caught = true;
521 }
522 assertTrue(caught);
523 }
524
525
526
527
528 @Test
529 public void assertAllAccessKeyAttributesUnique() throws Exception {
530 final String html1 = DOCTYPE_HTML + "<html><body><a accesskey='k'>foo</a></body></html>";
531 final HtmlPage page1 = loadPage(html1);
532
533 WebAssert.assertAllAccessKeyAttributesUnique(page1);
534
535 final String html2 = DOCTYPE_HTML
536 + "<html><body><a accesskey='k'>foo</a><a accesskey='k'>bar</a></body></html>";
537 final HtmlPage page2 = loadPage(html2);
538
539 boolean caught = false;
540 try {
541 WebAssert.assertAllAccessKeyAttributesUnique(page2);
542 }
543 catch (final AssertionError e) {
544 caught = true;
545 }
546 assertTrue(caught);
547 }
548
549
550
551
552 @Test
553 public void assertAllIdAttributesUnique() throws Exception {
554 final String html1 = DOCTYPE_HTML + "<html><body><a id='k'>foo</a></body></html>";
555 final HtmlPage page1 = loadPage(html1);
556
557 WebAssert.assertAllIdAttributesUnique(page1);
558
559 final String html2 = DOCTYPE_HTML + "<html><body><a id='k'>foo</a><a id='k'>bar</a></body></html>";
560 final HtmlPage page2 = loadPage(html2);
561
562 boolean caught = false;
563 try {
564 WebAssert.assertAllIdAttributesUnique(page2);
565 }
566 catch (final AssertionError e) {
567 caught = true;
568 }
569 assertTrue(caught);
570 }
571 }