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.doc;
16  
17  import java.util.Iterator;
18  import java.util.List;
19  
20  import org.htmlunit.WebClient;
21  import org.htmlunit.WebServerTestCase;
22  import org.htmlunit.html.DomElement;
23  import org.htmlunit.html.DomNode;
24  import org.htmlunit.html.DomNodeList;
25  import org.htmlunit.html.HtmlAnchor;
26  import org.htmlunit.html.HtmlBody;
27  import org.htmlunit.html.HtmlDivision;
28  import org.htmlunit.html.HtmlForm;
29  import org.htmlunit.html.HtmlPage;
30  import org.htmlunit.html.HtmlSubmitInput;
31  import org.htmlunit.html.HtmlTextInput;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests for the sample code from the documentation to make sure
37   * we adapt the docu or do not break the samples.
38   *
39   * @author Ronald Brill
40   */
41  public class GettingStartedTest extends WebServerTestCase {
42  
43      /**
44       * @throws Exception if an error occurs
45       */
46      @Test
47      public void homePage() throws Exception {
48          try (WebClient webClient = new WebClient()) {
49              final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
50              Assertions.assertEquals("HtmlUnit – Welcome to HtmlUnit", page.getTitleText());
51  
52              final String pageAsXml = page.asXml();
53              Assertions.assertTrue(pageAsXml.contains("<body class=\"topBarDisabled\">"));
54  
55              final String pageAsText = page.asNormalizedText();
56              Assertions.assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
57          }
58      }
59  
60      /**
61       * @throws Exception if an error occurs
62       */
63      @Test
64      public void xpath() throws Exception {
65          try (WebClient webClient = new WebClient()) {
66              final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
67  
68              //get list of all divs
69              final List<?> divs = page.getByXPath("//div");
70  
71              //get div which has a 'id' attribute of 'banner'
72              final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@id='banner']").get(0);
73          }
74      }
75  
76      /**
77       * @throws Exception if an error occurs
78       */
79      @Test
80      public void cssSelector() throws Exception {
81          try (WebClient webClient = new WebClient()) {
82              final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
83  
84              //get list of all divs
85              final DomNodeList<DomNode> divs = page.querySelectorAll("div");
86              for (final DomNode div : divs) {
87                  // ....
88              }
89  
90              //get div which has the id 'breadcrumbs'
91              final DomNode div = page.querySelector("div#breadcrumbs");
92          }
93      }
94  
95      /**
96       * @throws Exception if an error occurs
97       */
98      private void submittingForm() throws Exception {
99          try (WebClient webClient = new WebClient()) {
100 
101             // Get the first page
102             final HtmlPage page = webClient.getPage("http://some_url");
103 
104             // Get the form that we are dealing with and within that form,
105             // find the submit button and the field that we want to change.
106             final HtmlForm form = page.getFormByName("myform");
107 
108             final HtmlSubmitInput button = form.getInputByName("submitbutton");
109             final HtmlTextInput textField = form.getInputByName("userid");
110 
111             // Change the value of the text field
112             textField.type("root");
113 
114             // Now submit the form by clicking the button and get back the second page.
115             final HtmlPage secondPage = button.click();
116         }
117     }
118 
119     /**
120      * @throws Exception if an error occurs
121      */
122     @Test
123     public void extractTextToc() throws Exception {
124         try (WebClient webClient = new WebClient()) {
125             final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
126 
127             final DomNode sponsoringDiv = page.querySelector("#bodyColumn > section:nth-child(1) > div:nth-child(2)");
128 
129             // A normalized textual representation of this element that represents
130             // what would be visible to the user if this page was shown in a web browser.
131             // Whitespace is normalized like in the browser and block tags are separated by '\n'.
132             final String content = sponsoringDiv.asNormalizedText();
133         }
134     }
135 
136     /**
137      * @throws Exception if an error occurs
138      */
139     @Test
140     public void extractTextFromBody() throws Exception {
141         try (WebClient webClient = new WebClient()) {
142             final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
143 
144             final HtmlBody body = page.getBody();
145 
146             // A normalized textual representation of this element that represents
147             // what would be visible to the user if this page was shown in a web browser.
148             // Whitespace is normalized like in the browser and block tags are separated by '\n'.
149             final String bodyContent = body.asNormalizedText();
150         }
151     }
152 
153     /**
154      * @throws Exception if an error occurs
155      */
156     private void getElements() throws Exception {
157         try (WebClient webClient = new WebClient()) {
158             final HtmlPage page = webClient.getPage("http://some_url");
159 
160             final HtmlDivision div = page.getHtmlElementById("some_div_id");
161             final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
162         }
163     }
164 
165     /**
166      * @throws Exception if an error occurs
167      */
168     private void getElements2() throws Exception {
169         try (WebClient webClient = new WebClient()) {
170             final HtmlPage page = webClient.getPage("http://some_url");
171 
172             final DomNodeList<DomElement> inputs = page.getElementsByTagName("input");
173             final Iterator<DomElement> nodesIterator = inputs.iterator();
174             // now iterate
175         }
176     }
177 }