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;
16  
17  import static org.junit.jupiter.api.Assertions.fail;
18  
19  import java.io.PrintWriter;
20  import java.io.StringWriter;
21  import java.net.URL;
22  
23  import org.htmlunit.html.HtmlPage;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests for {@link ScriptException}.
28   *
29   * @author Marc Guillemot
30   * @author Ahmed Ashour
31   */
32  public final class ScriptExceptionTest extends SimpleWebTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      public void constructor() throws Exception {
39          final String message = "bla bla";
40          final Throwable t = new RuntimeException(message);
41          final HtmlPage page = loadPage(DOCTYPE_HTML + "<html></html>");
42          final ScriptException exception = new ScriptException(page, t);
43  
44          assertEquals(t, exception.getCause());
45          assertEquals(message, exception.getMessage());
46      }
47  
48      /**
49       * Tests access to the page where the exception occurred from the exception.
50       * @throws Exception if the test fails
51       */
52      @Test
53      public void getPage() throws Exception {
54          final String html = DOCTYPE_HTML + "<html><script>notExisting()</script></html>";
55          try {
56              loadPage(html);
57          }
58          catch (final ScriptException e) {
59              assertEquals(URL_FIRST, e.getPage().getUrl());
60          }
61      }
62  
63      /**
64       * Test the JavaScript stacktrace.
65       * Note: this test will fail when the information (line, col, source name) provided to execute
66       * scripts is improved. In this case this unit test should be adapted IF the provided information
67       * in the script stack trace gets better and provides more useful information to understand the
68       * cause of a problem.
69       * @throws Exception if the test fails
70       */
71      @Test
72      public void scriptStackTrace() throws Exception {
73          testScriptStackTrace("ScriptExceptionTest1");
74          testScriptStackTrace("ScriptExceptionTest2");
75      }
76  
77      private void testScriptStackTrace(final String baseFileName) throws Exception {
78          try {
79              loadPage(getBrowserVersion(), getFileContent(baseFileName + ".html"), null,
80                  new URL("http://www.gargoylesoftware.com/"));
81          }
82          catch (final ScriptException e) {
83              final StringWriter stringWriter = new StringWriter();
84              final PrintWriter printWriter = new PrintWriter(stringWriter);
85              e.printScriptStackTrace(printWriter);
86  
87              final String expectedTrace = getFileContent(baseFileName + ".txt");
88              assertEquals(expectedTrace, stringWriter.toString());
89              return;
90          }
91          fail("Exception not thrown");
92      }
93  
94      /**
95       * @throws Exception if the test fails
96       */
97      @Test
98      public void errorLineNumber() throws Exception {
99          testErrorLineNumber("testJsError1.html", 6);
100         testErrorLineNumber("testJsError2.html", 7);
101         testErrorLineNumber("testJsError3.html", 6);
102         testErrorLineNumber("testJsError4.html", 8);
103     }
104 
105     private void testErrorLineNumber(final String fileName, final int errorLine) throws Exception {
106         final WebClient webClient = getWebClient();
107         final URL url = getClass().getClassLoader().getResource(fileName);
108         assertNotNull(url);
109         try {
110             final HtmlPage page = webClient.getPage(url);
111             page.getHtmlElementById("clickMe").click();
112             fail();
113         }
114         catch (final ScriptException e) {
115             assertEquals(errorLine, e.getFailingLineNumber());
116         }
117     }
118 }