| 
									
										
										
										
											2020-08-03 13:41:48 -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-16 19:19:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:32:12 -07:00
										 |  |  | import './playwright.fixtures'; | 
					
						
							| 
									
										
										
										
											2020-08-16 19:19:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 16:04:27 -07:00
										 |  |  | import { registerFixture } from '../test-runner'; | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 11:19:15 -07:00
										 |  |  | import path from 'path'; | 
					
						
							|  |  |  | import fs from 'fs'; | 
					
						
							|  |  |  | import os from 'os'; | 
					
						
							|  |  |  | import {mkdtempAsync, removeFolderAsync} from './utils'; | 
					
						
							|  |  |  | import { Browser, BrowserContext } from '..'; | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 11:19:15 -07:00
										 |  |  | declare global { | 
					
						
							| 
									
										
										
										
											2020-08-20 12:51:05 -07:00
										 |  |  |   interface TestState { | 
					
						
							| 
									
										
										
										
											2020-08-07 11:19:15 -07:00
										 |  |  |     downloadsBrowser: Browser; | 
					
						
							|  |  |  |     persistentDownloadsContext: BrowserContext; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  | registerFixture('downloadsBrowser', async ({server, browserType, defaultBrowserOptions, tmpDir}, test) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   server.setRoute('/download', (req, res) => { | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/octet-stream'); | 
					
						
							|  |  |  |     res.setHeader('Content-Disposition', 'attachment; filename=file.txt'); | 
					
						
							|  |  |  |     res.end(`Hello world`); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const browser = await browserType.launch({ | 
					
						
							|  |  |  |     ...defaultBrowserOptions, | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  |     downloadsPath: tmpDir, | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-12 17:51:07 -07:00
										 |  |  |   await test(browser); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  | registerFixture('persistentDownloadsContext', async ({server, browserType, defaultBrowserOptions, tmpDir}, test) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const userDataDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-')); | 
					
						
							|  |  |  |   server.setRoute('/download', (req, res) => { | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/octet-stream'); | 
					
						
							|  |  |  |     res.setHeader('Content-Disposition', 'attachment; filename=file.txt'); | 
					
						
							|  |  |  |     res.end(`Hello world`); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const context = await browserType.launchPersistentContext( | 
					
						
							|  |  |  |     userDataDir, | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ...defaultBrowserOptions, | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  |       downloadsPath: tmpDir, | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |       acceptDownloads: true | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   const page = context.pages()[0]; | 
					
						
							|  |  |  |   page.setContent(`<a href="${server.PREFIX}/download">download</a>`); | 
					
						
							| 
									
										
										
										
											2020-08-12 17:51:07 -07:00
										 |  |  |   await test(context); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  |   await removeFolderAsync(userDataDir); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  | it('should keep downloadsPath folder', async({downloadsBrowser, tmpDir, server})  => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const page = await downloadsBrowser.newPage(); | 
					
						
							|  |  |  |   await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); | 
					
						
							|  |  |  |   const [ download ] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('download'), | 
					
						
							|  |  |  |     page.click('a') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(download.url()).toBe(`${server.PREFIX}/download`); | 
					
						
							|  |  |  |   expect(download.suggestedFilename()).toBe(`file.txt`); | 
					
						
							| 
									
										
										
										
											2020-08-07 11:19:15 -07:00
										 |  |  |   await download.path().catch(e => void 0); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   await page.close(); | 
					
						
							|  |  |  |   await downloadsBrowser.close(); | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  |   expect(fs.existsSync(tmpDir)).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  | it('should delete downloads when context closes', async({downloadsBrowser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const page = await downloadsBrowser.newPage({ acceptDownloads: true }); | 
					
						
							|  |  |  |   await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); | 
					
						
							|  |  |  |   const [ download ] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('download'), | 
					
						
							|  |  |  |     page.click('a') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   const path = await download.path(); | 
					
						
							|  |  |  |   expect(fs.existsSync(path)).toBeTruthy(); | 
					
						
							|  |  |  |   await page.close(); | 
					
						
							|  |  |  |   expect(fs.existsSync(path)).toBeFalsy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  | it('should report downloads in downloadsPath folder', async({downloadsBrowser, tmpDir, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const page = await downloadsBrowser.newPage({ acceptDownloads: true }); | 
					
						
							|  |  |  |   await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); | 
					
						
							|  |  |  |   const [ download ] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('download'), | 
					
						
							|  |  |  |     page.click('a') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   const path = await download.path(); | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  |   expect(path.startsWith(tmpDir)).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   await page.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  | it('should accept downloads', async({persistentDownloadsContext, tmpDir, server})  => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const page = persistentDownloadsContext.pages()[0]; | 
					
						
							|  |  |  |   const [ download ] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('download'), | 
					
						
							|  |  |  |     page.click('a') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(download.url()).toBe(`${server.PREFIX}/download`); | 
					
						
							|  |  |  |   expect(download.suggestedFilename()).toBe(`file.txt`); | 
					
						
							|  |  |  |   const path = await download.path(); | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  |   expect(path.startsWith(tmpDir)).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should not delete downloads when the context closes', async({persistentDownloadsContext}) => { | 
					
						
							|  |  |  |   const page = persistentDownloadsContext.pages()[0]; | 
					
						
							|  |  |  |   const [ download ] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('download'), | 
					
						
							|  |  |  |     page.click('a') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   const path = await download.path(); | 
					
						
							|  |  |  |   await persistentDownloadsContext.close(); | 
					
						
							|  |  |  |   expect(fs.existsSync(path)).toBeTruthy(); | 
					
						
							|  |  |  | }); |