| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test.describe('non-stalling evaluate', () => { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   test.skip(({ mode }) => mode !== 'default'); | 
					
						
							| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   test('should work', async ({ page, server, toImpl }) => { | 
					
						
							| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     const result = await toImpl(page.mainFrame()).nonStallingRawEvaluateInExistingMainContext('2+2'); | 
					
						
							|  |  |  |     expect(result).toBe(4); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   test('should throw while pending navigation', async ({ page, server, toImpl }) => { | 
					
						
							| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.evaluate(() => document.body.textContent = 'HELLO WORLD'); | 
					
						
							|  |  |  |     let error; | 
					
						
							|  |  |  |     await page.route('**/empty.html', async (route, request) => { | 
					
						
							|  |  |  |       error = await toImpl(page.mainFrame()).nonStallingRawEvaluateInExistingMainContext('2+2').catch(e => e); | 
					
						
							|  |  |  |       route.abort(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE).catch(() => {}); | 
					
						
							|  |  |  |     expect(error.message).toContain('Frame is currently attempting a navigation'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   test('should throw when no main execution context', async ({ page, toImpl }) => { | 
					
						
							| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  |     let errorPromise; | 
					
						
							|  |  |  |     page.on('frameattached', frame => { | 
					
						
							|  |  |  |       errorPromise = toImpl(frame).nonStallingRawEvaluateInExistingMainContext('2+2').catch(e => e); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.setContent('<iframe></iframe>'); | 
					
						
							|  |  |  |     const error = await errorPromise; | 
					
						
							| 
									
										
										
										
											2021-06-04 09:33:44 -07:00
										 |  |  |     // bail out if we accidentally succeeded
 | 
					
						
							|  |  |  |     if (error === 4) | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  |     // Testing this as a race.
 | 
					
						
							| 
									
										
										
										
											2021-06-08 16:27:44 -07:00
										 |  |  |     expect([ | 
					
						
							|  |  |  |       'Frame does not yet have a main execution context', | 
					
						
							| 
									
										
										
										
											2021-06-14 16:01:18 -07:00
										 |  |  |       'Frame is currently attempting a navigation', | 
					
						
							|  |  |  |       'Navigation interrupted the evaluation', | 
					
						
							| 
									
										
										
										
											2021-06-08 16:27:44 -07:00
										 |  |  |     ]).toContain(error.message); | 
					
						
							| 
									
										
										
										
											2021-04-29 09:28:19 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |