fix(connectOverCDP): make sure downloads work in default context (#14864)
This commit is contained in:
parent
4f1ab2fa62
commit
e640f553b5
|
|
@ -45,6 +45,7 @@ import http from 'http';
|
||||||
import https from 'https';
|
import https from 'https';
|
||||||
import { registry } from '../registry';
|
import { registry } from '../registry';
|
||||||
import { ManualPromise } from '../../utils/manualPromise';
|
import { ManualPromise } from '../../utils/manualPromise';
|
||||||
|
import { validateBrowserContextOptions } from '../browserContext';
|
||||||
|
|
||||||
const ARTIFACTS_FOLDER = path.join(os.tmpdir(), 'playwright-artifacts-');
|
const ARTIFACTS_FOLDER = path.join(os.tmpdir(), 'playwright-artifacts-');
|
||||||
|
|
||||||
|
|
@ -93,12 +94,13 @@ export class Chromium extends BrowserType {
|
||||||
await cleanedUp;
|
await cleanedUp;
|
||||||
};
|
};
|
||||||
const browserProcess: BrowserProcess = { close: doClose, kill: doClose };
|
const browserProcess: BrowserProcess = { close: doClose, kill: doClose };
|
||||||
|
const persistent: types.BrowserContextOptions = { noDefaultViewport: true };
|
||||||
const browserOptions: BrowserOptions = {
|
const browserOptions: BrowserOptions = {
|
||||||
...this._playwrightOptions,
|
...this._playwrightOptions,
|
||||||
slowMo: options.slowMo,
|
slowMo: options.slowMo,
|
||||||
name: 'chromium',
|
name: 'chromium',
|
||||||
isChromium: true,
|
isChromium: true,
|
||||||
persistent: { noDefaultViewport: true },
|
persistent,
|
||||||
browserProcess,
|
browserProcess,
|
||||||
protocolLogger: helper.debugProtocolLogger(),
|
protocolLogger: helper.debugProtocolLogger(),
|
||||||
browserLogsCollector: new RecentLogsCollector(),
|
browserLogsCollector: new RecentLogsCollector(),
|
||||||
|
|
@ -112,6 +114,7 @@ export class Chromium extends BrowserType {
|
||||||
// does not work at all with proxies on Windows.
|
// does not work at all with proxies on Windows.
|
||||||
proxy: { server: 'per-context' },
|
proxy: { server: 'per-context' },
|
||||||
};
|
};
|
||||||
|
validateBrowserContextOptions(persistent, browserOptions);
|
||||||
progress.throwIfAborted();
|
progress.throwIfAborted();
|
||||||
const browser = await CRBrowser.connect(chromeTransport, browserOptions);
|
const browser = await CRBrowser.connect(chromeTransport, browserOptions);
|
||||||
browser.on(Browser.Events.Disconnected, doCleanup);
|
browser.on(Browser.Events.Disconnected, doCleanup);
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,44 @@ playwrightTest('should cleanup artifacts dir after connectOverCDP disconnects du
|
||||||
expect(exists2).toBe(false);
|
expect(exists2).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
playwrightTest('should connectOverCDP and manage downloads in default context', async ({ browserType, toImpl, mode, server }, testInfo) => {
|
||||||
|
playwrightTest.skip(mode !== 'default');
|
||||||
|
|
||||||
|
server.setRoute('/downloadWithFilename', (req, res) => {
|
||||||
|
res.setHeader('Content-Type', 'application/octet-stream');
|
||||||
|
res.setHeader('Content-Disposition', 'attachment; filename=file.txt');
|
||||||
|
res.end(`Hello world`);
|
||||||
|
});
|
||||||
|
|
||||||
|
const port = 9339 + testInfo.workerIndex;
|
||||||
|
const browserServer = await browserType.launch({
|
||||||
|
args: ['--remote-debugging-port=' + port]
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const browser = await browserType.connectOverCDP({
|
||||||
|
endpointURL: `http://127.0.0.1:${port}/`,
|
||||||
|
});
|
||||||
|
const page = await browser.contexts()[0].newPage();
|
||||||
|
await page.setContent(`<a href="${server.PREFIX}/downloadWithFilename">download</a>`);
|
||||||
|
|
||||||
|
const [ download ] = await Promise.all([
|
||||||
|
page.waitForEvent('download'),
|
||||||
|
page.click('a')
|
||||||
|
]);
|
||||||
|
expect(download.page()).toBe(page);
|
||||||
|
expect(download.url()).toBe(`${server.PREFIX}/downloadWithFilename`);
|
||||||
|
expect(download.suggestedFilename()).toBe(`file.txt`);
|
||||||
|
|
||||||
|
const userPath = testInfo.outputPath('download.txt');
|
||||||
|
await download.saveAs(userPath);
|
||||||
|
expect(fs.existsSync(userPath)).toBeTruthy();
|
||||||
|
expect(fs.readFileSync(userPath).toString()).toBe('Hello world');
|
||||||
|
} finally {
|
||||||
|
await browserServer.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
playwrightTest('should connect to an existing cdp session twice', async ({ browserType, server }, testInfo) => {
|
playwrightTest('should connect to an existing cdp session twice', async ({ browserType, server }, testInfo) => {
|
||||||
const port = 9339 + testInfo.workerIndex;
|
const port = 9339 + testInfo.workerIndex;
|
||||||
const browserServer = await browserType.launch({
|
const browserServer = await browserType.launch({
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue