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