Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(mcp): do not disable extensions in persistent mode (#37325)

Pavel Feldman4d9e815ebb3c
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index 8cb60f038f..993b8bc296 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -235,14 +235,18 @@ Dangerous option; use with care. Defaults to `false`.
Network proxy settings.
+## js-browser-option-env
+* langs: js
+- `env` <[Object]<[string], [string]|[undefined]>>
+
## csharp-java-browser-option-env
* langs: csharp, java
- `env` <[Object]<[string], [string]>>
Specify environment variables that will be visible to the browser. Defaults to `process.env`.
-## js-python-browser-option-env
-* langs: js, python
+## python-browser-option-env
+* langs: python
- `env` <[Object]<[string], [string]|[float]|[boolean]>>
Specify environment variables that will be visible to the browser. Defaults to `process.env`.
@@ -1122,7 +1126,8 @@ Slows down Playwright operations by the specified amount of milliseconds. Useful
- %%-browser-option-devtools-%%
- %%-browser-option-downloadspath-%%
- %%-csharp-java-browser-option-env-%%
-- %%-js-python-browser-option-env-%%
+- %%-js-browser-option-env-%%
+- %%-python-browser-option-env-%%
- %%-browser-option-executablepath-%%
- %%-browser-option-handlesigint-%%
- %%-browser-option-handlesigterm-%%
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index 9d759a2bc2..2f6edbe3b3 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -14918,10 +14918,7 @@ export interface BrowserType<Unused = {}> {
*/
downloadsPath?: string;
- /**
- * Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- */
- env?: { [key: string]: string|number|boolean; };
+ env?: { [key: string]: string|undefined; };
/**
* Path to a browser executable to run instead of the bundled one. If
@@ -15351,10 +15348,7 @@ export interface BrowserType<Unused = {}> {
*/
downloadsPath?: string;
- /**
- * Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- */
- env?: { [key: string]: string|number|boolean; };
+ env?: { [key: string]: string|undefined; };
/**
* Path to a browser executable to run instead of the bundled one. If
@@ -21766,10 +21760,7 @@ export interface LaunchOptions {
*/
downloadsPath?: string;
- /**
- * Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- */
- env?: { [key: string]: string|number|boolean; };
+ env?: { [key: string]: string|undefined; };
/**
* Path to a browser executable to run instead of the bundled one. If
diff --git a/packages/playwright-core/src/browserServerImpl.ts b/packages/playwright-core/src/browserServerImpl.ts
index c789991584..a9b1e97ae9 100644
--- a/packages/playwright-core/src/browserServerImpl.ts
+++ b/packages/playwright-core/src/browserServerImpl.ts
@@ -26,7 +26,7 @@ import * as validatorPrimitives from './protocol/validatorPrimitives';
import { ProgressController } from './server/progress';
import type { BrowserServer, BrowserServerLauncher } from './client/browserType';
-import type { LaunchOptions, LaunchServerOptions, Logger, Env } from './client/types';
+import type { LaunchOptions, LaunchServerOptions, Logger } from './client/types';
import type { ProtocolLogger } from './server/types';
import type { WebSocketEventEmitter } from './utilsBundle';
import type { Browser } from './server/browser';
@@ -112,7 +112,7 @@ function toProtocolLogger(logger: Logger | undefined): ProtocolLogger | undefine
} : undefined;
}
-function envObjectToArray(env: Env): { name: string, value: string }[] {
+function envObjectToArray(env: NodeJS.ProcessEnv): { name: string, value: string }[] {
const result: { name: string, value: string }[] = [];
for (const name in env) {
if (!Object.is(env[name], undefined))
diff --git a/packages/playwright-core/src/client/clientHelper.ts b/packages/playwright-core/src/client/clientHelper.ts
index aecc92a156..6c9b8aad1b 100644
--- a/packages/playwright-core/src/client/clientHelper.ts
+++ b/packages/playwright-core/src/client/clientHelper.ts
@@ -17,10 +17,9 @@
import { isString } from '../utils/isomorphic/rtti';
-import type * as types from './types';
import type { Platform } from './platform';
-export function envObjectToArray(env: types.Env): { name: string, value: string }[] {
+export function envObjectToArray(env: NodeJS.ProcessEnv): { name: string, value: string }[] {
const result: { name: string, value: string }[] = [];
for (const name in env) {
if (!Object.is(env[name], undefined))
diff --git a/packages/playwright-core/src/client/electron.ts b/packages/playwright-core/src/client/electron.ts
index fc33350d07..083e7b6f9f 100644
--- a/packages/playwright-core/src/client/electron.ts
+++ b/packages/playwright-core/src/client/electron.ts
@@ -25,7 +25,7 @@ import { Waiter } from './waiter';
import { TimeoutSettings } from './timeoutSettings';
import type { Page } from './page';
-import type { BrowserContextOptions, Env, Headers, WaitForEventOptions } from './types';
+import type { BrowserContextOptions, Headers, WaitForEventOptions } from './types';
import type * as structs from '../../types/structs';
import type * as api from '../../types/types';
import type * as channels from '@protocol/channels';
@@ -34,7 +34,7 @@ import type { BrowserWindow } from 'electron';
import type { Playwright } from './playwright';
type ElectronOptions = Omit<channels.ElectronLaunchOptions, 'env'|'extraHTTPHeaders'|'recordHar'|'colorScheme'|'acceptDownloads'> & {
- env?: Env,
+ env?: NodeJS.ProcessEnv,
extraHTTPHeaders?: Headers,
recordHar?: BrowserContextOptions['recordHar'],
colorScheme?: 'dark' | 'light' | 'no-preference' | null,
diff --git a/packages/playwright-core/src/client/types.ts b/packages/playwright-core/src/client/types.ts
index bcdf728423..5b0b10b594 100644
--- a/packages/playwright-core/src/client/types.ts
+++ b/packages/playwright-core/src/client/types.ts
@@ -28,7 +28,6 @@ export interface Logger {
export type TimeoutOptions = { timeout?: number };
export type StrictOptions = { strict?: boolean };
export type Headers = { [key: string]: string };
-export type Env = { [key: string]: string | number | boolean | undefined };
export type WaitForEventOptions = Function | TimeoutOptions & { predicate?: Function };
export type WaitForFunctionOptions = TimeoutOptions & { polling?: 'raf' | number };
@@ -88,7 +87,7 @@ export type BrowserContextOptions = Omit<channels.BrowserNewContextOptions, 'vie
type LaunchOverrides = {
ignoreDefaultArgs?: boolean | string[];
- env?: Env;
+ env?: NodeJS.ProcessEnv;
logger?: Logger;
firefoxUserPrefs?: { [key: string]: string | number | boolean };
} & TimeoutOptions;
diff --git a/packages/playwright-core/src/server/bidi/bidiChromium.ts b/packages/playwright-core/src/server/bidi/bidiChromium.ts
index ac8639a1fc..b9fc22f0b2 100644
--- a/packages/playwright-core/src/server/bidi/bidiChromium.ts
+++ b/packages/playwright-core/src/server/bidi/bidiChromium.ts
@@ -26,7 +26,6 @@ import { waitForReadyState } from '../chromium/chromium';
import type { BrowserOptions } from '../browser';
import type { SdkObject } from '../instrumentation';
-import type { Env } from '../utils/processLauncher';
import type { ProtocolError } from '../protocolError';
import type { ConnectionTransport } from '../transport';
import type * as types from '../types';
@@ -77,7 +76,7 @@ export class BidiChromium extends BrowserType {
return error;
}
- override amendEnvironment(env: Env): Env {
+ override amendEnvironment(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
return env;
}
diff --git a/packages/playwright-core/src/server/bidi/bidiFirefox.ts b/packages/playwright-core/src/server/bidi/bidiFirefox.ts
index 7c96383fe1..08f99ce7d3 100644
--- a/packages/playwright-core/src/server/bidi/bidiFirefox.ts
+++ b/packages/playwright-core/src/server/bidi/bidiFirefox.ts
@@ -26,7 +26,6 @@ import { ManualPromise } from '../../utils/isomorphic/manualPromise';
import type { BrowserOptions } from '../browser';
import type { SdkObject } from '../instrumentation';
-import type { Env } from '../utils/processLauncher';
import type { ProtocolError } from '../protocolError';
import type { ConnectionTransport } from '../transport';
import type * as types from '../types';
@@ -57,7 +56,7 @@ export class BidiFirefox extends BrowserType {
return error;
}
- override amendEnvironment(env: Env): Env {
+ override amendEnvironment(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
if (!path.isAbsolute(os.homedir()))
throw new Error(`Cannot launch Firefox with relative home directory. Did you set ${os.platform() === 'win32' ? 'USERPROFILE' : 'HOME'} to a relative path?`);
diff --git a/packages/playwright-core/src/server/browserType.ts b/packages/playwright-core/src/server/browserType.ts
index 4f6491e11c..693d939a89 100644
--- a/packages/playwright-core/src/server/browserType.ts
+++ b/packages/playwright-core/src/server/browserType.ts
@@ -36,7 +36,6 @@ import { RecentLogsCollector } from './utils/debugLogger';
import type { Browser, BrowserOptions, BrowserProcess } from './browser';
import type { BrowserContext } from './browserContext';
-import type { Env } from './utils/processLauncher';
import type { Progress } from './progress';
import type { ProtocolError } from './protocolError';
import type { BrowserName } from './registry';
@@ -341,7 +340,7 @@ export abstract class BrowserType extends SdkObject {
abstract defaultArgs(options: types.LaunchOptions, isPersistent: boolean, userDataDir: string): string[];
abstract connectToTransport(transport: ConnectionTransport, options: BrowserOptions, browserLogsCollector: RecentLogsCollector): Promise<Browser>;
- abstract amendEnvironment(env: Env, userDataDir: string, isPersistent: boolean): Env;
+ abstract amendEnvironment(env: NodeJS.ProcessEnv, userDataDir: string, isPersistent: boolean): NodeJS.ProcessEnv;
abstract doRewriteStartupLog(error: ProtocolError): ProtocolError;
abstract attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void;
}
diff --git a/packages/playwright-core/src/server/chromium/chromium.ts b/packages/playwright-core/src/server/chromium/chromium.ts
index 60d9ccdbe3..c406cc7e87 100644
--- a/packages/playwright-core/src/server/chromium/chromium.ts
+++ b/packages/playwright-core/src/server/chromium/chromium.ts
@@ -41,7 +41,6 @@ import { gracefullyCloseSet } from '../utils/processLauncher';
import type { HTTPRequestParams } from '../utils/network';
import type { BrowserOptions, BrowserProcess } from '../browser';
import type { SdkObject } from '../instrumentation';
-import type { Env } from '../utils/processLauncher';
import type { Progress } from '../progress';
import type { ProtocolError } from '../protocolError';
import type { ConnectionTransport, ProtocolRequest } from '../transport';
@@ -165,7 +164,7 @@ export class Chromium extends BrowserType {
return error;
}
- override amendEnvironment(env: Env): Env {
+ override amendEnvironment(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
return env;
}
diff --git a/packages/playwright-core/src/server/firefox/firefox.ts b/packages/playwright-core/src/server/firefox/firefox.ts
index 79dca47465..c9ffbecc74 100644
--- a/packages/playwright-core/src/server/firefox/firefox.ts
+++ b/packages/playwright-core/src/server/firefox/firefox.ts
@@ -26,7 +26,6 @@ import { ManualPromise } from '../../utils/isomorphic/manualPromise';
import type { BrowserOptions } from '../browser';
import type { SdkObject } from '../instrumentation';
-import type { Env } from '../utils/processLauncher';
import type { ProtocolError } from '../protocolError';
import type { ConnectionTransport } from '../transport';
import type * as types from '../types';
@@ -52,7 +51,7 @@ export class Firefox extends BrowserType {
return error;
}
- override amendEnvironment(env: Env): Env {
+ override amendEnvironment(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
if (!path.isAbsolute(os.homedir()))
throw new Error(`Cannot launch Firefox with relative home directory. Did you set ${os.platform() === 'win32' ? 'USERPROFILE' : 'HOME'} to a relative path?`);
if (os.platform() === 'linux') {
diff --git a/packages/playwright-core/src/server/utils/processLauncher.ts b/packages/playwright-core/src/server/utils/processLauncher.ts
index e027708c1b..148b340ec7 100644
--- a/packages/playwright-core/src/server/utils/processLauncher.ts
+++ b/packages/playwright-core/src/server/utils/processLauncher.ts
@@ -22,12 +22,10 @@ import * as readline from 'readline';
import { removeFolders } from './fileUtils';
import { isUnderTest } from '../../utils';
-export type Env = {[key: string]: string | number | boolean | undefined};
-
export type LaunchProcessOptions = {
command: string,
args?: string[],
- env?: Env,
+ env?: NodeJS.ProcessEnv,
shell?: boolean,
handleSIGINT?: boolean,
@@ -138,7 +136,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
detached: process.platform !== 'win32',
- env: (options.env as {[key: string]: string}),
+ env: options.env,
cwd: options.cwd,
shell: options.shell,
stdio,
@@ -274,8 +272,8 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
return { launchedProcess: spawnedProcess, gracefullyClose, kill: killAndWait };
}
-export function envArrayToObject(env: { name: string, value: string }[]): Env {
- const result: Env = {};
+export function envArrayToObject(env: { name: string, value: string }[]): NodeJS.ProcessEnv {
+ const result: NodeJS.ProcessEnv = {};
for (const { name, value } of env)
result[name] = value;
return result;
diff --git a/packages/playwright-core/src/server/webkit/webkit.ts b/packages/playwright-core/src/server/webkit/webkit.ts
index 27ae2a7a05..db31187b78 100644
--- a/packages/playwright-core/src/server/webkit/webkit.ts
+++ b/packages/playwright-core/src/server/webkit/webkit.ts
@@ -24,7 +24,6 @@ import { WKBrowser } from '../webkit/wkBrowser';
import type { BrowserOptions } from '../browser';
import type { SdkObject } from '../instrumentation';
-import type { Env } from '../utils/processLauncher';
import type { ProtocolError } from '../protocolError';
import type { ConnectionTransport } from '../transport';
import type * as types from '../types';
@@ -38,7 +37,7 @@ export class WebKit extends BrowserType {
return WKBrowser.connect(this.attribution.playwright, transport, options);
}
- override amendEnvironment(env: Env, userDataDir: string, isPersistent: boolean): Env {
+ override amendEnvironment(env: NodeJS.ProcessEnv, userDataDir: string, isPersistent: boolean): NodeJS.ProcessEnv {
return {
...env,
CURL_COOKIE_JAR_PATH: process.platform === 'win32' && isPersistent ? path.join(userDataDir, 'cookiejar.db') : undefined,
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 9d759a2bc2..2f6edbe3b3 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -14918,10 +14918,7 @@ export interface BrowserType<Unused = {}> {
*/
downloadsPath?: string;
- /**
- * Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- */
- env?: { [key: string]: string|number|boolean; };
+ env?: { [key: string]: string|undefined; };
/**
* Path to a browser executable to run instead of the bundled one. If
@@ -15351,10 +15348,7 @@ export interface BrowserType<Unused = {}> {
*/
downloadsPath?: string;
- /**
- * Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- */
- env?: { [key: string]: string|number|boolean; };
+ env?: { [key: string]: string|undefined; };
/**
* Path to a browser executable to run instead of the bundled one. If
@@ -21766,10 +21760,7 @@ export interface LaunchOptions {
*/
downloadsPath?: string;
- /**
- * Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- */
- env?: { [key: string]: string|number|boolean; };
+ env?: { [key: string]: string|undefined; };
/**
* Path to a browser executable to run instead of the bundled one. If
diff --git a/packages/playwright/src/mcp/browser/browserContextFactory.ts b/packages/playwright/src/mcp/browser/browserContextFactory.ts
index 813e56e3ad..2c39ba47f5 100644
--- a/packages/playwright/src/mcp/browser/browserContextFactory.ts
+++ b/packages/playwright/src/mcp/browser/browserContextFactory.ts
@@ -26,6 +26,7 @@ import { logUnhandledError, testDebug } from '../log';
import { outputFile } from './config';
import type { FullConfig } from './config';
+import type { LaunchOptions } from '../../../../playwright-core/src/client/types';
export function contextFactory(config: FullConfig): BrowserContextFactory {
if (config.browser.remoteEndpoint)
@@ -179,14 +180,19 @@ class PersistentContextFactory implements BrowserContextFactory {
const browserType = playwright[this.config.browser.browserName];
for (let i = 0; i < 5; i++) {
+ const launchOptions: LaunchOptions = {
+ tracesDir,
+ ...this.config.browser.launchOptions,
+ ...this.config.browser.contextOptions,
+ handleSIGINT: false,
+ handleSIGTERM: false,
+ ignoreDefaultArgs: [
+ '--disable-extensions',
+ ],
+ assistantMode: true,
+ };
try {
- const browserContext = await browserType.launchPersistentContext(userDataDir, {
- tracesDir,
- ...this.config.browser.launchOptions,
- ...this.config.browser.contextOptions,
- handleSIGINT: false,
- handleSIGTERM: false,
- });
+ const browserContext = await browserType.launchPersistentContext(userDataDir, launchOptions);
const close = () => this._closeBrowserContext(browserContext, userDataDir);
return { browserContext, close };
} catch (error: any) {
diff --git a/utils/generate_types/test/test.ts b/utils/generate_types/test/test.ts
index 7aec26a418..a71dab64b0 100644
--- a/utils/generate_types/test/test.ts
+++ b/utils/generate_types/test/test.ts
@@ -228,9 +228,9 @@ playwright.chromium.launch().then(async browser => {
const launchOptions: playwright.LaunchOptions = {
devtools: true,
env: {
- TIMEOUT: 52,
+ TIMEOUT: '52',
SOMETHING: '/some/path',
- JEST_TEST: true
+ JEST_TEST: 'true'
}
};
const browser = await playwright.chromium.launch(launchOptions);