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.libraries;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.time.Duration;
22  
23  import org.apache.commons.io.IOUtils;
24  import org.htmlunit.MockWebConnection;
25  import org.htmlunit.WebDriverTestCase;
26  import org.htmlunit.junit.annotation.Alerts;
27  import org.htmlunit.util.MimeType;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests that depend on one of JavaScript libraries.
32   *
33   * @author Ahmed Ashour
34   * @author Ronald Brill
35   */
36  public class LibraryDependencyTest extends WebDriverTestCase {
37  
38      /**
39       * Test for http://sourceforge.net/p/htmlunit/bugs/637/.
40       * @throws Exception if the test fails
41       */
42      @Alerts("2")
43      @Test
44      public void contextFactory_Browser() throws Exception {
45          final String firstHtml = DOCTYPE_HTML
46              + "<html>\n"
47              + "<head>\n"
48              + "  <title>1</title>\n"
49              + "  <script src='" + URL_THIRD + "' type='text/javascript'></script>\n"
50              + "</head>\n"
51              + "<body>\n"
52              + "<script>\n"
53              + "  setTimeout(finishCreateAccount, 2500);\n"
54              + "  function finishCreateAccount() {\n"
55              + "    completionUrl = '" + URL_SECOND + "';\n"
56              + "    document.location.replace(completionUrl);\n"
57              + "  }\n"
58              + "</script>\n"
59              + "</body>\n"
60              + "</html>";
61          final String secondHtml = DOCTYPE_HTML
62              + "<html>\n"
63              + "<head>\n"
64              + "  <title>2</title>\n"
65              + "  <script src='" + URL_THIRD + "' type='text/javascript'></script>\n"
66              + "</head>\n"
67              + "<body onload='alert(2)'>\n"
68              + "<div id='id2'>Page2</div>\n"
69              + "</body>\n"
70              + "</html>";
71          final String prototype = getContent("libraries/prototype/1.6.0/dist/prototype.js");
72  
73          final MockWebConnection webConnection = getMockWebConnection();
74          webConnection.setResponse(URL_SECOND, secondHtml);
75          webConnection.setResponse(URL_THIRD, prototype, MimeType.TEXT_JAVASCRIPT);
76  
77          loadPageWithAlerts2(firstHtml, Duration.ofMillis(10_000));
78      }
79  
80      private String getContent(final String resourceName) throws IOException {
81          try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName)) {
82              return IOUtils.toString(in, ISO_8859_1);
83          }
84      }
85  }