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 static org.junit.jupiter.api.Assertions.assertThrows;
18  
19  import org.htmlunit.html.HtmlPage;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link WebAssert}.
24   *
25   * @author Daniel Gredler
26   * @author Ronald Brill
27   */
28  public class WebAssertTest extends SimpleWebTestCase {
29  
30      /**
31       * @throws Exception if an error occurs
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          final AssertionError error = assertThrows(AssertionError.class, () -> {
41              WebAssert.assertTitleEquals(page, "bar");
42          });
43          assertEquals("Page title 'foo' does not match expected title 'bar'.", error.getMessage());
44      }
45  
46      /**
47       * @throws Exception if an error occurs
48       */
49      @Test
50      public void assertTitleContains() throws Exception {
51          final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
52          final HtmlPage page = loadPage(html);
53  
54          WebAssert.assertTitleContains(page, "o");
55  
56          final AssertionError error = assertThrows(AssertionError.class, () -> {
57              WebAssert.assertTitleContains(page, "a");
58          });
59          assertEquals("Page title 'foo' does not contain the expected substring 'a'.", error.getMessage());
60      }
61  
62      /**
63       * @throws Exception if an error occurs
64       */
65      @Test
66      public void assertTitleMatches() throws Exception {
67          final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
68          final HtmlPage page = loadPage(html);
69  
70          WebAssert.assertTitleMatches(page, "f..");
71  
72          final AssertionError error = assertThrows(AssertionError.class, () -> {
73              WebAssert.assertTitleMatches(page, "b..");
74          });
75          assertEquals("Page title 'foo' does not match the expected regular expression 'b..'.", error.getMessage());
76      }
77  
78      /**
79       * @throws Exception if an error occurs
80       */
81      @Test
82      public void assertElementPresent() throws Exception {
83          final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
84          final HtmlPage page = loadPage(html);
85  
86          WebAssert.assertElementPresent(page, "a");
87  
88          final AssertionError error = assertThrows(AssertionError.class, () -> {
89              WebAssert.assertElementPresent(page, "b");
90          });
91          assertEquals("Expected element with ID 'b' was not found on the page.", error.getMessage());
92      }
93  
94      /**
95       * @throws Exception if an error occurs
96       */
97      @Test
98      public void assertElementPresentByXPath() throws Exception {
99          final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
100         final HtmlPage page = loadPage(html);
101 
102         WebAssert.assertElementPresentByXPath(page, "html/body/div");
103 
104         final AssertionError error = assertThrows(AssertionError.class, () -> {
105             WebAssert.assertElementPresentByXPath(page, "ul");
106         });
107         assertEquals("No elements found matching the XPath expression 'ul'.", error.getMessage());
108     }
109 
110     /**
111      * @throws Exception if an error occurs
112      */
113     @Test
114     public void assertElementNotPresent() 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.assertElementNotPresent(page, "b");
119 
120         final AssertionError error = assertThrows(AssertionError.class, () -> {
121             WebAssert.assertElementNotPresent(page, "a");
122         });
123         assertEquals("Found unexpected element with ID 'a' on the page.", error.getMessage());
124     }
125 
126     /**
127      * @throws Exception if an error occurs
128      */
129     @Test
130     public void assertElementNotPresentByXPath() throws Exception {
131         final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
132         final HtmlPage page = loadPage(html);
133 
134         WebAssert.assertElementNotPresentByXPath(page, "ul");
135 
136         final AssertionError error = assertThrows(AssertionError.class, () -> {
137             WebAssert.assertElementNotPresentByXPath(page, "html/body/div");
138         });
139         assertEquals("Found 1 unexpected element(s) matching the XPath expression 'html/body/div'.",
140                 error.getMessage());
141     }
142 
143     /**
144      * @throws Exception if an error occurs
145      */
146     @Test
147     public void assertTextPresent() throws Exception {
148         final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
149         final HtmlPage page = loadPage(html);
150 
151         WebAssert.assertTextPresent(page, "bar");
152 
153         final AssertionError error = assertThrows(AssertionError.class, () -> {
154             WebAssert.assertTextPresent(page, "baz");
155         });
156         assertEquals("Expected text 'baz' was not found on the page.", error.getMessage());
157     }
158 
159     /**
160      * @throws Exception if an error occurs
161      */
162     @Test
163     public void assertTextPresentInElement() throws Exception {
164         final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
165         final HtmlPage page = loadPage(html);
166 
167         WebAssert.assertTextPresentInElement(page, "bar", "a");
168 
169         AssertionError error = assertThrows(AssertionError.class, () -> {
170             WebAssert.assertTextPresentInElement(page, "baz", "a");
171         });
172         assertEquals("Element with ID 'a' does not contain the expected text 'baz'.", error.getMessage());
173 
174         error = assertThrows(AssertionError.class, () -> {
175             WebAssert.assertTextPresentInElement(page, "bar", "b");
176         });
177         assertEquals("Cannot verify text content: element with ID 'b' was not found on the page.", error.getMessage());
178     }
179 
180     /**
181      * @throws Exception if an error occurs
182      */
183     @Test
184     public void assertTextNotPresent() throws Exception {
185         final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
186         final HtmlPage page = loadPage(html);
187 
188         WebAssert.assertTextNotPresent(page, "baz");
189 
190         final AssertionError error = assertThrows(AssertionError.class, () -> {
191             WebAssert.assertTextNotPresent(page, "bar");
192         });
193         assertEquals("Found unexpected text 'bar' on the page.", error.getMessage());
194     }
195 
196     /**
197      * @throws Exception if an error occurs
198      */
199     @Test
200     public void assertTextNotPresentInElement() throws Exception {
201         final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
202         final HtmlPage page = loadPage(html);
203 
204         WebAssert.assertTextNotPresentInElement(page, "baz", "a");
205 
206         AssertionError error = assertThrows(AssertionError.class, () -> {
207             WebAssert.assertTextNotPresentInElement(page, "bar", "a");
208         });
209         assertEquals("Element with ID 'a' contains unexpected text 'bar'.", error.getMessage());
210 
211         error = assertThrows(AssertionError.class, () -> {
212             WebAssert.assertTextNotPresentInElement(page, "bar", "b");
213         });
214         assertEquals("Cannot verify text content: element with ID 'b' was not found on the page.", error.getMessage());
215     }
216 
217     /**
218      * @throws Exception if an error occurs
219      */
220     @Test
221     public void assertLinkPresent() throws Exception {
222         final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
223         final HtmlPage page = loadPage(html);
224 
225         WebAssert.assertLinkPresent(page, "x");
226 
227         final AssertionError error = assertThrows(AssertionError.class, () -> {
228             WebAssert.assertLinkPresent(page, "z");
229         });
230         assertEquals("Expected link with ID 'z' was not found on the page.", error.getMessage());
231     }
232 
233     /**
234      * @throws Exception if an error occurs
235      */
236     @Test
237     public void assertLinkNotPresent() throws Exception {
238         final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
239         final HtmlPage page = loadPage(html);
240 
241         WebAssert.assertLinkNotPresent(page, "z");
242 
243         final AssertionError error = assertThrows(AssertionError.class, () -> {
244             WebAssert.assertLinkNotPresent(page, "x");
245         });
246         assertEquals("Found unexpected link with ID 'x' on the page.", error.getMessage());
247     }
248 
249     /**
250      * @throws Exception if an error occurs
251      */
252     @Test
253     public void assertLinkPresentWithText() throws Exception {
254         final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
255         final HtmlPage page = loadPage(html);
256 
257         WebAssert.assertLinkPresentWithText(page, "r");
258 
259         final AssertionError error = assertThrows(AssertionError.class, () -> {
260             WebAssert.assertLinkPresentWithText(page, "x");
261         });
262         assertEquals("Expected link containing text 'x' was not found on the page.", error.getMessage());
263     }
264 
265     /**
266      * @throws Exception if an error occurs
267      */
268     @Test
269     public void assertLinkNotPresentWithText() 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.assertLinkNotPresentWithText(page, "x");
274 
275         final AssertionError error = assertThrows(AssertionError.class, () -> {
276             WebAssert.assertLinkNotPresentWithText(page, "r");
277         });
278         assertEquals("Found unexpected link containing text 'r' on the page.", error.getMessage());
279     }
280 
281     /**
282      * @throws Exception if an error occurs
283      */
284     @Test
285     public void assertFormPresent() throws Exception {
286         final String html = DOCTYPE_HTML + "<html><body><form name='f'>bar</form></body></html>";
287         final HtmlPage page = loadPage(html);
288 
289         WebAssert.assertFormPresent(page, "f");
290 
291         final AssertionError error = assertThrows(AssertionError.class, () -> {
292             WebAssert.assertFormPresent(page, "x");
293         });
294         assertEquals("Expected form with name 'x' was not found on the page.", error.getMessage());
295     }
296 
297     /**
298      * @throws Exception if an error occurs
299      */
300     @Test
301     public void assertFormNotPresent() throws Exception {
302         final String html = DOCTYPE_HTML + "<html><body><form name='f'>bar</form></body></html>";
303         final HtmlPage page = loadPage(html);
304 
305         WebAssert.assertFormNotPresent(page, "x");
306 
307         final AssertionError error = assertThrows(AssertionError.class, () -> {
308             WebAssert.assertFormNotPresent(page, "f");
309         });
310         assertEquals("Found unexpected form with name 'f' on the page.", error.getMessage());
311     }
312 
313     /**
314      * @throws Exception if an error occurs
315      */
316     @Test
317     public void assertInputPresent() throws Exception {
318         final String html = DOCTYPE_HTML
319                 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
320         final HtmlPage page = loadPage(html);
321 
322         WebAssert.assertInputPresent(page, "i");
323 
324         final AssertionError error = assertThrows(AssertionError.class, () -> {
325             WebAssert.assertInputPresent(page, "q");
326         });
327         assertEquals("Expected input element with name 'q' was not found on the page.", error.getMessage());
328     }
329 
330     /**
331      * @throws Exception if an error occurs
332      */
333     @Test
334     public void assertInputNotPresent() throws Exception {
335         final String html = DOCTYPE_HTML
336                 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
337         final HtmlPage page = loadPage(html);
338 
339         WebAssert.assertInputNotPresent(page, "q");
340 
341         final AssertionError error = assertThrows(AssertionError.class, () -> {
342             WebAssert.assertInputNotPresent(page, "i");
343         });
344         assertEquals("Found unexpected input element with name 'i' on the page.", error.getMessage());
345     }
346 
347     /**
348      * @throws Exception if an error occurs
349      */
350     @Test
351     public void assertInputContainsValue() throws Exception {
352         final String html = DOCTYPE_HTML
353                 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
354         final HtmlPage page = loadPage(html);
355 
356         WebAssert.assertInputContainsValue(page, "i", "x");
357 
358         AssertionError error = assertThrows(AssertionError.class, () -> {
359             WebAssert.assertInputContainsValue(page, "i", "z");
360         });
361         assertEquals("Input element 'i' has value 'x' but expected 'z'.", error.getMessage());
362 
363         error = assertThrows(AssertionError.class, () -> {
364             WebAssert.assertInputContainsValue(page, "q", "x");
365         });
366         assertEquals("Expected input element with name 'q' was not found on the page.", error.getMessage());
367     }
368 
369     /**
370      * @throws Exception if an error occurs
371      */
372     @Test
373     public void assertInputDoesNotContainValue() throws Exception {
374         final String html = DOCTYPE_HTML
375                 + "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
376         final HtmlPage page = loadPage(html);
377 
378         WebAssert.assertInputDoesNotContainValue(page, "i", "z");
379 
380         AssertionError error = assertThrows(AssertionError.class, () -> {
381             WebAssert.assertInputDoesNotContainValue(page, "i", "x");
382         });
383         assertEquals("Input element 'i' has unexpected value 'x'.", error.getMessage());
384 
385         error = assertThrows(AssertionError.class, () -> {
386             WebAssert.assertInputDoesNotContainValue(page, "q", "x");
387         });
388         assertEquals("Expected input element with name 'q' was not found on the page.", error.getMessage());
389     }
390 
391     /**
392      * @throws Exception if an error occurs
393      */
394     @Test
395     public void assertAllTabIndexAttributesSet() throws Exception {
396         final String html1 = DOCTYPE_HTML + "<html><body><a href='#' tabindex='1'>foo</a></body></html>";
397         final HtmlPage page1 = loadPage(html1);
398 
399         WebAssert.assertAllTabIndexAttributesSet(page1);
400 
401         final String html2 = DOCTYPE_HTML + "<html><body><a href='#'>foo</a></body></html>";
402         final HtmlPage page2 = loadPage(html2);
403 
404         AssertionError error = assertThrows(AssertionError.class, () -> {
405             WebAssert.assertAllTabIndexAttributesSet(page2);
406         });
407         assertEquals("Invalid tabindex value '' found on element.", error.getMessage());
408 
409         final String html3 = DOCTYPE_HTML + "<html><body><a href='#' tabindex='x'>foo</a></body></html>";
410         final HtmlPage page3 = loadPage(html3);
411 
412         error = assertThrows(AssertionError.class, () -> {
413             WebAssert.assertAllTabIndexAttributesSet(page3);
414         });
415         assertEquals("Invalid tabindex value 'x' found on element.", error.getMessage());
416     }
417 
418     /**
419      * @throws Exception if an error occurs
420      */
421     @Test
422     public void assertAllAccessKeyAttributesUnique() throws Exception {
423         final String html1 = DOCTYPE_HTML + "<html><body><a accesskey='k'>foo</a></body></html>";
424         final HtmlPage page1 = loadPage(html1);
425 
426         WebAssert.assertAllAccessKeyAttributesUnique(page1);
427 
428         final String html2 = DOCTYPE_HTML
429                 + "<html><body><a accesskey='k'>foo</a><a accesskey='k'>bar</a></body></html>";
430         final HtmlPage page2 = loadPage(html2);
431 
432         final AssertionError error = assertThrows(AssertionError.class, () -> {
433             WebAssert.assertAllAccessKeyAttributesUnique(page2);
434         });
435         assertEquals("Duplicate access key 'k' found on the page.", error.getMessage());
436     }
437 
438     /**
439      * @throws Exception if an error occurs
440      */
441     @Test
442     public void assertAllIdAttributesUnique() throws Exception {
443         final String html1 = DOCTYPE_HTML + "<html><body><a id='k'>foo</a></body></html>";
444         final HtmlPage page1 = loadPage(html1);
445 
446         WebAssert.assertAllIdAttributesUnique(page1);
447 
448         final String html2 = DOCTYPE_HTML + "<html><body><a id='k'>foo</a><a id='k'>bar</a></body></html>";
449         final HtmlPage page2 = loadPage(html2);
450 
451         final AssertionError error = assertThrows(AssertionError.class, () -> {
452             WebAssert.assertAllIdAttributesUnique(page2);
453         });
454         assertEquals("Duplicate element ID 'k' found on the page.", error.getMessage());
455     }
456 
457     /**
458      * @throws Exception if an error occurs
459      */
460     @Test
461     public void assertTitleEqualsNullHandling() throws Exception {
462         final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
463         final HtmlPage page = loadPage(html);
464 
465         assertThrows(NullPointerException.class, () -> {
466             WebAssert.assertTitleEquals(null, "title");
467         }, "Should throw NullPointerException when page is null");
468 
469         assertThrows(NullPointerException.class, () -> {
470             WebAssert.assertTitleEquals(page, null);
471         }, "Should throw NullPointerException when title is null");
472     }
473 }