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