| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  | import util from 'util'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  | it('should work @smoke', async ({ page, browserName }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let message = null; | 
					
						
							|  |  |  |   page.once('console', m => message = m); | 
					
						
							|  |  |  |   await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     page.evaluate(() => console.log('hello', 5, { foo: 'bar' })), | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     page.waitForEvent('console') | 
					
						
							|  |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  |   if (browserName !== 'firefox') | 
					
						
							|  |  |  |     expect(message.text()).toEqual('hello 5 {foo: bar}'); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(message.text()).toEqual('hello 5 JSHandle@object'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(message.type()).toEqual('log'); | 
					
						
							|  |  |  |   expect(await message.args()[0].jsonValue()).toEqual('hello'); | 
					
						
							|  |  |  |   expect(await message.args()[1].jsonValue()).toEqual(5); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   expect(await message.args()[2].jsonValue()).toEqual({ foo: 'bar' }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should emit same log twice', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const messages = []; | 
					
						
							|  |  |  |   page.on('console', m => messages.push(m.text())); | 
					
						
							| 
									
										
										
										
											2024-02-26 16:52:28 -08:00
										 |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     for (let i = 0; i < 2; ++i) | 
					
						
							|  |  |  |       console.log('hello'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(messages).toEqual(['hello', 'hello']); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should use text() for inspection', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let text; | 
					
						
							|  |  |  |   const inspect = value => { | 
					
						
							|  |  |  |     text = util.inspect(value); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   page.on('console', inspect); | 
					
						
							|  |  |  |   await page.evaluate(() => console.log('Hello world')); | 
					
						
							|  |  |  |   expect(text).toEqual('Hello world'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work for different console API calls', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const messages = []; | 
					
						
							|  |  |  |   page.on('console', msg => messages.push(msg)); | 
					
						
							|  |  |  |   // All console events will be reported before `page.evaluate` is finished.
 | 
					
						
							|  |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     // A pair of time/timeEnd generates only one Console API call.
 | 
					
						
							|  |  |  |     console.time('calling console.time'); | 
					
						
							|  |  |  |     console.timeEnd('calling console.time'); | 
					
						
							|  |  |  |     console.trace('calling console.trace'); | 
					
						
							|  |  |  |     console.dir('calling console.dir'); | 
					
						
							|  |  |  |     console.warn('calling console.warn'); | 
					
						
							|  |  |  |     console.error('calling console.error'); | 
					
						
							| 
									
										
										
										
											2021-11-29 22:20:46 +01:00
										 |  |  |     console.info('calling console.info'); | 
					
						
							|  |  |  |     console.debug('calling console.debug'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     console.log(Promise.resolve('should not wait until resolved!')); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-11-29 22:20:46 +01:00
										 |  |  |   // WebKit uses console.debug() to report binding calls, make sure they don't get reported.
 | 
					
						
							|  |  |  |   await page.exposeBinding('foobar', async (_, value) => page.evaluate(value => console.log(value), value)); | 
					
						
							|  |  |  |   await page.evaluate(() => window['foobar']('Using bindings')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(messages.map(msg => msg.type())).toEqual([ | 
					
						
							| 
									
										
										
										
											2021-11-29 22:20:46 +01:00
										 |  |  |     'timeEnd', 'trace', 'dir', 'warning', 'error', 'info', 'debug', 'log', 'log' | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(messages[0].text()).toContain('calling console.time'); | 
					
						
							|  |  |  |   expect(messages.slice(1).map(msg => msg.text())).toEqual([ | 
					
						
							|  |  |  |     'calling console.trace', | 
					
						
							|  |  |  |     'calling console.dir', | 
					
						
							|  |  |  |     'calling console.warn', | 
					
						
							|  |  |  |     'calling console.error', | 
					
						
							| 
									
										
										
										
											2021-11-29 22:20:46 +01:00
										 |  |  |     'calling console.info', | 
					
						
							|  |  |  |     'calling console.debug', | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  |     'Promise', | 
					
						
							| 
									
										
										
										
											2021-11-29 22:20:46 +01:00
										 |  |  |     'Using bindings', | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 02:18:20 +01:00
										 |  |  | it('should format the message correctly with time/timeLog/timeEnd', async ({ page, browserName }) => { | 
					
						
							|  |  |  |   it.fixme(browserName === 'firefox', 'https://github.com/microsoft/playwright/issues/10580'); | 
					
						
							|  |  |  |   const messages = []; | 
					
						
							|  |  |  |   page.on('console', msg => messages.push(msg)); | 
					
						
							|  |  |  |   await page.evaluate(async () => { | 
					
						
							|  |  |  |     console.time('foo time'); | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |     await new Promise(x => window.builtinSetTimeout(x, 100)); | 
					
						
							| 
									
										
										
										
											2021-11-30 02:18:20 +01:00
										 |  |  |     console.timeLog('foo time'); | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |     await new Promise(x => window.builtinSetTimeout(x, 100)); | 
					
						
							| 
									
										
										
										
											2021-11-30 02:18:20 +01:00
										 |  |  |     console.timeEnd('foo time'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(messages.length).toBe(2); | 
					
						
							|  |  |  |   if (browserName === 'webkit') | 
					
						
							|  |  |  |     expect(messages[0].type()).toBe('timeEnd'); | 
					
						
							|  |  |  |   else if (browserName === 'chromium') | 
					
						
							|  |  |  |     expect(messages[0].type()).toBe('log'); | 
					
						
							|  |  |  |   expect(messages[1].type()).toBe('timeEnd'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // WebKit has a space before the unit: https://bugs.webkit.org/show_bug.cgi?id=233556
 | 
					
						
							|  |  |  |   expect(messages[0].text()).toMatch(/foo time: \d+.\d+ ?ms/); | 
					
						
							|  |  |  |   expect(messages[1].text()).toMatch(/foo time: \d+.\d+ ?ms/); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  | it('should not fail for window object', async ({ page, browserName }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let message = null; | 
					
						
							|  |  |  |   page.once('console', msg => message = msg); | 
					
						
							|  |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     page.evaluate(() => console.error(window)), | 
					
						
							|  |  |  |     page.waitForEvent('console') | 
					
						
							|  |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  |   if (browserName !== 'firefox') | 
					
						
							|  |  |  |     expect(message.text()).toEqual('Window'); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(message.text()).toEqual('JSHandle@object'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should trigger correct Log', async ({ page, server, browserName, isWindows }) => { | 
					
						
							| 
									
										
										
										
											2021-08-25 14:27:49 -07:00
										 |  |  |   it.skip(browserName === 'webkit' && isWindows, 'Upstream issue https://bugs.webkit.org/show_bug.cgi?id=229515'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto('about:blank'); | 
					
						
							|  |  |  |   const [message] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('console'), | 
					
						
							|  |  |  |     page.evaluate(async url => fetch(url).catch(e => {}), server.EMPTY_PAGE) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(message.text()).toContain('Access-Control-Allow-Origin'); | 
					
						
							|  |  |  |   expect(message.type()).toEqual('error'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should have location for console API calls', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [message] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2022-08-26 08:56:31 -07:00
										 |  |  |     page.waitForEvent('console', m => m.text().startsWith('here:')), | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     page.goto(server.PREFIX + '/consolelog.html'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(message.type()).toBe('log'); | 
					
						
							|  |  |  |   const location = message.location(); | 
					
						
							|  |  |  |   // Engines have different column notion.
 | 
					
						
							|  |  |  |   delete location.columnNumber; | 
					
						
							|  |  |  |   expect(location).toEqual({ | 
					
						
							|  |  |  |     url: server.PREFIX + '/consolelog.html', | 
					
						
							|  |  |  |     lineNumber: 7, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should not throw when there are console messages in detached iframes', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   // @see https://github.com/GoogleChrome/puppeteer/issues/3865
 | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(async () => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |       // 1. Create a popup that Playwright is not connected to.
 | 
					
						
							|  |  |  |       const win = window.open(''); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |       window['_popup'] = win; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |       if (window.document.readyState !== 'complete') | 
					
						
							|  |  |  |         await new Promise(f => window.addEventListener('load', f)); | 
					
						
							|  |  |  |       // 2. In this popup, create an iframe that console.logs a message.
 | 
					
						
							|  |  |  |       win.document.body.innerHTML = `<iframe src='/consolelog.html'></iframe>`; | 
					
						
							|  |  |  |       const frame = win.document.querySelector('iframe'); | 
					
						
							|  |  |  |       if (!frame.contentDocument || frame.contentDocument.readyState !== 'complete') | 
					
						
							|  |  |  |         await new Promise(f => frame.addEventListener('load', f)); | 
					
						
							|  |  |  |       // 3. After that, remove the iframe.
 | 
					
						
							|  |  |  |       frame.remove(); | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   // 4. Connect to the popup and make sure it doesn't throw.
 | 
					
						
							|  |  |  |   expect(await popup.evaluate('1 + 1')).toBe(2); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should use object previews for arrays and objects', async ({ page, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  |   let text: string; | 
					
						
							|  |  |  |   page.on('console', message => { | 
					
						
							|  |  |  |     text = message.text(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await page.evaluate(() => console.log([1, 2, 3], { a: 1 }, window)); | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (browserName !== 'firefox') | 
					
						
							| 
									
										
										
											
												fix: better formatting for sparse arrays (#20379)
Right now arrays preview yields all array elements. In case
of a sparse array with a single element on index 10000000,
this results in a large string that OOM Node.js.
This patch changes pretty-printing. For example:
```ts
// Given this array
const a = [];
a[10] = 1;
// Before this patch, pretty printing will yield:
"[,,,,,,,,1]"
// With this patch, pretty printing yields:
"[empty x 9, 1]"
```
The new array pretty-printing is equal to what Chrome DevTools
do to render sparse arrays.
Fixes #20347
											
										 
											2023-01-27 05:07:55 -08:00
										 |  |  |     expect(text).toEqual('[1, 2, 3] {a: 1} Window'); | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  |   else | 
					
						
							|  |  |  |     expect(text).toEqual('Array JSHandle@object JSHandle@object'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should use object previews for errors', async ({ page, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-08-06 11:37:36 -07:00
										 |  |  |   let text: string; | 
					
						
							|  |  |  |   page.on('console', message => { | 
					
						
							|  |  |  |     text = message.text(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.evaluate(() => console.log(new Error('Exception'))); | 
					
						
							|  |  |  |   if (browserName === 'chromium') | 
					
						
							|  |  |  |     expect(text).toContain('.evaluate'); | 
					
						
							|  |  |  |   if (browserName === 'webkit') | 
					
						
							|  |  |  |     expect(text).toEqual('Error: Exception'); | 
					
						
							|  |  |  |   if (browserName === 'firefox') | 
					
						
							|  |  |  |     expect(text).toEqual('Error'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-05-09 09:34:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('do not update console count on unhandled rejections', async ({ page }) => { | 
					
						
							|  |  |  |   const messages: string[] = []; | 
					
						
							|  |  |  |   const consoleEventListener = m => messages.push(m.text()); | 
					
						
							|  |  |  |   page.addListener('console', consoleEventListener); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     const fail = async () => Promise.reject(new Error('error')); | 
					
						
							|  |  |  |     console.log('begin'); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void fail(); | 
					
						
							|  |  |  |     void fail(); | 
					
						
							| 
									
										
										
										
											2023-05-09 09:34:57 -07:00
										 |  |  |     fail().catch(() => { | 
					
						
							|  |  |  |       console.log('end'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await expect.poll(() => messages).toEqual(['begin', 'end']); | 
					
						
							|  |  |  | }); |