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