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;
16  
17  import java.util.List;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.htmlunit.junit.annotation.BuggyWebDriver;
22  import org.junit.jupiter.api.Test;
23  import org.openqa.selenium.WebDriver;
24  import org.openqa.selenium.logging.LogEntries;
25  import org.openqa.selenium.logging.LogEntry;
26  import org.openqa.selenium.logging.LogType;
27  import org.openqa.selenium.logging.Logs;
28  
29  /**
30   * Tests for Console.
31   *
32   * @author Ahmed Ashour
33   * @author Marc Guillemot
34   * @author Ronald Brill
35   */
36  public class ConsoleTest extends WebDriverTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts({"false", "object", "true"})
43      public void prototype() throws Exception {
44          final String html = DOCTYPE_HTML
45              + "<html>\n"
46              + "<body>\n"
47              + "<script>\n"
48              + LOG_TITLE_FUNCTION
49              + "  try {\n"
50              + "    log(window.console == undefined);\n"
51              + "    log(typeof window.console);\n"
52              + "    log('console' in window);\n"
53              + "  } catch(e) { logEx(e);}\n"
54              + "</script>\n"
55              + "</body></html>";
56  
57          loadPageVerifyTitle2(html);
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts({"true", "undefined", "false"})
65      public void prototypeUppercase() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html>\n"
68              + "<body>\n"
69              + "<script>\n"
70              + LOG_TITLE_FUNCTION
71              + "  try {\n"
72              + "    log(window.Console == undefined);\n"
73              + "    log(typeof window.Console);\n"
74              + "    log('Console' in window);\n"
75              + "  } catch(e) { logEx(e);}\n"
76              + "</script>\n"
77              + "</body></html>";
78  
79          loadPageVerifyTitle2(html);
80      }
81  
82      /**
83       * @throws Exception if the test fails
84       */
85      @Test
86      @Alerts({})
87      public void timeStamp() throws Exception {
88          final String html = DOCTYPE_HTML
89              + "<html>\n"
90              + "<body>\n"
91              + "<script>\n"
92              + LOG_TITLE_FUNCTION
93              + "  if (window.console && window.console.timeStamp) {\n"
94              + "    console.timeStamp();\n"
95              + "    console.timeStamp('ready');\n"
96              + "  } else { log('window.console.timeStamp not available');}\n"
97              + "</script>\n"
98              + "</body></html>";
99  
100         loadPageVerifyTitle2(html);
101     }
102 
103     /**
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts({"function", "function", "function", "function", "function", "function"})
108     public void methods() throws Exception {
109         final String html = DOCTYPE_HTML
110             + "<html>\n"
111             + "<body>\n"
112             + "<script>\n"
113             + LOG_TITLE_FUNCTION
114             + "  log(typeof console.log);\n"
115             + "  log(typeof console.info);\n"
116             + "  log(typeof console.warn);\n"
117             + "  log(typeof console.error);\n"
118             + "  log(typeof console.debug);\n"
119             + "  log(typeof console.timeStamp);\n"
120             + "</script>\n"
121             + "</body></html>";
122 
123         loadPageVerifyTitle2(html);
124     }
125 
126     /**
127      * @throws Exception if the test fails
128      */
129     @Test
130     @Alerts("true")
131     public void windowProperty() throws Exception {
132         final String html = DOCTYPE_HTML
133             + "<html>\n"
134             + "<body>\n"
135             + "<script>\n"
136             + LOG_TITLE_FUNCTION
137             + "  try {\n"
138             + "    var x = Object.getOwnPropertyNames(window).indexOf('console');\n"
139             + "    log(x >= 0);\n"
140             + "  } catch(e) { logEx(e) }\n"
141             + "</script>\n"
142             + "</body></html>";
143 
144         loadPageVerifyTitle2(html);
145     }
146 
147     /**
148      * @throws Exception if the test fails
149      */
150     @Test
151     @Alerts("success")
152     public void fromWindow() throws Exception {
153         final String html = DOCTYPE_HTML
154             + "<html>\n"
155             + "<body>\n"
156             + "<script>\n"
157             + LOG_TITLE_FUNCTION
158             + "  try {\n"
159             + "    var x = console.error;\n"
160             + "    x('hello');\n"
161             + "    log('success');\n"
162             + "  } catch(e) { logEx(e) }\n"
163             + "</script>\n"
164             + "</body></html>";
165 
166         loadPageVerifyTitle2(html);
167     }
168 
169     /**
170      * @throws Exception if the test fails
171      */
172     @Test
173     @BuggyWebDriver
174     public void simpleString() throws Exception {
175         final String html = DOCTYPE_HTML
176             + "<html>\n"
177             + "<body>\n"
178             + "<script>\n"
179             + "  for (i = 0; i < 4; i++) {\n"
180             + "    console.log('test log ' + i);\n"
181             + "  }\n"
182             + "</script>\n"
183             + "</body></html>";
184 
185         final WebDriver driver = loadPage2(html);
186 
187         final Logs logs = driver.manage().logs();
188         final LogEntries logEntries = logs.get(LogType.BROWSER);
189         final List<LogEntry> logEntryList = logEntries.getAll();
190 
191         final int count = logEntryList.size();
192         assertTrue(count > 0);
193 
194         long timestamp = 0;
195         for (int i = 0; i < 4; i++) {
196             final LogEntry logEntry = logEntryList.get(i);
197             assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("test log " + i));
198             assertTrue(logEntry.getTimestamp() >= timestamp);
199             timestamp = logEntry.getTimestamp();
200         }
201     }
202 
203     /**
204      * @throws Exception if the test fails
205      */
206     @Test
207     @BuggyWebDriver
208     public void assertOnly() throws Exception {
209         final String html = DOCTYPE_HTML
210             + "<html>\n"
211             + "<body>\n"
212             + "<script>\n"
213             + "  number = 1;\n"
214             + "  console.assert(number % 2 === 0);\n"
215             + "</script>\n"
216             + "</body></html>";
217 
218         final WebDriver driver = loadPage2(html);
219 
220         final Logs logs = driver.manage().logs();
221         final LogEntries logEntries = logs.get(LogType.BROWSER);
222         final List<LogEntry> logEntryList = logEntries.getAll();
223 
224         assertEquals(1, logEntryList.size());
225 
226         final LogEntry logEntry = logEntryList.get(0);
227         assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("Assertion failed"));
228     }
229 
230     /**
231      * @throws Exception if the test fails
232      */
233     @Test
234     @BuggyWebDriver
235     public void assertString() throws Exception {
236         final String html = DOCTYPE_HTML
237             + "<html>\n"
238             + "<body>\n"
239             + "<script>\n"
240             + "  number = 1;\n"
241             + "  console.assert(number % 2 === 0, 'the # is not even');\n"
242             + "</script>\n"
243             + "</body></html>";
244 
245         final WebDriver driver = loadPage2(html);
246 
247         final Logs logs = driver.manage().logs();
248         final LogEntries logEntries = logs.get(LogType.BROWSER);
249         final List<LogEntry> logEntryList = logEntries.getAll();
250 
251         assertEquals(1, logEntryList.size());
252 
253         final LogEntry logEntry = logEntryList.get(0);
254         assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("Assertion failed: the # is not even"));
255     }
256 
257     /**
258      * @throws Exception if the test fails
259      */
260     @Test
261     @BuggyWebDriver
262     public void assertObject() throws Exception {
263         final String html = DOCTYPE_HTML
264             + "<html>\n"
265             + "<body>\n"
266             + "<script>\n"
267             + "  var number = 1;\n"
268             + "  console.assert(number % 2 === 0, {number: number, errorMsg: 'the # is not even'});\n"
269             + "</script>\n"
270             + "</body></html>";
271 
272         final WebDriver driver = loadPage2(html);
273 
274         final Logs logs = driver.manage().logs();
275         final LogEntries logEntries = logs.get(LogType.BROWSER);
276         final List<LogEntry> logEntryList = logEntries.getAll();
277 
278         assertEquals(1, logEntryList.size());
279 
280         final LogEntry logEntry = logEntryList.get(0);
281         assertTrue(logEntry.getMessage(), logEntry.getMessage()
282                 .contains("Assertion failed: {\"number\":1,\"errorMsg\":\"the # is not even\"}"));
283     }
284 
285     /**
286      * @throws Exception if the test fails
287      */
288     @Test
289     @BuggyWebDriver
290     public void assertObjects() throws Exception {
291         final String html = DOCTYPE_HTML
292             + "<html>\n"
293             + "<body>\n"
294             + "<script>\n"
295             + "  number = 1;\n"
296             + "  console.assert(number % 2 === 0, {number: number}, {errorMsg: 'the # is not even'});\n"
297             + "</script>\n"
298             + "</body></html>";
299 
300         final WebDriver driver = loadPage2(html);
301 
302         final Logs logs = driver.manage().logs();
303         final LogEntries logEntries = logs.get(LogType.BROWSER);
304         final List<LogEntry> logEntryList = logEntries.getAll();
305 
306         assertEquals(1, logEntryList.size());
307 
308         final LogEntry logEntry = logEntryList.get(0);
309         assertTrue(logEntry.getMessage(), logEntry.getMessage()
310                 .contains("Assertion failed: {\"number\":1} {\"errorMsg\":\"the # is not even\"}"));
311     }
312 
313     /**
314      * @throws Exception if the test fails
315      */
316     @Test
317     @BuggyWebDriver
318     public void assertParams() throws Exception {
319         final String html = DOCTYPE_HTML
320             + "<html>\n"
321             + "<body>\n"
322             + "<script>\n"
323             + "  console.assert(false, 'the word is %s', 'foo');\n"
324             + "</script>\n"
325             + "</body></html>";
326 
327         final WebDriver driver = loadPage2(html);
328 
329         final Logs logs = driver.manage().logs();
330         final LogEntries logEntries = logs.get(LogType.BROWSER);
331         final List<LogEntry> logEntryList = logEntries.getAll();
332 
333         assertEquals(1, logEntryList.size());
334 
335         final LogEntry logEntry = logEntryList.get(0);
336         assertTrue(logEntry.getMessage(), logEntry.getMessage()
337                 .contains("Assertion failed: the word is foo"));
338     }
339 
340     /**
341      * @throws Exception if the test fails
342      */
343     @Test
344     @BuggyWebDriver
345     public void trace() throws Exception {
346         final String html
347             = "<html>\n"
348             + "<body>\n"
349             + "<script>\n"
350             + "  function foo() {\n"
351             + "    function bar() {\n"
352             + "      console.trace();\n"
353             + "    }\n"
354             + "    bar();\n"
355             + "  }\n"
356             + "  foo();\n"
357             + "</script>\n"
358             + "</body></html>";
359 
360         final WebDriver driver = loadPage2(html);
361 
362         final Logs logs = driver.manage().logs();
363         final LogEntries logEntries = logs.get(LogType.BROWSER);
364         final List<LogEntry> logEntryList = logEntries.getAll();
365 
366         assertEquals(1, logEntryList.size());
367 
368         final LogEntry logEntry = logEntryList.get(0);
369         final String logMsg = logEntry.getMessage();
370         assertTrue(logMsg, logMsg
371                 .matches("bar\\(\\)@script in http.*:6\\n"
372                         + "foo\\(\\)@script in http.*:8\\n"
373                         + "@script in http.*:10"));
374     }
375 
376     /**
377      * @throws Exception if the test fails
378      */
379     @Test
380     @BuggyWebDriver
381     public void errorCall() throws Exception {
382         final String html = DOCTYPE_HTML
383             + "<html>\n"
384             + "<body>\n"
385             + "<script>\n"
386             + "  function foo() {\n"
387             + "    (undefined || console.error)('he ho');\n"
388             + "  }\n"
389             + "  foo();\n"
390             + "</script>\n"
391             + "</body></html>";
392 
393         final WebDriver driver = loadPage2(html);
394 
395         final Logs logs = driver.manage().logs();
396         final LogEntries logEntries = logs.get(LogType.BROWSER);
397         final List<LogEntry> logEntryList = logEntries.getAll();
398 
399         assertEquals(1, logEntryList.size());
400 
401         final LogEntry logEntry = logEntryList.get(0);
402         final String logMsg = logEntry.getMessage();
403         assertTrue(logMsg, logMsg.contains("he ho"));
404     }
405 }