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.TimeZone;
18  
19  import org.htmlunit.BrowserVersion;
20  import org.htmlunit.WebClient;
21  import org.htmlunit.WebServerTestCase;
22  import org.htmlunit.html.HtmlPage;
23  import org.junit.jupiter.api.Test;
24  
25  
26  /**
27   * Tests for the sample code from the documentation to make sure
28   * we adapt the docu or do not break the samples.
29   *
30   * @author Ronald Brill
31   */
32  public class WebClientTest extends WebServerTestCase {
33  
34      /**
35       * @throws Exception if an error occurs
36       */
37      @Test
38      public void homePage_Firefox() throws Exception {
39          try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
40              // disable javascript
41              webClient.getOptions().setJavaScriptEnabled(false);
42              // disable css support
43              webClient.getOptions().setCssEnabled(false);
44  
45              final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
46              assertEquals("HtmlUnit – Welcome to HtmlUnit", page.getTitleText());
47          }
48      }
49  
50      /**
51       * @throws Exception if an error occurs
52       */
53      @Test
54      public void homePage_Firefox2() throws Exception {
55          try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
56              // proceed with the js execution on unhandled js errors
57              webClient.getOptions().setThrowExceptionOnScriptError(false);
58  
59              final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
60              assertEquals("HtmlUnit – Welcome to HtmlUnit", page.getTitleText());
61          }
62      }
63  
64      /**
65       * @throws Exception if an error occurs
66       */
67      @Test
68      public void changeBrowserLanguage() throws Exception {
69          final BrowserVersion.BrowserVersionBuilder builder =
70                          new BrowserVersion.BrowserVersionBuilder(BrowserVersion.FIREFOX);
71  
72          builder.setSystemTimezone(TimeZone.getTimeZone("Europe/Berlin"));
73          builder.setBrowserLanguage("de-DE");
74          builder.setAcceptLanguageHeader("de-DE,de");
75  
76          final BrowserVersion germanFirefox = builder.build();
77          try (WebClient webClient = new WebClient(germanFirefox)) {
78              final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
79              assertEquals("HtmlUnit – Welcome to HtmlUnit", page.getTitleText());
80          }
81      }
82  
83      /**
84       * @throws Exception if an error occurs
85       */
86      @Test
87      public void changeUserAgent() throws Exception {
88          final BrowserVersion.BrowserVersionBuilder builder =
89                          new BrowserVersion.BrowserVersionBuilder(BrowserVersion.FIREFOX);
90  
91          builder.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) "
92                  + "AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/128.0 Mobile/15E148 Safari/605.1.15");
93  
94          final BrowserVersion iosFirefox = builder.build();
95          try (WebClient webClient = new WebClient(iosFirefox)) {
96              final HtmlPage page = webClient.getPage("https://www.htmlunit.org/");
97              assertEquals("HtmlUnit – Welcome to HtmlUnit", page.getTitleText());
98          }
99      }
100 }