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