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