| 
									
										
										
										
											2020-08-04 16:32:10 -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-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2021-10-11 10:52:17 -04:00
										 |  |  | import type { Frame } from 'playwright-core'; | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | import { expectedSSLError } from '../config/utils'; | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [response] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation(), | 
					
						
							|  |  |  |     page.evaluate(url => window.location.href = url, server.PREFIX + '/grid.html') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  |   expect(response.url()).toContain('grid.html'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should respect timeout', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const promise = page.waitForNavigation({ url: '**/frame.html', timeout: 5000 }); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const error = await promise.catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('page.waitForNavigation: Timeout 5000ms exceeded.'); | 
					
						
							|  |  |  |   expect(error.message).toContain('waiting for navigation to "**/frame.html" until "load"'); | 
					
						
							|  |  |  |   expect(error.message).toContain(`navigated to "${server.EMPTY_PAGE}"`); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with both domcontentloaded and load', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   let response = null; | 
					
						
							|  |  |  |   server.setRoute('/one-style.css', (req, res) => response = res); | 
					
						
							|  |  |  |   const navigationPromise = page.goto(server.PREFIX + '/one-style.html'); | 
					
						
							|  |  |  |   const domContentLoadedPromise = page.waitForNavigation({ | 
					
						
							|  |  |  |     waitUntil: 'domcontentloaded' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let bothFired = false; | 
					
						
							|  |  |  |   const bothFiredPromise = Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation({ waitUntil: 'load' }), | 
					
						
							|  |  |  |     domContentLoadedPromise | 
					
						
							|  |  |  |   ]).then(() => bothFired = true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await server.waitForRequest('/one-style.css'); | 
					
						
							|  |  |  |   await domContentLoadedPromise; | 
					
						
							|  |  |  |   expect(bothFired).toBe(false); | 
					
						
							|  |  |  |   response.end(); | 
					
						
							|  |  |  |   await bothFiredPromise; | 
					
						
							|  |  |  |   await navigationPromise; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:12:19 -07:00
										 |  |  | it('should work with commit', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2023-01-11 10:16:51 -08:00
										 |  |  |   server.setRoute('/script.js', (req, res) => {}); | 
					
						
							| 
									
										
										
										
											2021-11-01 17:12:19 -07:00
										 |  |  |   server.setRoute('/empty.html', (req, res) => { | 
					
						
							| 
									
										
										
										
											2023-01-11 10:16:51 -08:00
										 |  |  |     res.setHeader('content-type', 'text/html'); | 
					
						
							|  |  |  |     res.end('<title>Hello</title><script src="script.js"></script>'); | 
					
						
							| 
									
										
										
										
											2021-11-01 17:12:19 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   page.goto(server.EMPTY_PAGE).catch(e => {}); | 
					
						
							|  |  |  |   await page.waitForNavigation({ waitUntil: 'commit' }); | 
					
						
							| 
									
										
										
										
											2023-01-11 10:16:51 -08:00
										 |  |  |   expect(await page.title()).toBe('Hello'); | 
					
						
							| 
									
										
										
										
											2021-11-01 17:12:19 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with clicking on anchor links', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent(`<a href='#foobar'>foobar</a>`); | 
					
						
							|  |  |  |   const [response] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation(), | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(response).toBe(null); | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.EMPTY_PAGE + '#foobar'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-31 11:24:04 -07:00
										 |  |  | it('should work with clicking on links which do not commit navigation', async ({ page, server, httpsServer, browserName, platform }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent(`<a href='${httpsServer.EMPTY_PAGE}'>foobar</a>`); | 
					
						
							|  |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation().catch(e => e), | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2023-08-23 17:59:07 +02:00
										 |  |  |   expect(error.message).toMatch(expectedSSLError(browserName, platform)); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with history.pushState()', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent(`
 | 
					
						
							|  |  |  |     <a onclick='javascript:pushState()'>SPA</a> | 
					
						
							|  |  |  |     <script> | 
					
						
							|  |  |  |       function pushState() { history.pushState({}, '', 'wow.html') } | 
					
						
							|  |  |  |     </script> | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |   const [response] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation(), | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(response).toBe(null); | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.PREFIX + '/wow.html'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with history.replaceState()', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent(`
 | 
					
						
							|  |  |  |     <a onclick='javascript:replaceState()'>SPA</a> | 
					
						
							|  |  |  |     <script> | 
					
						
							|  |  |  |       function replaceState() { history.replaceState({}, '', '/replaced.html') } | 
					
						
							|  |  |  |     </script> | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |   const [response] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation(), | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(response).toBe(null); | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.PREFIX + '/replaced.html'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with DOM history.back()/history.forward()', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.setContent(`
 | 
					
						
							|  |  |  |     <a id=back onclick='javascript:goBack()'>back</a> | 
					
						
							|  |  |  |     <a id=forward onclick='javascript:goForward()'>forward</a> | 
					
						
							|  |  |  |     <script> | 
					
						
							|  |  |  |       function goBack() { history.back(); } | 
					
						
							|  |  |  |       function goForward() { history.forward(); } | 
					
						
							|  |  |  |       history.pushState({}, '', '/first.html'); | 
					
						
							|  |  |  |       history.pushState({}, '', '/second.html'); | 
					
						
							|  |  |  |     </script> | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.PREFIX + '/second.html'); | 
					
						
							|  |  |  |   const [backResponse] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation(), | 
					
						
							|  |  |  |     page.click('a#back'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(backResponse).toBe(null); | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.PREFIX + '/first.html'); | 
					
						
							|  |  |  |   const [forwardResponse] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForNavigation(), | 
					
						
							|  |  |  |     page.click('a#forward'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(forwardResponse).toBe(null); | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.PREFIX + '/second.html'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 13:48:05 -07:00
										 |  |  | it('should work when subframe issues window.stop()', async ({ browserName, page, server }) => { | 
					
						
							| 
									
										
										
										
											2022-11-07 15:35:21 -08:00
										 |  |  |   it.fixme(browserName === 'webkit', 'WebKit issues load event in some cases, but not always'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   server.setRoute('/frames/style.css', (req, res) => {}); | 
					
						
							| 
									
										
										
										
											2022-08-26 13:48:05 -07:00
										 |  |  |   let done = false; | 
					
						
							|  |  |  |   page.goto(server.PREFIX + '/frames/one-frame.html').then(() => done = true).catch(() => {}); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   const frame = await new Promise<Frame>(f => page.once('frameattached', f)); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |   await new Promise<void>(fulfill => page.on('framenavigated', f => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     if (f === frame) | 
					
						
							|  |  |  |       fulfill(); | 
					
						
							|  |  |  |   })); | 
					
						
							| 
									
										
										
										
											2022-08-26 13:48:05 -07:00
										 |  |  |   await frame.evaluate(() => window.stop()); | 
					
						
							| 
									
										
										
										
											2022-11-07 15:35:21 -08:00
										 |  |  |   expect(done).toBe(true); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with url match', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   let response1 = null; | 
					
						
							|  |  |  |   const response1Promise = page.waitForNavigation({ url: /one-style\.html/ }).then(response => response1 = response); | 
					
						
							|  |  |  |   let response2 = null; | 
					
						
							|  |  |  |   const response2Promise = page.waitForNavigation({ url: /\/frame.html/ }).then(response => response2 = response); | 
					
						
							|  |  |  |   let response3 = null; | 
					
						
							|  |  |  |   const response3Promise = page.waitForNavigation({ url: url => url.searchParams.get('foo') === 'bar' }).then(response => response3 = response); | 
					
						
							|  |  |  |   expect(response1).toBe(null); | 
					
						
							|  |  |  |   expect(response2).toBe(null); | 
					
						
							|  |  |  |   expect(response3).toBe(null); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(response1).toBe(null); | 
					
						
							|  |  |  |   expect(response2).toBe(null); | 
					
						
							|  |  |  |   expect(response3).toBe(null); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/frame.html'); | 
					
						
							|  |  |  |   expect(response1).toBe(null); | 
					
						
							|  |  |  |   await response2Promise; | 
					
						
							|  |  |  |   expect(response2).not.toBe(null); | 
					
						
							|  |  |  |   expect(response3).toBe(null); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/one-style.html'); | 
					
						
							|  |  |  |   await response1Promise; | 
					
						
							|  |  |  |   expect(response1).not.toBe(null); | 
					
						
							|  |  |  |   expect(response2).not.toBe(null); | 
					
						
							|  |  |  |   expect(response3).toBe(null); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/frame.html?foo=bar'); | 
					
						
							|  |  |  |   await response3Promise; | 
					
						
							|  |  |  |   expect(response1).not.toBe(null); | 
					
						
							|  |  |  |   expect(response2).not.toBe(null); | 
					
						
							|  |  |  |   expect(response3).not.toBe(null); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   expect(response1.url()).toBe(server.PREFIX + '/one-style.html'); | 
					
						
							|  |  |  |   expect(response2.url()).toBe(server.PREFIX + '/frame.html'); | 
					
						
							|  |  |  |   expect(response3.url()).toBe(server.PREFIX + '/frame.html?foo=bar'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with url match for same document navigations', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let resolved = false; | 
					
						
							|  |  |  |   const waitPromise = page.waitForNavigation({ url: /third\.html/ }).then(() => resolved = true); | 
					
						
							|  |  |  |   expect(resolved).toBe(false); | 
					
						
							|  |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     history.pushState({}, '', '/first.html'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(resolved).toBe(false); | 
					
						
							|  |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     history.pushState({}, '', '/second.html'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(resolved).toBe(false); | 
					
						
							|  |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     history.pushState({}, '', '/third.html'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await waitPromise; | 
					
						
							|  |  |  |   expect(resolved).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work for cross-process navigations', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   const waitPromise = page.waitForNavigation({ waitUntil: 'domcontentloaded' }); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const url = server.CROSS_PROCESS_PREFIX + '/empty.html'; | 
					
						
							|  |  |  |   const gotoPromise = page.goto(url); | 
					
						
							|  |  |  |   const response = await waitPromise; | 
					
						
							|  |  |  |   expect(response.url()).toBe(url); | 
					
						
							|  |  |  |   expect(page.url()).toBe(url); | 
					
						
							|  |  |  |   expect(await page.evaluate('document.location.href')).toBe(url); | 
					
						
							|  |  |  |   await gotoPromise; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work on frame', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/frames/one-frame.html'); | 
					
						
							|  |  |  |   const frame = page.frames()[1]; | 
					
						
							|  |  |  |   const [response] = await Promise.all([ | 
					
						
							|  |  |  |     frame.waitForNavigation(), | 
					
						
							|  |  |  |     frame.evaluate(url => window.location.href = url, server.PREFIX + '/grid.html') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  |   expect(response.url()).toContain('grid.html'); | 
					
						
							|  |  |  |   expect(response.frame()).toBe(frame); | 
					
						
							|  |  |  |   expect(page.url()).toContain('/frames/one-frame.html'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should fail when frame detaches', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/frames/one-frame.html'); | 
					
						
							|  |  |  |   const frame = page.frames()[1]; | 
					
						
							|  |  |  |   server.setRoute('/empty.html', () => {}); | 
					
						
							| 
									
										
										
										
											2023-03-10 09:25:54 -08:00
										 |  |  |   server.setRoute('/one-style.css', () => {}); | 
					
						
							| 
									
										
										
										
											2021-09-22 17:09:02 -07:00
										 |  |  |   const [error] = await Promise.all([ | 
					
						
							|  |  |  |     frame.waitForNavigation().catch(e => e), | 
					
						
							| 
									
										
										
										
											2023-03-10 09:25:54 -08:00
										 |  |  |     page.$eval('iframe', frame => { frame.contentWindow.location.href = '/one-style.html'; }), | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:43 -08:00
										 |  |  |     // Make sure policy checks pass and navigation actually begins before removing the frame to avoid other errors
 | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |     server.waitForRequest('/one-style.css').then(() => page.$eval('iframe', frame => window.builtinSetTimeout(() => frame.remove(), 0))) | 
					
						
							| 
									
										
										
										
											2021-09-22 17:09:02 -07:00
										 |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   expect(error.message).toContain('waiting for navigation until "load"'); | 
					
						
							|  |  |  |   expect(error.message).toContain('frame was detached'); | 
					
						
							|  |  |  | }); |