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