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