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              "function", "function", "function", "function", "function", "function",
109              "function"})
110     public void methods() throws Exception {
111         final String html = DOCTYPE_HTML
112             + "<html>\n"
113             + "<body>\n"
114             + "<script>\n"
115             + LOG_TITLE_FUNCTION
116             + "  log(typeof console.log);\n"
117             + "  log(typeof console.trace);\n"
118             + "  log(typeof console.info);\n"
119             + "  log(typeof console.warn);\n"
120             + "  log(typeof console.error);\n"
121             + "  log(typeof console.debug);\n"
122 
123             + "  log(typeof console.assert);\n"
124 
125             + "  log(typeof console.time);\n"
126             + "  log(typeof console.timeLog);\n"
127             + "  log(typeof console.timeEnd);\n"
128             + "  log(typeof console.timeStamp);\n"
129 
130             + "  log(typeof console.count);\n"
131             + "  log(typeof console.countReset);\n"
132 
133             + "</script>\n"
134             + "</body></html>";
135 
136         loadPageVerifyTitle2(html);
137     }
138 
139     /**
140      * @throws Exception if the test fails
141      */
142     @Test
143     @Alerts("true")
144     public void windowProperty() throws Exception {
145         final String html = DOCTYPE_HTML
146             + "<html>\n"
147             + "<body>\n"
148             + "<script>\n"
149             + LOG_TITLE_FUNCTION
150             + "  try {\n"
151             + "    var x = Object.getOwnPropertyNames(window).indexOf('console');\n"
152             + "    log(x >= 0);\n"
153             + "  } catch(e) { logEx(e) }\n"
154             + "</script>\n"
155             + "</body></html>";
156 
157         loadPageVerifyTitle2(html);
158     }
159 
160     /**
161      * @throws Exception if the test fails
162      */
163     @Test
164     @Alerts("success")
165     public void logFromWindow() throws Exception {
166         final String html = DOCTYPE_HTML
167             + "<html>\n"
168             + "<body>\n"
169             + "<script>\n"
170             + LOG_TITLE_FUNCTION
171             + "  try {\n"
172             + "    let x = console.log;\n"
173             + "    x('hello');\n"
174 
175             + "    x = console.trace;\n"
176             + "    x('hello');\n"
177 
178             + "    x = console.info;\n"
179             + "    x('hello');\n"
180 
181             + "    x = console.warn;\n"
182             + "    x('hello');\n"
183 
184             + "    x = console.error;\n"
185             + "    x('hello');\n"
186 
187             + "    x = console.debug;\n"
188             + "    x('hello');\n"
189 
190             + "    log('success');\n"
191             + "  } catch(e) { logEx(e) }\n"
192             + "</script>\n"
193             + "</body></html>";
194 
195         loadPageVerifyTitle2(html);
196     }
197 
198     /**
199      * @throws Exception if the test fails
200      */
201     @Test
202     @Alerts("success")
203     public void timeFromWindow() throws Exception {
204         final String html = DOCTYPE_HTML
205             + "<html>\n"
206             + "<body>\n"
207             + "<script>\n"
208             + LOG_TITLE_FUNCTION
209             + "  try {\n"
210             + "    let x = console.time;\n"
211             + "    x('hello');\n"
212 
213             + "    x = console.timeLog;\n"
214             + "    x('hello');\n"
215 
216             + "    x = console.timeEnd;\n"
217             + "    x('hello');\n"
218 
219             + "    x = console.timeStamp;\n"
220             + "    x('hello');\n"
221 
222             + "    log('success');\n"
223             + "  } catch(e) { logEx(e) }\n"
224             + "</script>\n"
225             + "</body></html>";
226 
227         loadPageVerifyTitle2(html);
228     }
229 
230     /**
231      * @throws Exception if the test fails
232      */
233     @Test
234     @Alerts("success")
235     public void assertFromWindow() throws Exception {
236         final String html = DOCTYPE_HTML
237             + "<html>\n"
238             + "<body>\n"
239             + "<script>\n"
240             + LOG_TITLE_FUNCTION
241             + "  try {\n"
242             + "    let x = console.assert;\n"
243             + "    x(true, 'hello');\n"
244 
245             + "    log('success');\n"
246             + "  } catch(e) { logEx(e) }\n"
247             + "</script>\n"
248             + "</body></html>";
249 
250         loadPageVerifyTitle2(html);
251     }
252 
253     /**
254      * @throws Exception if the test fails
255      */
256     @Test
257     @Alerts("success")
258     public void countFromWindow() throws Exception {
259         final String html = DOCTYPE_HTML
260             + "<html>\n"
261             + "<body>\n"
262             + "<script>\n"
263             + LOG_TITLE_FUNCTION
264             + "  try {\n"
265             + "    let x = console.count;\n"
266             + "    x('hello');\n"
267 
268             + "    x = console.countReset;\n"
269             + "    x('hello');\n"
270 
271             + "    log('success');\n"
272             + "  } catch(e) { logEx(e) }\n"
273             + "</script>\n"
274             + "</body></html>";
275 
276         loadPageVerifyTitle2(html);
277     }
278 
279     /**
280      * @throws Exception if the test fails
281      */
282     @Test
283     @BuggyWebDriver
284     public void simpleString() throws Exception {
285         final String html = DOCTYPE_HTML
286             + "<html>\n"
287             + "<body>\n"
288             + "<script>\n"
289             + "  for (i = 0; i < 4; i++) {\n"
290             + "    console.log('test log ' + i);\n"
291             + "  }\n"
292             + "</script>\n"
293             + "</body></html>";
294 
295         final WebDriver driver = loadPage2(html);
296 
297         final Logs logs = driver.manage().logs();
298         final LogEntries logEntries = logs.get(LogType.BROWSER);
299         final List<LogEntry> logEntryList = logEntries.getAll();
300 
301         final int count = logEntryList.size();
302         assertTrue(count > 0);
303 
304         long timestamp = 0;
305         for (int i = 0; i < 4; i++) {
306             final LogEntry logEntry = logEntryList.get(i);
307             assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("test log " + i));
308             assertTrue(logEntry.getTimestamp() >= timestamp);
309             timestamp = logEntry.getTimestamp();
310         }
311     }
312 
313     /**
314      * @throws Exception if the test fails
315      */
316     @Test
317     @BuggyWebDriver
318     public void assertOnly() throws Exception {
319         final String html = DOCTYPE_HTML
320             + "<html>\n"
321             + "<body>\n"
322             + "<script>\n"
323             + "  number = 1;\n"
324             + "  console.assert(number % 2 === 0);\n"
325             + "</script>\n"
326             + "</body></html>";
327 
328         final WebDriver driver = loadPage2(html);
329 
330         final Logs logs = driver.manage().logs();
331         final LogEntries logEntries = logs.get(LogType.BROWSER);
332         final List<LogEntry> logEntryList = logEntries.getAll();
333 
334         assertEquals(1, logEntryList.size());
335 
336         final LogEntry logEntry = logEntryList.get(0);
337         assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("Assertion failed"));
338     }
339 
340     /**
341      * @throws Exception if the test fails
342      */
343     @Test
344     @BuggyWebDriver
345     public void assertString() throws Exception {
346         final String html = DOCTYPE_HTML
347             + "<html>\n"
348             + "<body>\n"
349             + "<script>\n"
350             + "  number = 1;\n"
351             + "  console.assert(number % 2 === 0, 'the # is not even');\n"
352             + "</script>\n"
353             + "</body></html>";
354 
355         final WebDriver driver = loadPage2(html);
356 
357         final Logs logs = driver.manage().logs();
358         final LogEntries logEntries = logs.get(LogType.BROWSER);
359         final List<LogEntry> logEntryList = logEntries.getAll();
360 
361         assertEquals(1, logEntryList.size());
362 
363         final LogEntry logEntry = logEntryList.get(0);
364         assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("Assertion failed: the # is not even"));
365     }
366 
367     /**
368      * @throws Exception if the test fails
369      */
370     @Test
371     @BuggyWebDriver
372     public void assertObject() throws Exception {
373         final String html = DOCTYPE_HTML
374             + "<html>\n"
375             + "<body>\n"
376             + "<script>\n"
377             + "  var number = 1;\n"
378             + "  console.assert(number % 2 === 0, {number: number, errorMsg: 'the # is not even'});\n"
379             + "</script>\n"
380             + "</body></html>";
381 
382         final WebDriver driver = loadPage2(html);
383 
384         final Logs logs = driver.manage().logs();
385         final LogEntries logEntries = logs.get(LogType.BROWSER);
386         final List<LogEntry> logEntryList = logEntries.getAll();
387 
388         assertEquals(1, logEntryList.size());
389 
390         final LogEntry logEntry = logEntryList.get(0);
391         assertTrue(logEntry.getMessage(), logEntry.getMessage()
392                 .contains("Assertion failed: {\"number\":1,\"errorMsg\":\"the # is not even\"}"));
393     }
394 
395     /**
396      * @throws Exception if the test fails
397      */
398     @Test
399     @BuggyWebDriver
400     public void assertObjects() throws Exception {
401         final String html = DOCTYPE_HTML
402             + "<html>\n"
403             + "<body>\n"
404             + "<script>\n"
405             + "  number = 1;\n"
406             + "  console.assert(number % 2 === 0, {number: number}, {errorMsg: 'the # is not even'});\n"
407             + "</script>\n"
408             + "</body></html>";
409 
410         final WebDriver driver = loadPage2(html);
411 
412         final Logs logs = driver.manage().logs();
413         final LogEntries logEntries = logs.get(LogType.BROWSER);
414         final List<LogEntry> logEntryList = logEntries.getAll();
415 
416         assertEquals(1, logEntryList.size());
417 
418         final LogEntry logEntry = logEntryList.get(0);
419         assertTrue(logEntry.getMessage(), logEntry.getMessage()
420                 .contains("Assertion failed: {\"number\":1} {\"errorMsg\":\"the # is not even\"}"));
421     }
422 
423     /**
424      * @throws Exception if the test fails
425      */
426     @Test
427     @BuggyWebDriver
428     public void assertParams() throws Exception {
429         final String html = DOCTYPE_HTML
430             + "<html>\n"
431             + "<body>\n"
432             + "<script>\n"
433             + "  console.assert(false, 'the word is %s', 'foo');\n"
434             + "</script>\n"
435             + "</body></html>";
436 
437         final WebDriver driver = loadPage2(html);
438 
439         final Logs logs = driver.manage().logs();
440         final LogEntries logEntries = logs.get(LogType.BROWSER);
441         final List<LogEntry> logEntryList = logEntries.getAll();
442 
443         assertEquals(1, logEntryList.size());
444 
445         final LogEntry logEntry = logEntryList.get(0);
446         assertTrue(logEntry.getMessage(), logEntry.getMessage()
447                 .contains("Assertion failed: the word is foo"));
448     }
449 
450     /**
451      * @throws Exception if the test fails
452      */
453     @Test
454     @BuggyWebDriver
455     public void trace() throws Exception {
456         final String html
457             = "<html>\n"
458             + "<body>\n"
459             + "<script>\n"
460             + "  function foo() {\n"
461             + "    function bar() {\n"
462             + "      console.trace();\n"
463             + "    }\n"
464             + "    bar();\n"
465             + "  }\n"
466             + "  foo();\n"
467             + "</script>\n"
468             + "</body></html>";
469 
470         final WebDriver driver = loadPage2(html);
471 
472         final Logs logs = driver.manage().logs();
473         final LogEntries logEntries = logs.get(LogType.BROWSER);
474         final List<LogEntry> logEntryList = logEntries.getAll();
475 
476         assertEquals(1, logEntryList.size());
477 
478         final LogEntry logEntry = logEntryList.get(0);
479         final String logMsg = logEntry.getMessage();
480         assertTrue(logMsg, logMsg
481                 .matches("bar\\(\\)@script in http.*:6\\n"
482                         + "foo\\(\\)@script in http.*:8\\n"
483                         + "@script in http.*:10"));
484     }
485 
486     /**
487      * @throws Exception if the test fails
488      */
489     @Test
490     @BuggyWebDriver
491     public void errorCall() throws Exception {
492         final String html = DOCTYPE_HTML
493             + "<html>\n"
494             + "<body>\n"
495             + "<script>\n"
496             + "  function foo() {\n"
497             + "    (undefined || console.error)('he ho');\n"
498             + "  }\n"
499             + "  foo();\n"
500             + "</script>\n"
501             + "</body></html>";
502 
503         final WebDriver driver = loadPage2(html);
504 
505         final Logs logs = driver.manage().logs();
506         final LogEntries logEntries = logs.get(LogType.BROWSER);
507         final List<LogEntry> logEntryList = logEntries.getAll();
508 
509         assertEquals(1, logEntryList.size());
510 
511         final LogEntry logEntry = logEntryList.get(0);
512         final String logMsg = logEntry.getMessage();
513         assertTrue(logMsg, logMsg.contains("he ho"));
514     }
515 }