mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: optionally connect instead of launching (#17174)
This commit is contained in:
parent
041dfd055a
commit
c91df61ca3
@ -49,6 +49,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
|
|||||||
// Instrumentation.
|
// Instrumentation.
|
||||||
_defaultContextOptions?: BrowserContextOptions;
|
_defaultContextOptions?: BrowserContextOptions;
|
||||||
_defaultLaunchOptions?: LaunchOptions;
|
_defaultLaunchOptions?: LaunchOptions;
|
||||||
|
_defaultConnectOptions?: ConnectOptions;
|
||||||
_onDidCreateContext?: (context: BrowserContext) => Promise<void>;
|
_onDidCreateContext?: (context: BrowserContext) => Promise<void>;
|
||||||
_onWillCloseContext?: (context: BrowserContext) => Promise<void>;
|
_onWillCloseContext?: (context: BrowserContext) => Promise<void>;
|
||||||
|
|
||||||
@ -67,6 +68,9 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
|
|||||||
}
|
}
|
||||||
|
|
||||||
async launch(options: LaunchOptions = {}): Promise<Browser> {
|
async launch(options: LaunchOptions = {}): Promise<Browser> {
|
||||||
|
if (this._defaultConnectOptions)
|
||||||
|
return await this._connectInsteadOfLaunching();
|
||||||
|
|
||||||
const logger = options.logger || this._defaultLaunchOptions?.logger;
|
const logger = options.logger || this._defaultLaunchOptions?.logger;
|
||||||
assert(!(options as any).userDataDir, 'userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
|
assert(!(options as any).userDataDir, 'userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
|
||||||
assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
|
assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
|
||||||
@ -83,6 +87,18 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
|
|||||||
return browser;
|
return browser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async _connectInsteadOfLaunching(): Promise<Browser> {
|
||||||
|
const connectOptions = this._defaultConnectOptions!;
|
||||||
|
return this._connect(connectOptions.wsEndpoint, {
|
||||||
|
headers: {
|
||||||
|
'x-playwright-browser': this.name(),
|
||||||
|
'x-playwright-launch-options': JSON.stringify(this._defaultLaunchOptions || {}),
|
||||||
|
...connectOptions.headers,
|
||||||
|
},
|
||||||
|
timeout: connectOptions.timeout ?? 3 * 60 * 1000, // 3 minutes
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async launchServer(options: LaunchServerOptions = {}): Promise<api.BrowserServer> {
|
async launchServer(options: LaunchServerOptions = {}): Promise<api.BrowserServer> {
|
||||||
if (!this._serverLauncher)
|
if (!this._serverLauncher)
|
||||||
throw new Error('Launching server is not supported');
|
throw new Error('Launching server is not supported');
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import type { LaunchOptions, BrowserContextOptions, Page, Browser, BrowserContext, Video, APIRequestContext, Tracing } from 'playwright-core';
|
import type { LaunchOptions, BrowserContextOptions, Page, BrowserContext, Video, APIRequestContext, Tracing } from 'playwright-core';
|
||||||
import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, TestInfo, VideoMode, TraceMode } from '../types/test';
|
import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, TestInfo, VideoMode, TraceMode } from '../types/test';
|
||||||
import { rootTestType } from './testType';
|
import { rootTestType } from './testType';
|
||||||
import { createGuid, debugMode } from 'playwright-core/lib/utils';
|
import { createGuid, debugMode } from 'playwright-core/lib/utils';
|
||||||
@ -48,7 +48,6 @@ type TestFixtures = PlaywrightTestArgs & PlaywrightTestOptions & {
|
|||||||
_contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
|
_contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
|
||||||
};
|
};
|
||||||
type WorkerFixtures = PlaywrightWorkerArgs & PlaywrightWorkerOptions & {
|
type WorkerFixtures = PlaywrightWorkerArgs & PlaywrightWorkerOptions & {
|
||||||
_connectedBrowser: Browser | undefined,
|
|
||||||
_browserOptions: LaunchOptions;
|
_browserOptions: LaunchOptions;
|
||||||
_artifactsDir: () => string;
|
_artifactsDir: () => string;
|
||||||
_snapshotSuffix: string;
|
_snapshotSuffix: string;
|
||||||
@ -91,7 +90,7 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({
|
|||||||
await removeFolders([dir]);
|
await removeFolders([dir]);
|
||||||
}, { scope: 'worker', _title: 'playwright configuration' } as any],
|
}, { scope: 'worker', _title: 'playwright configuration' } as any],
|
||||||
|
|
||||||
_browserOptions: [async ({ playwright, headless, channel, launchOptions }, use) => {
|
_browserOptions: [async ({ playwright, headless, channel, launchOptions, connectOptions }, use) => {
|
||||||
const options: LaunchOptions = {
|
const options: LaunchOptions = {
|
||||||
handleSIGINT: false,
|
handleSIGINT: false,
|
||||||
timeout: 0,
|
timeout: 0,
|
||||||
@ -102,38 +101,18 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({
|
|||||||
if (channel !== undefined)
|
if (channel !== undefined)
|
||||||
options.channel = channel;
|
options.channel = channel;
|
||||||
|
|
||||||
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit])
|
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit]) {
|
||||||
(browserType as any)._defaultLaunchOptions = options;
|
(browserType as any)._defaultLaunchOptions = options;
|
||||||
|
(browserType as any)._defaultConnectOptions = connectOptions;
|
||||||
|
}
|
||||||
await use(options);
|
await use(options);
|
||||||
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit])
|
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit]) {
|
||||||
(browserType as any)._defaultLaunchOptions = undefined;
|
(browserType as any)._defaultLaunchOptions = undefined;
|
||||||
|
(browserType as any)._defaultConnectOptions = undefined;
|
||||||
|
}
|
||||||
}, { scope: 'worker', auto: true }],
|
}, { scope: 'worker', auto: true }],
|
||||||
|
|
||||||
_connectedBrowser: [async ({ playwright, browserName, connectOptions, _browserOptions }, use) => {
|
browser: [async ({ playwright, browserName }, use) => {
|
||||||
if (!connectOptions) {
|
|
||||||
await use(undefined);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!['chromium', 'firefox', 'webkit'].includes(browserName))
|
|
||||||
throw new Error(`Unexpected browserName "${browserName}", must be one of "chromium", "firefox" or "webkit"`);
|
|
||||||
const browser = await playwright[browserName].connect(connectOptions.wsEndpoint, {
|
|
||||||
headers: {
|
|
||||||
'x-playwright-browser': browserName,
|
|
||||||
'x-playwright-launch-options': JSON.stringify(_browserOptions),
|
|
||||||
...connectOptions.headers,
|
|
||||||
},
|
|
||||||
timeout: connectOptions.timeout ?? 3 * 60 * 1000, // 3 minutes
|
|
||||||
});
|
|
||||||
await use(browser);
|
|
||||||
await browser.close();
|
|
||||||
}, { scope: 'worker', timeout: 0, _title: 'remote connection' } as any],
|
|
||||||
|
|
||||||
browser: [async ({ playwright, browserName, _connectedBrowser }, use) => {
|
|
||||||
if (_connectedBrowser) {
|
|
||||||
await use(_connectedBrowser);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!['chromium', 'firefox', 'webkit'].includes(browserName))
|
if (!['chromium', 'firefox', 'webkit'].includes(browserName))
|
||||||
throw new Error(`Unexpected browserName "${browserName}", must be one of "chromium", "firefox" or "webkit"`);
|
throw new Error(`Unexpected browserName "${browserName}", must be one of "chromium", "firefox" or "webkit"`);
|
||||||
const browser = await playwright[browserName].launch();
|
const browser = await playwright[browserName].launch();
|
||||||
|
@ -627,4 +627,16 @@ test('should upload large file', async ({ browserType, startRemoteServer, server
|
|||||||
await Promise.all([uploadFile, file1.filepath].map(fs.promises.unlink));
|
await Promise.all([uploadFile, file1.filepath].map(fs.promises.unlink));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should connect when launching', async ({ browserType, startRemoteServer, httpsServer, mode }) => {
|
||||||
|
const remoteServer = await startRemoteServer();
|
||||||
|
(browserType as any)._defaultConnectOptions = {
|
||||||
|
wsEndpoint: remoteServer.wsEndpoint()
|
||||||
|
};
|
||||||
|
|
||||||
|
const browser = await browserType.launch();
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
new Promise(f => browser.on('disconnected', f)),
|
||||||
|
remoteServer.close(),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
@ -70,7 +70,8 @@ test('should throw with bad connectOptions', async ({ runInlineTest }) => {
|
|||||||
});
|
});
|
||||||
expect(result.exitCode).toBe(1);
|
expect(result.exitCode).toBe(1);
|
||||||
expect(result.passed).toBe(0);
|
expect(result.passed).toBe(0);
|
||||||
expect(result.output).toContain('browserType.connect:');
|
expect(result.output).toContain('browserType.launch:');
|
||||||
|
expect(result.output).toContain('does-not-exist-bad-domain');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should respect connectOptions.timeout', async ({ runInlineTest }) => {
|
test('should respect connectOptions.timeout', async ({ runInlineTest }) => {
|
||||||
@ -93,5 +94,5 @@ test('should respect connectOptions.timeout', async ({ runInlineTest }) => {
|
|||||||
});
|
});
|
||||||
expect(result.exitCode).toBe(1);
|
expect(result.exitCode).toBe(1);
|
||||||
expect(result.passed).toBe(0);
|
expect(result.passed).toBe(0);
|
||||||
expect(result.output).toContain('browserType.connect: Timeout 1ms exceeded.');
|
expect(result.output).toContain('browserType.launch: Timeout 1ms exceeded.');
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user