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 java.util.Iterator;
18  import java.util.ListIterator;
19  
20  import org.htmlunit.SimpleWebTestCase;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link DomNodeList}.
25   *
26   * @author <a href="mailto:tom.anderson@univ.oxon.org">Tom Anderson</a>
27   * @author Frank Danek
28   * @author Ronald Brill
29   */
30  public class DomNodeListTest extends SimpleWebTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      public void getElementsByTagName() throws Exception {
37          final String firstContent = DOCTYPE_HTML
38              + "<html><head><title>First</title></head>\n"
39              + "<body>\n"
40              + "<form><input type='button' name='button1' value='pushme'></form>\n"
41              + "<div>a</div> <div>b</div> <div>c</div>\n"
42              + "</body></html>";
43  
44          final HtmlPage page = loadPage(firstContent);
45  
46          final DomNodeList<DomElement> divs = page.getElementsByTagName("div");
47  
48          assertEquals(3, divs.getLength());
49          validateDomNodeList(divs);
50  
51          final HtmlDivision newDiv = new HtmlDivision(HtmlDivision.TAG_NAME, page, null);
52          page.getBody().appendChild(newDiv);
53          assertEquals(4, divs.getLength());
54          validateDomNodeList(divs);
55      }
56  
57      /**
58       * @throws Exception if the test fails
59       */
60      @Test
61      public void getChildNodes() throws Exception {
62          final String firstContent = DOCTYPE_HTML
63              + "<html><head><title>First</title></head>\n"
64              + "<body>\n"
65              + "<form><input type='button' name='button1' value='pushme'></form>\n"
66              + "<div>a</div> <div>b</div> <div>c</div>\n"
67              + "</body></html>";
68  
69          final HtmlPage page = loadPage(firstContent);
70          final DomNodeList<DomNode> bodyChildren = page.getBody().getChildNodes();
71          validateDomNodeList(bodyChildren);
72      }
73  
74      private <E extends DomNode> void validateDomNodeList(final DomNodeList<E> nodes) {
75          assertEquals(nodes.getLength(), nodes.size());
76          final Iterator<E> nodesIterator = nodes.iterator();
77          for (int i = 0; i < nodes.getLength(); i++) {
78              assertEquals(nodes.item(i), nodes.get(i));
79              assertEquals(nodes.item(i), nodesIterator.next());
80              assertEquals(i, nodes.indexOf(nodes.item(i)));
81          }
82          assertEquals(false, nodesIterator.hasNext());
83          final ListIterator<E> nodesListIterator = nodes.listIterator();
84          assertEquals(nodes.item(0), nodesListIterator.next());
85          assertEquals(nodes.item(1), nodesListIterator.next());
86          assertEquals(nodes.item(1), nodesListIterator.previous());
87      }
88  
89  }