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 org.htmlunit.MockWebConnection;
18  import org.htmlunit.SimpleWebTestCase;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link HtmlTitle}.
24   *
25   * @author Ahmed Ashour
26   * @author Marc Guillemot
27   */
28  public class HtmlTitleTest extends SimpleWebTestCase {
29  
30      /**
31       * It is questionable to have the title in HtmlPage.asText() but if we have it, then
32       * it should be followed by a new line.
33       * @throws Exception if the test fails
34       */
35      @Test
36      public void pageAsNormalizedText() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html>\n"
39              + "<head>\n"
40              + "<title>Dummy</title>\n"
41              + "</head>\n"
42              + "\n"
43              + "<body>\n"
44              + "Dummy page\n"
45              + "</body>\n"
46              + "</html>\n";
47  
48          final HtmlPage page = loadPage(html);
49          final String expected = "Dummy\nDummy page";
50          assertEquals(expected, page.asNormalizedText());
51      }
52  
53      /**
54       * @throws Exception if the test fails
55       */
56      @Test
57      public void getTitleText() throws Exception {
58          final String html = DOCTYPE_HTML
59              + "<html>\n"
60              + "<head>\n"
61              + "<title>Title\nText     Test</title>\n"
62              + "</head>\n"
63              + "\n"
64              + "<body>\n"
65              + "Dummy page\n"
66              + "</body>\n"
67              + "</html>\n";
68  
69          final HtmlPage page = loadPage(html);
70          assertEquals("Title Text Test", page.getTitleText());
71      }
72  
73      /**
74       * @throws Exception if the test fails
75       */
76      @Test
77      public void asTextEmptyTitle() throws Exception {
78          final String html = DOCTYPE_HTML
79              + "<html>\n"
80              + "<head>\n"
81              + "<title></title>\n"
82              + "</head>\n"
83              + "\n"
84              + "<body>\n"
85              + "Dummy page\n"
86              + "</body>\n"
87              + "</html>\n";
88  
89          final HtmlPage page = loadPage(html);
90          assertEquals("", page.getTitleText());
91      }
92  
93      /**
94       * @throws Exception if the test fails
95       */
96      @Test
97      public void asTextContainingTags() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html>\n"
100             + "<head>\n"
101             + "<title>My<br>Title<p>Text</p></title>\n"
102             + "</head>\n"
103             + "\n"
104             + "<body>\n"
105             + "Dummy page\n"
106             + "</body>\n"
107             + "</html>\n";
108 
109         final HtmlPage page = loadPage(html);
110         assertEquals("My<br>Title<p>Text</p>", page.getTitleText());
111     }
112 
113     /**
114      * Test for Bug #1199.
115      * @throws Exception if the test fails
116      */
117     @Test
118     public void getTitleTextDontLoadCss() throws Exception {
119         final String html = DOCTYPE_HTML
120             + "<html>\n"
121             + "<head>\n"
122             + "<title>Title</title>\n"
123             + "<link href='styles.css' type='text/css' rel='stylesheet'>\n"
124             + "</head>\n"
125             + "\n"
126             + "<body>\n"
127             + "Dummy page\n"
128             + "</body>\n"
129             + "</html>\n";
130 
131         final HtmlPage page = loadPage(html);
132         final MockWebConnection conn = (MockWebConnection) page.getWebClient().getWebConnection();
133         assertEquals(2, conn.getRequestCount());
134 
135         assertEquals("Title", page.getTitleText());
136         assertEquals(2, conn.getRequestCount());
137     }
138 
139     /**
140      * As of WebDriver 2.25.0, this can't be run with FirefoxDriver as following exception occurs:
141      * org.openqa.selenium.WebDriverException: waiting for doc.body failed.
142      * @throws Exception if the test fails
143      */
144     @Test
145     @Alerts("")
146     public void titleAfterDeleteDocumentElement() throws Exception {
147         final String html = DOCTYPE_HTML
148             + "<html><head><title>foo</title>\n"
149             + "<script>\n"
150             + "function test() {\n"
151             + "  try {\n"
152             + "    document.documentElement.parentNode.removeChild(document.documentElement);\n"
153             + "    alert(document.title);\n"
154             + "  } catch(e) { alert('exception'); }\n"
155             + "}\n"
156             + "</script>\n"
157             + "</head><body onload='test()'>\n"
158             + "</body></html>";
159 
160         final HtmlPage page = loadPageWithAlerts(html);
161         page.asXml(); // ensure that no NPE occurs here now
162     }
163 }