| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 11:11:32 -07:00
										 |  |  | import { playwrightTest as it, expect } from './config/browserTest'; | 
					
						
							| 
									
										
										
										
											2020-08-07 11:19:15 -07:00
										 |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2021-05-05 17:07:10 +02:00
										 |  |  | import path from 'path'; | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  | it.describe('downloads path', () => { | 
					
						
							| 
									
										
										
										
											2021-10-14 10:41:03 -07:00
										 |  |  |   it.skip(({ mode }) => mode === 'service', 'download.path() is not available in remote mode'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it.beforeEach(async ({ server }) => { | 
					
						
							| 
									
										
										
										
											2021-04-01 13:18:04 -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`); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-10-12 09:16:02 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should keep downloadsPath folder', async ({ browserType, server }, testInfo)  => { | 
					
						
							|  |  |  |     const downloadsBrowser = await browserType.launch({ downloadsPath: testInfo.outputPath('') }); | 
					
						
							| 
									
										
										
										
											2021-04-01 13:18:04 -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`); | 
					
						
							|  |  |  |     await download.path().catch(e => void 0); | 
					
						
							|  |  |  |     await page.close(); | 
					
						
							|  |  |  |     await downloadsBrowser.close(); | 
					
						
							|  |  |  |     expect(fs.existsSync(testInfo.outputPath(''))).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2020-10-12 09:16:02 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should delete downloads when context closes', async ({ browserType, server }, testInfo) => { | 
					
						
							|  |  |  |     const downloadsBrowser = await browserType.launch({ downloadsPath: testInfo.outputPath('') }); | 
					
						
							| 
									
										
										
										
											2021-12-06 09:25:24 -08:00
										 |  |  |     const page = await downloadsBrowser.newPage(); | 
					
						
							| 
									
										
										
										
											2021-04-01 13:18:04 -07:00
										 |  |  |     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(); | 
					
						
							|  |  |  |     await downloadsBrowser.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should report downloads in downloadsPath folder', async ({ browserType, server }, testInfo) => { | 
					
						
							|  |  |  |     const downloadsBrowser = await browserType.launch({ downloadsPath: testInfo.outputPath('') }); | 
					
						
							| 
									
										
										
										
											2021-12-06 09:25:24 -08:00
										 |  |  |     const page = await downloadsBrowser.newPage(); | 
					
						
							| 
									
										
										
										
											2021-04-01 13:18:04 -07:00
										 |  |  |     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(path.startsWith(testInfo.outputPath(''))).toBeTruthy(); | 
					
						
							|  |  |  |     await page.close(); | 
					
						
							|  |  |  |     await downloadsBrowser.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should report downloads in downloadsPath folder with a relative path', async ({ browserType, server }, testInfo) => { | 
					
						
							|  |  |  |     const downloadsBrowser = await browserType.launch({ downloadsPath: path.relative(process.cwd(), testInfo.outputPath('')) }); | 
					
						
							| 
									
										
										
										
											2021-12-06 09:25:24 -08:00
										 |  |  |     const page = await downloadsBrowser.newPage(); | 
					
						
							| 
									
										
										
										
											2021-05-05 17:07:10 +02:00
										 |  |  |     await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); | 
					
						
							|  |  |  |     const [ download ] = await Promise.all([ | 
					
						
							|  |  |  |       page.waitForEvent('download'), | 
					
						
							|  |  |  |       page.click('a') | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     const downloadPath = await download.path(); | 
					
						
							|  |  |  |     expect(downloadPath.startsWith(testInfo.outputPath(''))).toBeTruthy(); | 
					
						
							|  |  |  |     await page.close(); | 
					
						
							|  |  |  |     await downloadsBrowser.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should accept downloads in persistent context', async ({ launchPersistent, server }, testInfo)  => { | 
					
						
							| 
									
										
										
										
											2021-12-06 09:25:24 -08:00
										 |  |  |     const { context, page } = await launchPersistent({ downloadsPath: testInfo.outputPath('') }); | 
					
						
							| 
									
										
										
										
											2021-04-01 13:18:04 -07:00
										 |  |  |     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`); | 
					
						
							|  |  |  |     const path = await download.path(); | 
					
						
							|  |  |  |     expect(path.startsWith(testInfo.outputPath(''))).toBeTruthy(); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should delete downloads when persistent context closes', async ({ launchPersistent, server }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2021-12-06 09:25:24 -08:00
										 |  |  |     const { context, page } = await launchPersistent({ downloadsPath: testInfo.outputPath('') }); | 
					
						
							| 
									
										
										
										
											2021-04-01 13:18:04 -07:00
										 |  |  |     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 context.close(); | 
					
						
							|  |  |  |     expect(fs.existsSync(path)).toBeFalsy(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); |