| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 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-09-02 21:43:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { browserTest as it, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should have url', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   const [otherPage] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.evaluate(url => window.open(url), server.EMPTY_PAGE) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(otherPage.url()).toBe(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should have url after domcontentloaded', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   const [otherPage] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.evaluate(url => window.open(url), server.EMPTY_PAGE) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   await otherPage.waitForLoadState('domcontentloaded'); | 
					
						
							|  |  |  |   expect(otherPage.url()).toBe(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should have about:blank url with domcontentloaded', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   const [otherPage] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.evaluate(url => window.open(url), 'about:blank') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   await otherPage.waitForLoadState('domcontentloaded'); | 
					
						
							|  |  |  |   expect(otherPage.url()).toBe('about:blank'); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should have about:blank for empty url with domcontentloaded', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   const [otherPage] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.evaluate(() => window.open()) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   await otherPage.waitForLoadState('domcontentloaded'); | 
					
						
							|  |  |  |   expect(otherPage.url()).toBe('about:blank'); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should report when a new page is created and closed', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   const [otherPage] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.evaluate(url => window.open(url), server.CROSS_PROCESS_PREFIX + '/empty.html'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   // The url is about:blank in FF when 'page' event is fired.
 | 
					
						
							|  |  |  |   expect(otherPage.url()).toContain(server.CROSS_PROCESS_PREFIX); | 
					
						
							|  |  |  |   expect(await otherPage.evaluate(() => ['Hello', 'world'].join(' '))).toBe('Hello world'); | 
					
						
							|  |  |  |   expect(await otherPage.$('body')).toBeTruthy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let allPages = context.pages(); | 
					
						
							|  |  |  |   expect(allPages).toContain(page); | 
					
						
							|  |  |  |   expect(allPages).toContain(otherPage); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let closeEventReceived; | 
					
						
							|  |  |  |   otherPage.once('close', () => closeEventReceived = true); | 
					
						
							|  |  |  |   await otherPage.close(); | 
					
						
							|  |  |  |   expect(closeEventReceived).toBeTruthy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   allPages = context.pages(); | 
					
						
							|  |  |  |   expect(allPages).toContain(page); | 
					
						
							|  |  |  |   expect(allPages).not.toContain(otherPage); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should report initialized pages', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const pagePromise = context.waitForEvent('page'); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |   void context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const newPage = await pagePromise; | 
					
						
							|  |  |  |   expect(newPage.url()).toBe('about:blank'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const popupPromise = context.waitForEvent('page'); | 
					
						
							|  |  |  |   const evaluatePromise = newPage.evaluate(() => window.open('about:blank')); | 
					
						
							|  |  |  |   const popup = await popupPromise; | 
					
						
							|  |  |  |   expect(popup.url()).toBe('about:blank'); | 
					
						
							|  |  |  |   await evaluatePromise; | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should not crash while redirecting of original request was missed', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   let serverResponse = null; | 
					
						
							|  |  |  |   server.setRoute('/one-style.css', (req, res) => serverResponse = res); | 
					
						
							|  |  |  |   // Open a new page. Use window.open to connect to the page later.
 | 
					
						
							|  |  |  |   const [newPage] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.evaluate(url => window.open(url), server.PREFIX + '/one-style.html'), | 
					
						
							|  |  |  |     server.waitForRequest('/one-style.css') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   // Issue a redirect.
 | 
					
						
							|  |  |  |   serverResponse.writeHead(302, { location: '/injectedstyle.css' }); | 
					
						
							|  |  |  |   serverResponse.end(); | 
					
						
							|  |  |  |   await newPage.waitForLoadState('domcontentloaded'); | 
					
						
							|  |  |  |   expect(newPage.url()).toBe(server.PREFIX + '/one-style.html'); | 
					
						
							|  |  |  |   // Cleanup.
 | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should have an opener', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/popup/window-open.html') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(popup.url()).toBe(server.PREFIX + '/popup/popup.html'); | 
					
						
							|  |  |  |   expect(await popup.opener()).toBe(page); | 
					
						
							|  |  |  |   expect(await page.opener()).toBe(null); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should fire page lifecycle events', async function({ browser, server }) { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const events = []; | 
					
						
							|  |  |  |   context.on('page', async page => { | 
					
						
							|  |  |  |     events.push('CREATED: ' + page.url()); | 
					
						
							|  |  |  |     page.on('close', () => events.push('DESTROYED: ' + page.url())); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.close(); | 
					
						
							|  |  |  |   expect(events).toEqual([ | 
					
						
							|  |  |  |     'CREATED: about:blank', | 
					
						
							|  |  |  |     `DESTROYED: ${server.EMPTY_PAGE}` | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with Shift-clicking', async ({ browser, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent('<a href="/one-style.html">yo</a>'); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							|  |  |  |     page.click('a', { modifiers: ['Shift'] }), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(await popup.opener()).toBe(null); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 08:15:12 -07:00
										 |  |  | it('should work with Ctrl-clicking', async ({ browser, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   it.fixme(browserName === 'firefox', 'Reports an opener in this case.'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent('<a href="/one-style.html">yo</a>'); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							| 
									
										
										
										
											2024-04-29 08:15:12 -07:00
										 |  |  |     page.click('a', { modifiers: ['ControlOrMeta'] }), | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(await popup.opener()).toBe(null); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-01-28 11:47:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 08:15:12 -07:00
										 |  |  | it('should not hang on ctrl-click during provisional load', async ({ context, page, server, isWindows, browserName, isLinux }) => { | 
					
						
							| 
									
										
										
										
											2022-01-28 11:47:14 -08:00
										 |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/11595' }); | 
					
						
							|  |  |  |   it.skip(browserName === 'chromium', 'Chromium does not dispatch renderer messages while navigation is provisional.'); | 
					
						
							| 
									
										
										
										
											2022-02-03 09:09:38 -08:00
										 |  |  |   it.fixme(browserName === 'webkit' && isWindows, 'Timesout while trying to click'); | 
					
						
							| 
									
										
										
										
											2023-03-30 01:03:16 -07:00
										 |  |  |   it.fixme(browserName === 'webkit' && isLinux, 'Timesout while trying to click'); | 
					
						
							| 
									
										
										
										
											2022-01-28 11:47:14 -08:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent('<a href="/one-style.html">yo</a>'); | 
					
						
							|  |  |  |   server.setRoute('/slow.html', () => {}); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     context.waitForEvent('page'), | 
					
						
							| 
									
										
										
										
											2024-04-29 08:15:12 -07:00
										 |  |  |     server.waitForRequest('/slow.html').then(() => page.click('a', { modifiers: ['ControlOrMeta'] })), | 
					
						
							| 
									
										
										
										
											2022-01-28 11:47:14 -08:00
										 |  |  |     page.evaluate(url => setTimeout(() => location.href = url, 0), server.CROSS_PROCESS_PREFIX + '/slow.html'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(popup).toBeTruthy(); | 
					
						
							|  |  |  | }); |