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