|
16 | 16 |
|
17 | 17 | const fs = require('fs');
|
18 | 18 | const path = require('path');
|
| 19 | +const util = require('util'); |
| 20 | +const os = require('os'); |
| 21 | +const removeFolder = require('rimraf'); |
| 22 | +const mkdtempAsync = util.promisify(fs.mkdtemp); |
| 23 | +const removeFolderAsync = util.promisify(removeFolder); |
| 24 | + |
19 | 25 | const {FFOX, CHROMIUM, WEBKIT, HEADLESS} = testOptions;
|
20 | 26 |
|
| 27 | +registerFixture('persistentDirectory', async ({}, test) => { |
| 28 | + const persistentDirectory = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-')); |
| 29 | + try { |
| 30 | + await test(persistentDirectory); |
| 31 | + } finally { |
| 32 | + await removeFolderAsync(persistentDirectory); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
21 | 36 | describe('Download', function() {
|
22 | 37 | beforeEach(async ({server}) => {
|
23 | 38 | server.setRoute('/download', (req, res) => {
|
@@ -57,6 +72,110 @@ describe('Download', function() {
|
57 | 72 | expect(fs.readFileSync(path).toString()).toBe('Hello world');
|
58 | 73 | await page.close();
|
59 | 74 | });
|
| 75 | + it('should save to user-specified path', async({persistentDirectory, browser, server}) => { |
| 76 | + const page = await browser.newPage({ acceptDownloads: true }); |
| 77 | + await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); |
| 78 | + const [ download ] = await Promise.all([ |
| 79 | + page.waitForEvent('download'), |
| 80 | + page.click('a') |
| 81 | + ]); |
| 82 | + const userPath = path.join(persistentDirectory, "download.txt"); |
| 83 | + await download.saveAs(userPath); |
| 84 | + expect(fs.existsSync(userPath)).toBeTruthy(); |
| 85 | + expect(fs.readFileSync(userPath).toString()).toBe('Hello world'); |
| 86 | + await page.close(); |
| 87 | + }); |
| 88 | + it('should save to user-specified path without updating original path', async({persistentDirectory, browser, server}) => { |
| 89 | + const page = await browser.newPage({ acceptDownloads: true }); |
| 90 | + await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); |
| 91 | + const [ download ] = await Promise.all([ |
| 92 | + page.waitForEvent('download'), |
| 93 | + page.click('a') |
| 94 | + ]); |
| 95 | + const userPath = path.join(persistentDirectory, "download.txt"); |
| 96 | + await download.saveAs(userPath); |
| 97 | + expect(fs.existsSync(userPath)).toBeTruthy(); |
| 98 | + expect(fs.readFileSync(userPath).toString()).toBe('Hello world'); |
| 99 | + |
| 100 | + const originalPath = await download.path(); |
| 101 | + expect(fs.existsSync(originalPath)).toBeTruthy(); |
| 102 | + expect(fs.readFileSync(originalPath).toString()).toBe('Hello world'); |
| 103 | + await page.close(); |
| 104 | + }); |
| 105 | + it('should save to two different paths with multiple saveAs calls', async({persistentDirectory, browser, server}) => { |
| 106 | + const page = await browser.newPage({ acceptDownloads: true }); |
| 107 | + await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); |
| 108 | + const [ download ] = await Promise.all([ |
| 109 | + page.waitForEvent('download'), |
| 110 | + page.click('a') |
| 111 | + ]); |
| 112 | + const userPath = path.join(persistentDirectory, "download.txt"); |
| 113 | + await download.saveAs(userPath); |
| 114 | + expect(fs.existsSync(userPath)).toBeTruthy(); |
| 115 | + expect(fs.readFileSync(userPath).toString()).toBe('Hello world'); |
| 116 | + |
| 117 | + const anotherUserPath = path.join(persistentDirectory, "download (2).txt"); |
| 118 | + await download.saveAs(anotherUserPath); |
| 119 | + expect(fs.existsSync(anotherUserPath)).toBeTruthy(); |
| 120 | + expect(fs.readFileSync(anotherUserPath).toString()).toBe('Hello world'); |
| 121 | + await page.close(); |
| 122 | + }); |
| 123 | + it('should save to overwritten filepath', async({persistentDirectory, browser, server}) => { |
| 124 | + const page = await browser.newPage({ acceptDownloads: true }); |
| 125 | + await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); |
| 126 | + const [ download ] = await Promise.all([ |
| 127 | + page.waitForEvent('download'), |
| 128 | + page.click('a') |
| 129 | + ]); |
| 130 | + const userPath = path.join(persistentDirectory, "download.txt"); |
| 131 | + await download.saveAs(userPath); |
| 132 | + expect((await util.promisify(fs.readdir)(persistentDirectory)).length).toBe(1); |
| 133 | + await download.saveAs(userPath); |
| 134 | + expect((await util.promisify(fs.readdir)(persistentDirectory)).length).toBe(1); |
| 135 | + expect(fs.existsSync(userPath)).toBeTruthy(); |
| 136 | + expect(fs.readFileSync(userPath).toString()).toBe('Hello world'); |
| 137 | + await page.close(); |
| 138 | + }); |
| 139 | + it('should error when saving to non-existent user-specified path', async({persistentDirectory, browser, server}) => { |
| 140 | + const page = await browser.newPage({ acceptDownloads: true }); |
| 141 | + await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); |
| 142 | + const [ download ] = await Promise.all([ |
| 143 | + page.waitForEvent('download'), |
| 144 | + page.click('a') |
| 145 | + ]); |
| 146 | + const nonExistentUserPath = path.join(persistentDirectory, "does-not-exist","download.txt"); |
| 147 | + const { message } = await download.saveAs(nonExistentUserPath).catch(e => e); |
| 148 | + expect(message).toContain('ENOENT'); |
| 149 | + expect(message).toContain('copyfile'); |
| 150 | + expect(message).toContain('no such file or directory'); |
| 151 | + expect(message).toContain('does-not-exist'); |
| 152 | + await page.close(); |
| 153 | + }); |
| 154 | + it('should error when saving with downloads disabled', async({persistentDirectory, browser, server}) => { |
| 155 | + const page = await browser.newPage({ acceptDownloads: false }); |
| 156 | + await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); |
| 157 | + const [ download ] = await Promise.all([ |
| 158 | + page.waitForEvent('download'), |
| 159 | + page.click('a') |
| 160 | + ]); |
| 161 | + const userPath = path.join(persistentDirectory, "download.txt"); |
| 162 | + const { message } = await download.saveAs(userPath).catch(e => e); |
| 163 | + expect(message).toContain('Pass { acceptDownloads: true } when you are creating your browser context'); |
| 164 | + await page.close(); |
| 165 | + }); |
| 166 | + it('should error when saving after deletion', async({persistentDirectory, browser, server}) => { |
| 167 | + const page = await browser.newPage({ acceptDownloads: true }); |
| 168 | + await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); |
| 169 | + const [ download ] = await Promise.all([ |
| 170 | + page.waitForEvent('download'), |
| 171 | + page.click('a') |
| 172 | + ]); |
| 173 | + const userPath = path.join(persistentDirectory, "download.txt"); |
| 174 | + await download.delete(); |
| 175 | + const { message } = await download.saveAs(userPath).catch(e => e); |
| 176 | + expect(message).toContain('Download already deleted. Save before deleting.'); |
| 177 | + await page.close(); |
| 178 | + }); |
60 | 179 | it('should report non-navigation downloads', async({browser, server}) => {
|
61 | 180 | // Mac WebKit embedder does not download in this case, although Safari does.
|
62 | 181 | server.setRoute('/download', (req, res) => {
|
|
0 commit comments