| 
									
										
										
										
											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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should fire', async ({ page, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-07-01 14:31:20 -07:00
										 |  |  |   const url = server.PREFIX + '/error.html'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							| 
									
										
										
										
											2021-07-01 14:31:20 -07:00
										 |  |  |     page.goto(url), | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(error.name).toBe('Error'); | 
					
						
							|  |  |  |   expect(error.message).toBe('Fancy error!'); | 
					
						
							| 
									
										
										
										
											2021-07-01 14:31:20 -07:00
										 |  |  |   if (browserName === 'chromium') { | 
					
						
							|  |  |  |     expect(error.stack).toBe(`Error: Fancy error!
 | 
					
						
							|  |  |  |     at c (myscript.js:14:11) | 
					
						
							|  |  |  |     at b (myscript.js:10:5) | 
					
						
							|  |  |  |     at a (myscript.js:6:5) | 
					
						
							|  |  |  |     at myscript.js:3:1`);
 | 
					
						
							|  |  |  |   } else if (browserName === 'webkit') { | 
					
						
							|  |  |  |     expect(error.stack).toBe(`Error: Fancy error!
 | 
					
						
							|  |  |  |     at c (${url}:14:36) | 
					
						
							|  |  |  |     at b (${url}:10:6) | 
					
						
							|  |  |  |     at a (${url}:6:6) | 
					
						
							|  |  |  |     at global code (${url}:3:2)`);
 | 
					
						
							|  |  |  |   } else if (browserName === 'firefox') { | 
					
						
							|  |  |  |     expect(error.stack).toBe(`Error: Fancy error!
 | 
					
						
							|  |  |  |     at c (myscript.js:14:11) | 
					
						
							|  |  |  |     at b (myscript.js:10:5) | 
					
						
							|  |  |  |     at a (myscript.js:6:5) | 
					
						
							|  |  |  |     at  (myscript.js:3:1)`);
 | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 13:23:23 -07:00
										 |  |  | it('should not receive console message for pageError', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   const messages = []; | 
					
						
							|  |  |  |   page.on('console', e => messages.push(e)); | 
					
						
							|  |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/error.html'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(messages.length).toBe(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should contain sourceURL', async ({ page, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   it.fail(browserName === 'webkit'); | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/error.html'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(error.stack).toContain('myscript.js'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 22:37:38 +02:00
										 |  |  | it('should contain the Error.name property', async ({ page }) => { | 
					
						
							|  |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |       window.builtinSetTimeout(() => { | 
					
						
							| 
									
										
										
										
											2021-04-19 22:37:38 +02:00
										 |  |  |         const error = new Error('my-message'); | 
					
						
							|  |  |  |         error.name = 'my-name'; | 
					
						
							|  |  |  |         throw error; | 
					
						
							|  |  |  |       }, 0); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(error.name).toBe('my-name'); | 
					
						
							|  |  |  |   expect(error.message).toBe('my-message'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should support an empty Error.name property', async ({ page }) => { | 
					
						
							|  |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |       window.builtinSetTimeout(() => { | 
					
						
							| 
									
										
										
										
											2021-04-19 22:37:38 +02:00
										 |  |  |         const error = new Error('my-message'); | 
					
						
							|  |  |  |         error.name = ''; | 
					
						
							|  |  |  |         throw error; | 
					
						
							|  |  |  |       }, 0); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(error.name).toBe(''); | 
					
						
							|  |  |  |   expect(error.message).toBe('my-message'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should handle odd values', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const cases = [ | 
					
						
							|  |  |  |     [null, 'null'], | 
					
						
							|  |  |  |     [undefined, 'undefined'], | 
					
						
							|  |  |  |     [0, '0'], | 
					
						
							|  |  |  |     ['', ''], | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  |   for (const [value, message] of cases) { | 
					
						
							|  |  |  |     const [error] = await Promise.all([ | 
					
						
							|  |  |  |       page.waitForEvent('pageerror'), | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |       page.evaluate(value => { | 
					
						
							|  |  |  |         window.builtinSetTimeout(() => { throw value; }, 0); | 
					
						
							|  |  |  |       }, value), | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2021-04-19 22:37:38 +02:00
										 |  |  |     expect(error.message).toBe(message); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should handle object', async ({ page, browserName }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |     page.evaluate(() => { | 
					
						
							|  |  |  |       window.builtinSetTimeout(() => { throw {}; }, 0); | 
					
						
							|  |  |  |     }), | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   expect(error.message).toBe(browserName === 'chromium' ? 'Object' : '[object Object]'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-28 15:41:32 +02:00
										 |  |  | it('should handle window', async ({ page, browserName }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |     page.evaluate(() => { | 
					
						
							|  |  |  |       window.builtinSetTimeout(() => { throw window; }, 0); | 
					
						
							|  |  |  |     }), | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   expect(error.message).toBe(browserName === 'chromium' ? 'Window' : '[object Window]'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-06-29 13:53:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should remove a listener of a non-existing event handler', async ({ page }) => { | 
					
						
							|  |  |  |   page.removeListener('pageerror', () => {}); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-07-05 15:46:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should emit error from unhandled rejects', async ({ page, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/14165' }); | 
					
						
							|  |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('pageerror'), | 
					
						
							|  |  |  |     page.setContent(`
 | 
					
						
							|  |  |  |         <script> | 
					
						
							|  |  |  |           Promise.reject(new Error('sad :(')); | 
					
						
							|  |  |  |         </script> | 
					
						
							|  |  |  |     `),
 | 
					
						
							|  |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2022-07-05 22:15:40 -07:00
										 |  |  |   expect(error.message).toContain('sad :('); | 
					
						
							| 
									
										
										
										
											2022-07-05 15:46:28 -07:00
										 |  |  | }); |