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.WebClient;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for elements inside {@link HtmlNoScript}.
25   *
26   * @author Ahmed Ashour
27   * @author Marc Guillemot
28   * @author Ronald Brill
29   */
30  public class HtmlNoScript2Test extends SimpleWebTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts("<body>\r\n"
37                  + "  <noscript>&lt;div&gt;hello</noscript>\r\n"
38                  + "</body>")
39      public void asXml_jsEnabled() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html><body>\n"
42              + "<noscript><div>hello</noscript>"
43              + "</body></html>";
44  
45          final HtmlPage page = loadPage(html);
46          assertEquals(getExpectedAlerts()[0], page.getBody().asXml());
47      }
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      public void asXml_jsDisabled() throws Exception {
54          final String html = DOCTYPE_HTML
55              + "<html><body>\n"
56              + "<noscript><div>hello</noscript>"
57              + "</body></html>";
58  
59          final String expected = "<body>\r\n"
60              + "  <noscript>\r\n"
61              + "    <div>hello</div>\r\n"
62              + "  </noscript>\r\n"
63              + "</body>";
64  
65          final WebClient client = getWebClient();
66          client.getOptions().setJavaScriptEnabled(false);
67  
68          final MockWebConnection webConnection = new MockWebConnection();
69          webConnection.setDefaultResponse(html);
70          client.setWebConnection(webConnection);
71  
72          final HtmlPage page = client.getPage(URL_FIRST);
73          assertEquals(expected, page.getBody().asXml());
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      public void asNormalizedTextjsEnabled() throws Exception {
81          final String htmlContent = DOCTYPE_HTML
82              + "<html><body>\n"
83              + "<noscript>hello</noscript>"
84              + "</body></html>";
85  
86          final HtmlPage page = loadPage(htmlContent);
87          assertEquals("", page.getBody().asNormalizedText());
88      }
89  
90      /**
91       * @throws Exception if the test fails
92       */
93      @Test
94      public void asNormalizedText_jsDisabled() throws Exception {
95          final String html = DOCTYPE_HTML
96              + "<html><body>\n"
97              + "<noscript>hello</noscript>"
98              + "</body></html>";
99  
100         final String expected = "hello";
101 
102         final WebClient client = getWebClient();
103         client.getOptions().setJavaScriptEnabled(false);
104 
105         final MockWebConnection webConnection = new MockWebConnection();
106         webConnection.setDefaultResponse(html);
107         client.setWebConnection(webConnection);
108 
109         final HtmlPage page = client.getPage(URL_FIRST);
110         assertEquals(expected, page.getBody().asNormalizedText());
111     }
112 
113     /**
114      * @throws Exception if the test fails
115      */
116     @Test
117     public void isEmptyXmlTagExpanded() throws Exception {
118         final String html = DOCTYPE_HTML
119             + "<html><body>\n"
120             + "<noscript></noscript>"
121             + "</body></html>";
122 
123         final HtmlPage page = loadPage(html);
124         assertTrue(page.asXml().contains("</noscript>"));
125     }
126 }