| 
									
										
										
										
											2020-08-03 15:23:53 -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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-26 10:59:27 -07:00
										 |  |  | import { it, expect } from './fixtures'; | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work', async ({page}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(() => window['__popup'] = window.open('about:blank')), | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with window features', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(() => window['__popup'] = window.open(window.location.href, 'Title', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top=0,left=0')), | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 23:46:12 +01:00
										 |  |  | it('should emit for immediately closed popups', async ({browser}) => { | 
					
						
							|  |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							|  |  |  |       const win = window.open('about:blank'); | 
					
						
							|  |  |  |       win.close(); | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(popup).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2020-12-18 23:46:12 +01:00
										 |  |  |   await context.close(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 09:04:08 -08:00
										 |  |  | it('should emit for immediately closed popups 2', (test, {browserName, video}) => { | 
					
						
							|  |  |  |   test.fixme(browserName === 'firefox' && video); | 
					
						
							|  |  |  | }, async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							|  |  |  |       const win = window.open(window.location.href); | 
					
						
							|  |  |  |       win.close(); | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(popup).toBeTruthy(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 23:46:12 +01:00
										 |  |  | it('should be able to capture alert', async ({browser}) => { | 
					
						
							|  |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const evaluatePromise = page.evaluate(() => { | 
					
						
							|  |  |  |     const win = window.open(''); | 
					
						
							|  |  |  |     win.alert('hello'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const popup = await page.waitForEvent('popup'); | 
					
						
							|  |  |  |   const dialog = await popup.waitForEvent('dialog'); | 
					
						
							|  |  |  |   expect(dialog.message()).toBe('hello'); | 
					
						
							|  |  |  |   await dialog.dismiss(); | 
					
						
							|  |  |  |   await evaluatePromise; | 
					
						
							| 
									
										
										
										
											2020-12-18 23:46:12 +01:00
										 |  |  |   await context.close(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with empty url', async ({page}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(() => window['__popup'] = window.open('')), | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with noopener and no url', async ({page}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(() => window['__popup'] = window.open(undefined, null, 'noopener')), | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   // Chromium reports `about:blank#blocked` here.
 | 
					
						
							|  |  |  |   expect(popup.url().split('#')[0]).toBe('about:blank'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with noopener and about:blank', async ({page}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(() => window['__popup'] = window.open('about:blank', null, 'noopener')), | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with noopener and url', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(url => window['__popup'] = window.open(url, null, 'noopener'), server.EMPTY_PAGE), | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with clicking target=_blank', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent('<a target=_blank rel="opener" href="/one-style.html">yo</a>'); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with fake-clicking target=_blank and rel=noopener', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>'); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							|  |  |  |     page.$eval('a', a => a.click()), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:28:44 +01:00
										 |  |  | it('should work with clicking target=_blank and rel=noopener', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>'); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  |   expect(await popup.evaluate(() => !!window.opener)).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 23:46:12 +01:00
										 |  |  | it('should not treat navigations as new popups', async ({context, server}) => { | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>'); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   let badSecondPopup = false; | 
					
						
							|  |  |  |   page.on('popup', () => badSecondPopup = true); | 
					
						
							|  |  |  |   await popup.goto(server.CROSS_PROCESS_PREFIX + '/empty.html'); | 
					
						
							| 
									
										
										
										
											2020-12-18 23:46:12 +01:00
										 |  |  |   await context.close(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   expect(badSecondPopup).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 |