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.javascript;
16  
17  import java.net.URL;
18  
19  import org.htmlunit.MockWebConnection;
20  import org.htmlunit.WebDriverTestCase;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.junit.jupiter.api.Test;
23  import org.openqa.selenium.WebDriver;
24  
25  /**
26   * Tests for the {@link PostponedAction}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   * @author Marc Guillemot
31   * @author Matthias Brandt
32   */
33  public class PostponedActionTest extends WebDriverTestCase {
34  
35      /**
36       * @throws Exception if the test fails
37       */
38      @Test
39      @Alerts("before after second.html third.html")
40      public void loadingJavaScript() throws Exception {
41          final String html = DOCTYPE_HTML
42              + "<html>\n"
43              + "<head>\n"
44              + "<script>\n"
45              + "function test() {\n"
46              + "  document.title += ' before';\n"
47              + "  var iframe2 = document.createElement('iframe');\n"
48              + "  iframe2.src = 'frame2.html';\n"
49              + "  document.body.appendChild(iframe2);\n"
50              + "  var iframe3 = document.createElement('iframe');\n"
51              + "  document.body.appendChild(iframe3);\n"
52              + "  iframe3.src = 'frame3.html';\n"
53              + "  document.title += ' after';\n"
54              + "}\n"
55              + "</script>\n"
56              + "</head>\n"
57              + "<body onload='test()'>\n"
58              + "</body>\n"
59              + "</html>";
60          final String secondContent
61              = "<script>top.document.title += ' second.html';</script>";
62          final String thirdContent
63              = "<script>top.document.title += ' third.html';</script>";
64  
65          final MockWebConnection conn = getMockWebConnection();
66          conn.setResponse(new URL(URL_FIRST, "frame2.html"), secondContent);
67          conn.setResponse(new URL(URL_FIRST, "frame3.html"), thirdContent);
68  
69          final WebDriver driver = loadPage2(html);
70          assertTitle(driver, getExpectedAlerts()[0]);
71      }
72  
73      /**
74       * @throws Exception if the test fails
75       */
76      @Test
77      @Alerts("before after second.html")
78      public void loadingJavaScript2() throws Exception {
79          final String firstContent = DOCTYPE_HTML
80              + "<html>\n"
81              + "<head>\n"
82              + "<script>\n"
83              + "function test() {\n"
84              + "  document.title += ' before';\n"
85              + "  var iframe = document.createElement('iframe');\n"
86              + "  document.body.appendChild(iframe);\n"
87              + "  iframe.contentWindow.location.replace('" + URL_SECOND + "');\n"
88              + "  document.title += ' after';\n"
89              + "}\n"
90              + "</script>\n"
91              + "</head>\n"
92              + "<body onload='test()'>\n"
93              + "</body>\n"
94              + "</html>";
95          final String secondContent
96              = "<script>top.document.title += ' second.html';</script>";
97  
98          getMockWebConnection().setResponse(URL_SECOND, secondContent);
99  
100         final WebDriver driver = loadPage2(firstContent);
101         assertTitle(driver, getExpectedAlerts()[0]);
102     }
103 
104     /**
105      * Test case for bug #1686.
106      *
107      * @throws Exception if the test fails
108      */
109     @Test
110     @Alerts("setting timeout before after iframe.html simpleAlert")
111     public void loadingJavaScriptWithTimeout() throws Exception {
112         final String html = DOCTYPE_HTML
113                 + "<html>\n"
114                 + "<head>\n"
115                 + "<script>\n"
116                 + "function test() {\n"
117                 + "  document.title += ' before';\n"
118                 + "  var iframe = document.createElement('iframe');\n"
119                 + "  iframe.src = 'iframe.html';\n"
120                 + "  document.body.appendChild(iframe);\n"
121                 + "  document.title += ' after';\n"
122                 + "}\n"
123                 + "function timeout() {\n"
124                 + "  document.title += ' setting timeout';\n"
125                 + "  window.setTimeout(function() {test()}, 400);\n"
126                 + "  window.setTimeout(function() {top.document.title += ' simpleAlert'}, 500);\n"
127                 + "}\n"
128                 + "</script>\n"
129                 + "</head>\n"
130                 + "<body onload='timeout()'>\n"
131                 + "</body>\n"
132                 + "</html>";
133         final String secondContent
134                 = "<script>top.document.title += ' iframe.html'</script>";
135 
136         final MockWebConnection conn = getMockWebConnection();
137         conn.setResponse(new URL(URL_FIRST, "iframe.html"), secondContent);
138 
139         final WebDriver driver = loadPage2(html);
140         assertTitle(driver, getExpectedAlerts()[0]);
141     }
142 
143     /**
144      * @throws Exception if the test fails
145      */
146     @Test
147     @Alerts("page 2 / script executed")
148     public void javascriptReload() throws Exception {
149         final String html = DOCTYPE_HTML
150                 + "<html>\n"
151                 + "<head>\n"
152                 + "</head>\n"
153                 + "<body onload='document.forms[0].submit()'>\n"
154                 + "  <form action='step2.html' method='post'>\n"
155                 + "  </form>"
156                 + "</body>\n"
157                 + "</html>";
158 
159         final String secondContent = DOCTYPE_HTML
160                 + "<html><head>\n"
161                 + "<title>page 2</title>\n"
162                 + "</head>\n"
163                 + "<body>\n"
164                 + "<script async>document.title += ' / script executed§';</script>\n"
165                 + "</body>\n"
166                 + "</html>";
167 
168         final MockWebConnection conn = getMockWebConnection();
169         getMockWebConnection().setResponse(new URL(URL_FIRST, "step2.html"), secondContent);
170 
171         final WebDriver driver = loadPage2(html);
172         verifyTitle2(DEFAULT_WAIT_TIME, driver, getExpectedAlerts()[0]);
173     }
174 }