2020-08-03 23:41:48 +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.
|
|
|
|
*/
|
2020-08-17 05:19:52 +03:00
|
|
|
|
2020-10-12 19:16:02 +03:00
|
|
|
import { folio } from './fixtures';
|
2020-08-17 05:19:52 +03:00
|
|
|
|
2020-08-07 21:19:15 +03:00
|
|
|
import fs from 'fs';
|
2020-08-27 00:16:35 +03:00
|
|
|
import type { Browser, BrowserContext } from '..';
|
2020-08-03 23:41:48 +03:00
|
|
|
|
2020-09-11 07:31:46 +03:00
|
|
|
type TestState = {
|
|
|
|
downloadsBrowser: Browser;
|
|
|
|
persistentDownloadsContext: BrowserContext;
|
|
|
|
};
|
2020-10-14 08:40:25 +03:00
|
|
|
const fixtures = folio.extend<TestState>();
|
2020-08-03 23:41:48 +03:00
|
|
|
|
2020-10-13 23:18:36 +03:00
|
|
|
fixtures.downloadsBrowser.init(async ({ server, browserType, browserOptions, testInfo }, test) => {
|
2020-10-12 19:16:02 +03: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({
|
2020-10-13 23:18:36 +03:00
|
|
|
...browserOptions,
|
2020-10-12 19:16:02 +03:00
|
|
|
downloadsPath: testInfo.outputPath(''),
|
|
|
|
});
|
|
|
|
await test(browser);
|
|
|
|
await browser.close();
|
2020-08-03 23:41:48 +03:00
|
|
|
});
|
|
|
|
|
2020-10-12 23:48:56 +03:00
|
|
|
fixtures.persistentDownloadsContext.init(async ({ server, launchPersistent, testInfo }, test) => {
|
2020-10-12 19:16:02 +03: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`);
|
|
|
|
});
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('--- launching persistent context ---');
|
2020-10-12 19:16:02 +03:00
|
|
|
const { context, page } = await launchPersistent(
|
|
|
|
{
|
|
|
|
downloadsPath: testInfo.outputPath(''),
|
|
|
|
acceptDownloads: true
|
|
|
|
}
|
|
|
|
);
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('--- setting content for the page ---');
|
2020-10-12 19:16:02 +03:00
|
|
|
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('--- launching test ---');
|
2020-10-12 19:16:02 +03:00
|
|
|
await test(context);
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('--- closing context ---');
|
2020-10-12 19:16:02 +03:00
|
|
|
await context.close();
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('--- DONE ---');
|
2020-10-12 19:16:02 +03:00
|
|
|
});
|
|
|
|
|
2021-03-06 01:39:28 +03:00
|
|
|
function logOnCI(...args) {
|
|
|
|
if (!process.env.CI)
|
|
|
|
return;
|
|
|
|
console.log(...args);
|
|
|
|
}
|
|
|
|
|
2020-10-12 19:16:02 +03:00
|
|
|
const { it, expect } = fixtures.build();
|
2020-08-03 23:41:48 +03:00
|
|
|
|
2020-10-06 03:03:24 +03:00
|
|
|
it('should keep downloadsPath folder', async ({downloadsBrowser, testInfo, server}) => {
|
2020-08-03 23:41:48 +03: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 21:19:15 +03:00
|
|
|
await download.path().catch(e => void 0);
|
2020-08-03 23:41:48 +03:00
|
|
|
await page.close();
|
|
|
|
await downloadsBrowser.close();
|
2020-10-06 03:03:24 +03:00
|
|
|
expect(fs.existsSync(testInfo.outputPath(''))).toBeTruthy();
|
2020-08-03 23:41:48 +03:00
|
|
|
});
|
|
|
|
|
2020-08-28 14:20:29 +03:00
|
|
|
it('should delete downloads when context closes', async ({downloadsBrowser, server}) => {
|
2020-08-03 23:41:48 +03: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 21:43:40 +03:00
|
|
|
|
2020-10-06 03:03:24 +03:00
|
|
|
it('should report downloads in downloadsPath folder', async ({downloadsBrowser, testInfo, server}) => {
|
2020-08-03 23:41:48 +03: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-10-06 03:03:24 +03:00
|
|
|
expect(path.startsWith(testInfo.outputPath(''))).toBeTruthy();
|
2020-08-03 23:41:48 +03:00
|
|
|
await page.close();
|
|
|
|
});
|
|
|
|
|
2020-10-06 03:03:24 +03:00
|
|
|
it('should accept downloads in persistent context', async ({persistentDownloadsContext, testInfo, server}) => {
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.1');
|
2020-08-03 23:41:48 +03:00
|
|
|
const page = persistentDownloadsContext.pages()[0];
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.2');
|
2020-08-03 23:41:48 +03:00
|
|
|
const [ download ] = await Promise.all([
|
2021-03-02 04:52:45 +03:00
|
|
|
page.waitForEvent('download').then(d => {
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.3');
|
2021-03-02 04:52:45 +03:00
|
|
|
return d;
|
|
|
|
}),
|
|
|
|
page.click('a').then(d => {
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.4');
|
2021-03-02 04:52:45 +03:00
|
|
|
}),
|
2020-08-03 23:41:48 +03:00
|
|
|
]);
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.5');
|
2020-08-03 23:41:48 +03:00
|
|
|
expect(download.url()).toBe(`${server.PREFIX}/download`);
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.6');
|
2020-08-03 23:41:48 +03:00
|
|
|
expect(download.suggestedFilename()).toBe(`file.txt`);
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.7');
|
2020-08-03 23:41:48 +03:00
|
|
|
const path = await download.path();
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.8');
|
2020-10-06 03:03:24 +03:00
|
|
|
expect(path.startsWith(testInfo.outputPath(''))).toBeTruthy();
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 1.9');
|
2020-08-03 23:41:48 +03:00
|
|
|
});
|
|
|
|
|
2020-10-05 04:18:05 +03:00
|
|
|
it('should delete downloads when persistent context closes', async ({persistentDownloadsContext}) => {
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.1');
|
2020-08-03 23:41:48 +03:00
|
|
|
const page = persistentDownloadsContext.pages()[0];
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.2');
|
2020-08-03 23:41:48 +03:00
|
|
|
const [ download ] = await Promise.all([
|
2021-03-02 04:52:45 +03:00
|
|
|
page.waitForEvent('download').then(d => {
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.3');
|
2021-03-02 04:52:45 +03:00
|
|
|
return d;
|
|
|
|
}),
|
|
|
|
page.click('a').then(() => {
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.4');
|
2021-03-02 04:52:45 +03:00
|
|
|
}),
|
2020-08-03 23:41:48 +03:00
|
|
|
]);
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.5');
|
2020-08-03 23:41:48 +03:00
|
|
|
const path = await download.path();
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.6');
|
2020-08-03 23:41:48 +03:00
|
|
|
expect(fs.existsSync(path)).toBeTruthy();
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.7');
|
2020-10-05 04:18:05 +03:00
|
|
|
await persistentDownloadsContext.close();
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.8');
|
2020-10-05 04:18:05 +03:00
|
|
|
expect(fs.existsSync(path)).toBeFalsy();
|
2021-03-06 01:39:28 +03:00
|
|
|
logOnCI('----- 2.9');
|
2020-08-03 23:41:48 +03:00
|
|
|
});
|