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.html;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * Tests for {@link HtmlUnorderedList}.
22   *
23   * @author Ahmed Ashour
24   * @author Marc Guillemot
25   * @author Ronald Brill
26   */
27  public class HtmlUnorderedList2Test extends SimpleWebTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      public void asNormalizedText() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html><head>\n"
36              + "</head>\n"
37              + "<body>\n"
38              + "  <ul id='foo'>\n"
39              + "    <li>first item</li>\n"
40              + "    <li>second item</li>\n"
41              + "something without li node\n"
42              + "    <li>third item</li>\n"
43              + "  </ul>\n"
44              + "</body></html>";
45  
46          final HtmlPage page = loadPage(html);
47          final HtmlElement node = page.getHtmlElementById("foo");
48          final String expectedText = "first item\nsecond item\nsomething without li node\nthird item";
49  
50          assertEquals(expectedText, node.asNormalizedText());
51          assertEquals(expectedText, page.asNormalizedText());
52      }
53  
54      /**
55       * Browsers ignore closing information in a self closing UL tag.
56       * @throws Exception if the test fails
57       */
58      @Test
59      public void asXml() throws Exception {
60          final String content = DOCTYPE_HTML
61              + "<html><head></head>\n"
62              + "<body>\n"
63              + "  <ul id='myNode'></ul>\n"
64              + "foo\n"
65              + "</form></body></html>";
66          final HtmlPage page = loadPage(content);
67          final HtmlElement element = page.getHtmlElementById("myNode");
68  
69          assertEquals("<ul id=\"myNode\"></ul>", element.asXml());
70          assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
71                  + "<html>\r\n"
72                  + "  <head/>\r\n"
73                  + "  <body>\r\n"
74                  + "    <ul id=\"myNode\"></ul>\n"
75                  + "foo\n"
76                  + "</body>\r\n"
77                  + "</html>", page.asXml());
78      }
79  }