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.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.By;
21  import org.openqa.selenium.WebDriver;
22  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
23  
24  /**
25   * Tests for elements inside {@link HtmlNoScript}.
26   *
27   * @author Ahmed Ashour
28   * @author Marc Guillemot
29   * @author Frank Danek
30   * @author Ronald Brill
31   */
32  public class HtmlNoScriptTest extends WebDriverTestCase {
33  
34      /**
35       * Verifies getVisibleText().
36       * @throws Exception if the test fails
37       */
38      @Test
39      @Alerts("")
40      public void getVisibleText() throws Exception {
41          final String htmlContent = DOCTYPE_HTML
42              + "<html>\n"
43              + "<head></head>\n"
44              + "<body>\n"
45              + "  <noscript id='tester'>\n"
46              + "</body></html>";
47  
48          final WebDriver driver = loadPage2(htmlContent);
49          final String text = driver.findElement(By.id("tester")).getText();
50          assertEquals(getExpectedAlerts()[0], text);
51  
52          if (driver instanceof HtmlUnitDriver) {
53              final HtmlPage page = (HtmlPage) getEnclosedPage();
54              assertEquals(getExpectedAlerts()[0], page.getBody().getVisibleText());
55          }
56      }
57  
58      /**
59       * @throws Exception if the test fails
60       */
61      @Test
62      @Alerts("null")
63      public void getElementById() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html><head>\n"
66              + "<script>\n"
67              + LOG_TITLE_FUNCTION
68              + "  function test() {\n"
69              + "    log(document.getElementById('second'));\n"
70              + "  }\n"
71              + "</script>\n"
72              + "</head><body onload='test()'>\n"
73              + "  <input type='text' id='first' name='textfield'/>\n"
74              + "  <noscript>\n"
75              + "  <input type='text' id='second' name='button'/>\n"
76              + "  </noscript>\n"
77              + "</body></html>";
78  
79          loadPageVerifyTitle2(html);
80      }
81  
82      /**
83       * @throws Exception if the test fails
84       */
85      @Test
86      @Alerts({"1", "[object Text]"})
87      public void childNodes() throws Exception {
88          final String html = DOCTYPE_HTML
89              + "<html><head>\n"
90              + "<script>\n"
91              + LOG_TITLE_FUNCTION
92              + "  function test() {\n"
93              + "    var noscript = document.getElementById('myDiv').childNodes.item(0);\n"
94              + "    log(noscript.childNodes.length);\n"
95              + "    log(noscript.firstChild);\n"
96              + "  }\n"
97              + "</script>\n"
98              + "</head><body onload='test()'>\n"
99              + "  <div id='myDiv'><noscript>\n"
100             + "    <input type='text' name='button'/>\n"
101             + "  </noscript></div>\n"
102             + "</body></html>";
103 
104         loadPageVerifyTitle2(html);
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     @Alerts("1")
112     public void testJavaScript() throws Exception {
113         final String html = DOCTYPE_HTML
114             + "<html><head>\n"
115             + "<script>\n"
116             + "  alert(1);\n"
117             + "</script>\n"
118             + "<noscript>\n"
119             + "  <script>\n"
120             + "    alert(2);\n"
121             + "  </script>\n"
122             + "</noscript>\n"
123             + "</head><body>\n"
124             + "</body></html>";
125 
126         loadPageWithAlerts2(html);
127     }
128 
129     /**
130      * @throws Exception if the test fails
131      */
132     @Test
133     public void formValues() throws Exception {
134         final String html = DOCTYPE_HTML
135             + "<html><body>\n"
136             + "<form name='item' method='get'>\n"
137             + "  <noscript>\n"
138             + "    <input type=hidden name='__webpage_no_js__' value='1'>\n"
139             + "  </noscript>\n"
140             + "  <input type=hidden name='myParam' value='myValue'>\n"
141             + "  <input type='submit' id='clickMe'>\n"
142             + "</form>\n"
143             + "</body></html>";
144 
145         final WebDriver webDriver = loadPage2(html);
146         webDriver.findElement(By.id("clickMe")).click();
147 
148         assertFalse(webDriver.getCurrentUrl().contains("__webpage_no_js__"));
149     }
150 
151     /**
152      * @throws Exception if the test fails
153      */
154     @Test
155     @Alerts("Has Script")
156     public void jsDetection() throws Exception {
157         final String html = DOCTYPE_HTML
158             + "<html>\n"
159             + "<head>\n"
160             + "<noscript>\n"
161             + "  <meta http-equiv='refresh' content='0;url=" + URL_SECOND + "'>\n"
162             + "</noscript>\n"
163             + "<title>start</title>\n"
164             + "</head>\n"
165             + "<body onload='document.form.submit()'>\n"
166             + "<form name='form' action='" + URL_THIRD + "'></form>\n"
167             + "</body>\n"
168             + "</html>";
169 
170         final String htmlNoScript = DOCTYPE_HTML
171             + "<html>\n"
172             + "<head><title>No Script\u00A7</title></head>\n"
173             + "<body></body>\n"
174             + "</html>";
175         getMockWebConnection().setResponse(URL_SECOND, htmlNoScript);
176 
177         final String htmlHasScript = DOCTYPE_HTML
178                 + "<html>\n"
179                 + "<head><title>Has Script\u00A7</title></head>\n"
180                 + "<body></body>\n"
181                 + "</html>";
182         getMockWebConnection().setResponse(URL_THIRD, htmlHasScript);
183 
184         loadPage2(html);
185         verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
186     }
187 }