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