2020-04-03 03:56:14 +03: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2020-04-24 06:04:19 +03:00
|
|
|
const {FFOX, CHROMIUM, WEBKIT, MAC} = require('./utils').testOptions(browserType);
|
2020-04-03 03:56:14 +03:00
|
|
|
|
2020-04-09 01:19:09 +03:00
|
|
|
describe('Download', function() {
|
|
|
|
beforeEach(async(state) => {
|
|
|
|
state.server.setRoute('/download', (req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'application/octet-stream');
|
|
|
|
res.setHeader('Content-Disposition', 'attachment');
|
|
|
|
res.end(`Hello world`);
|
2020-04-03 03:56:14 +03:00
|
|
|
});
|
2020-04-09 01:19:09 +03:00
|
|
|
});
|
2020-04-03 03:56:14 +03:00
|
|
|
|
2020-04-09 01:19:09 +03:00
|
|
|
it('should report downloads with acceptDownloads: false', async({page, server}) => {
|
2020-04-22 20:15:02 +03:00
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
2020-04-09 01:19:09 +03:00
|
|
|
const [ download ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
let error;
|
|
|
|
expect(download.url()).toBe(`${server.PREFIX}/download`);
|
|
|
|
await download.path().catch(e => error = e);
|
|
|
|
expect(await download.failure()).toContain('acceptDownloads');
|
|
|
|
expect(error.message).toContain('acceptDownloads: true');
|
|
|
|
});
|
|
|
|
it('should report downloads with acceptDownloads: true', async({browser, server}) => {
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
2020-04-22 20:15:02 +03:00
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
2020-04-09 01:19:09 +03:00
|
|
|
const [ download ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
2020-04-21 06:23:09 +03:00
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const path = await download.path();
|
|
|
|
expect(fs.existsSync(path)).toBeTruthy();
|
|
|
|
expect(fs.readFileSync(path).toString()).toBe('Hello world');
|
|
|
|
await page.close();
|
|
|
|
});
|
|
|
|
it.fail(WEBKIT)('should report non-navigation downloads', async({browser, server}) => {
|
2020-04-22 20:15:02 +03:00
|
|
|
// Our WebKit embedder does not download in this case, although Safari does.
|
2020-04-21 06:23:09 +03:00
|
|
|
server.setRoute('/download', (req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'application/octet-stream');
|
|
|
|
res.end(`Hello world`);
|
|
|
|
});
|
|
|
|
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent(`<a download="file.txt" href="${server.PREFIX}/download">download</a>`);
|
|
|
|
const [ download ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
2020-04-09 01:19:09 +03:00
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const path = await download.path();
|
|
|
|
expect(fs.existsSync(path)).toBeTruthy();
|
|
|
|
expect(fs.readFileSync(path).toString()).toBe('Hello world');
|
|
|
|
await page.close();
|
|
|
|
});
|
2020-04-23 23:02:37 +03:00
|
|
|
it(`should report download path within page.on('download', …) handler for Files`, async({browser, server}) => {
|
2020-04-23 03:02:15 +03:00
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
2020-04-23 23:02:37 +03:00
|
|
|
const onDownloadPath = new Promise((res) => {
|
2020-04-23 03:02:15 +03:00
|
|
|
page.on('download', dl => {
|
|
|
|
dl.path().then(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
|
|
|
await page.click('a');
|
2020-04-23 23:02:37 +03:00
|
|
|
const path = await onDownloadPath;
|
|
|
|
expect(fs.readFileSync(path).toString()).toBe('Hello world');
|
|
|
|
await page.close();
|
|
|
|
})
|
2020-04-25 18:46:58 +03:00
|
|
|
it(`should report download path within page.on('download', …) handler for Blobs`, async({browser, server}) => {
|
2020-04-23 23:02:37 +03:00
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
|
|
|
const onDownloadPath = new Promise((res) => {
|
|
|
|
page.on('download', dl => {
|
|
|
|
dl.path().then(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/download-blob.html');
|
|
|
|
await page.click('a');
|
|
|
|
const path = await onDownloadPath;
|
2020-04-23 03:02:15 +03:00
|
|
|
expect(fs.readFileSync(path).toString()).toBe('Hello world');
|
|
|
|
await page.close();
|
|
|
|
})
|
2020-04-22 20:15:02 +03:00
|
|
|
it.skip(FFOX).fail(CHROMIUM || WEBKIT)('should report alt-click downloads', async({browser, server}) => {
|
|
|
|
// Firefox does not download on alt-click by default.
|
|
|
|
// Our WebKit embedder does not download on alt-click, although Safari does.
|
|
|
|
// Chromium hangs waiting for navigation because of Page.frameRequestedNavigation.
|
|
|
|
server.setRoute('/download', (req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'application/octet-stream');
|
|
|
|
res.end(`Hello world`);
|
|
|
|
});
|
|
|
|
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
|
|
|
const [ download ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a', { modifiers: ['Alt']})
|
|
|
|
]);
|
|
|
|
const path = await download.path();
|
|
|
|
expect(fs.existsSync(path)).toBeTruthy();
|
|
|
|
expect(fs.readFileSync(path).toString()).toBe('Hello world');
|
|
|
|
await page.close();
|
|
|
|
});
|
2020-04-25 19:35:35 +03:00
|
|
|
it.fail(CHROMIUM || WEBKIT || FFOX)('should report new window downloads', async({browser, server}) => {
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
|
|
|
await page.setContent(`<a target=_blank 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();
|
|
|
|
});
|
2020-04-09 01:19:09 +03:00
|
|
|
it('should delete file', async({browser, server}) => {
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
2020-04-22 20:15:02 +03:00
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
2020-04-09 01:19:09 +03:00
|
|
|
const [ download ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const path = await download.path();
|
|
|
|
expect(fs.existsSync(path)).toBeTruthy();
|
|
|
|
await download.delete();
|
|
|
|
expect(fs.existsSync(path)).toBeFalsy();
|
2020-04-10 04:14:29 +03:00
|
|
|
await page.close();
|
2020-04-09 01:19:09 +03:00
|
|
|
});
|
|
|
|
it('should expose stream', async({browser, server}) => {
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
2020-04-22 20:15:02 +03:00
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
2020-04-09 01:19:09 +03:00
|
|
|
const [ download ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const stream = await download.createReadStream();
|
|
|
|
let content = '';
|
|
|
|
stream.on('data', data => content += data.toString());
|
|
|
|
await new Promise(f => stream.on('end', f));
|
|
|
|
expect(content).toBe('Hello world');
|
|
|
|
stream.close();
|
2020-04-10 04:14:29 +03:00
|
|
|
await page.close();
|
2020-04-09 01:19:09 +03:00
|
|
|
});
|
|
|
|
it('should delete downloads on context destruction', async({browser, server}) => {
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
2020-04-22 20:15:02 +03:00
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
2020-04-09 01:19:09 +03:00
|
|
|
const [ download1 ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const [ download2 ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const path1 = await download1.path();
|
|
|
|
const path2 = await download2.path();
|
|
|
|
expect(fs.existsSync(path1)).toBeTruthy();
|
|
|
|
expect(fs.existsSync(path2)).toBeTruthy();
|
|
|
|
await page.context().close();
|
|
|
|
expect(fs.existsSync(path1)).toBeFalsy();
|
|
|
|
expect(fs.existsSync(path2)).toBeFalsy();
|
|
|
|
});
|
2020-04-13 08:19:26 +03:00
|
|
|
it('should delete downloads on browser gone', async ({ server, browserType, defaultBrowserOptions }) => {
|
2020-04-09 01:19:09 +03:00
|
|
|
const browser = await browserType.launch(defaultBrowserOptions);
|
|
|
|
const page = await browser.newPage({ acceptDownloads: true });
|
2020-04-22 20:15:02 +03:00
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
2020-04-09 01:19:09 +03:00
|
|
|
const [ download1 ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const [ download2 ] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click('a')
|
|
|
|
]);
|
|
|
|
const path1 = await download1.path();
|
|
|
|
const path2 = await download2.path();
|
|
|
|
expect(fs.existsSync(path1)).toBeTruthy();
|
|
|
|
expect(fs.existsSync(path2)).toBeTruthy();
|
|
|
|
await browser.close();
|
|
|
|
expect(fs.existsSync(path1)).toBeFalsy();
|
|
|
|
expect(fs.existsSync(path2)).toBeFalsy();
|
|
|
|
expect(fs.existsSync(path.join(path1, '..'))).toBeFalsy();
|
2020-04-03 03:56:14 +03:00
|
|
|
});
|
2020-04-09 01:19:09 +03:00
|
|
|
});
|