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