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.host.html;
16  
17  import java.net.URL;
18  
19  import org.htmlunit.MockWebConnection;
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.WebClient;
22  import org.htmlunit.html.FrameWindow;
23  import org.htmlunit.html.HtmlPage;
24  import org.htmlunit.junit.annotation.Alerts;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests for {@link HTMLDocument}'s write(ln) function.
29   *
30   * @author Ahmed Ashour
31   * @author Marc Guillemot
32   * @author Frank Danek
33   */
34  public class HTMLDocumentWriteTest extends SimpleWebTestCase {
35  
36      /**
37       * IE accepts the use of detached functions.
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts("exception occurred")
42      public void write_AssignedToVar() throws Exception {
43          final String html = DOCTYPE_HTML
44              + "<html><head><title>foo</title><script>\n"
45              + "function doTheFoo() {\n"
46              + "var d = document.writeln;\n"
47              + "try {\n"
48              + "  d('foo');\n"
49              + "} catch(e) { alert('exception occurred') }\n"
50              + "  document.writeln('foo');\n"
51              + "}\n"
52              + "</script></head><body onload='doTheFoo()'>\n"
53              + "<p>hello world</p>\n"
54              + "</body></html>";
55  
56          loadPageWithAlerts(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      public void writeFrameRelativeURLMultipleFrameset() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html><head><title>frameset</title></head>\n"
66              + "<script>\n"
67              + "    document.write('<frameset><frame src=\"frame.html\"/></frameset>');\n"
68              + "</script>\n"
69              + "<frameset><frame src='blank.html'/></frameset>\n"
70              + "</html>";
71  
72          final URL baseURL = new URL("http://base/subdir/");
73          final URL framesetURL = new URL(baseURL + "test.html");
74          final URL frameURL = new URL(baseURL + "frame.html");
75          final URL blankURL = new URL(baseURL + "blank.html");
76  
77          final WebClient client = getWebClient();
78          final MockWebConnection webConnection = new MockWebConnection();
79          webConnection.setResponse(framesetURL, html);
80          webConnection.setResponseAsGenericHtml(frameURL, "frame");
81          webConnection.setResponseAsGenericHtml(blankURL, "blank");
82          client.setWebConnection(webConnection);
83  
84          final HtmlPage framesetPage = client.getPage(framesetURL);
85          final FrameWindow frame = framesetPage.getFrames().get(0);
86          final HtmlPage framePage = (HtmlPage) frame.getEnclosedPage();
87  
88          assertNotNull(frame);
89          assertEquals(frameURL.toExternalForm(), framePage.getUrl().toExternalForm());
90          assertEquals("frame", framePage.getTitleText());
91      }
92  }