Skip to content

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

feat: add defaultTimeout browser context configuration option (#6944)

Amit Abershitz617dfdef9e35
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index 3b88a716d1..c6bb0055ad 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -469,6 +469,11 @@ contexts override the proxy, global proxy will be never used and can be any stri
`launch({ proxy: { server: 'http://per-context' } })`.
:::
+## context-option-defaulttimeout
+- `defaultTimeout` <[float]>
+
+Set the default browser context timeout for the new context. Equivalent to calling [`method: BrowserContext.setDefaultTimeout`].
+
## select-options-values
* langs: java, js, csharp
- `values` <[null]|[string]|[ElementHandle]|[Array]<[string]>|[Object]|[Array]<[ElementHandle]>|[Array]<[Object]>>
@@ -594,6 +599,7 @@ using the [`method: AndroidDevice.setDefaultTimeout`] method.
- %%-context-option-recordvideo-%%
- %%-context-option-recordvideo-dir-%%
- %%-context-option-recordvideo-size-%%
+- %%-context-option-defaulttimeout-%%
## browser-option-args
- `args` <[Array]<[string]>>
diff --git a/src/protocol/channels.ts b/src/protocol/channels.ts
index ac662bf859..158be0edbb 100644
--- a/src/protocol/channels.ts
+++ b/src/protocol/channels.ts
@@ -339,6 +339,7 @@ export type BrowserTypeLaunchPersistentContextParams = {
omitContent?: boolean,
path: string,
},
+ defaultTimeout?: number,
userDataDir: string,
slowMo?: number,
};
@@ -409,6 +410,7 @@ export type BrowserTypeLaunchPersistentContextOptions = {
omitContent?: boolean,
path: string,
},
+ defaultTimeout?: number,
slowMo?: number,
};
export type BrowserTypeLaunchPersistentContextResult = {
@@ -499,6 +501,7 @@ export type BrowserNewContextParams = {
omitContent?: boolean,
path: string,
},
+ defaultTimeout?: number,
proxy?: {
server: string,
bypass?: string,
@@ -556,6 +559,7 @@ export type BrowserNewContextOptions = {
omitContent?: boolean,
path: string,
},
+ defaultTimeout?: number,
proxy?: {
server: string,
bypass?: string,
diff --git a/src/protocol/protocol.yml b/src/protocol/protocol.yml
index 8b372e33bf..1149bd2d25 100644
--- a/src/protocol/protocol.yml
+++ b/src/protocol/protocol.yml
@@ -322,6 +322,7 @@ ContextOptions:
properties:
omitContent: boolean?
path: string
+ defaultTimeout: number?
Playwright:
diff --git a/src/protocol/validator.ts b/src/protocol/validator.ts
index 1ae820a8b1..cdede28504 100644
--- a/src/protocol/validator.ts
+++ b/src/protocol/validator.ts
@@ -248,6 +248,7 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
omitContent: tOptional(tBoolean),
path: tString,
})),
+ defaultTimeout: tOptional(tNumber),
userDataDir: tString,
slowMo: tOptional(tNumber),
});
@@ -307,6 +308,7 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
omitContent: tOptional(tBoolean),
path: tString,
})),
+ defaultTimeout: tOptional(tNumber),
proxy: tOptional(tObject({
server: tString,
bypass: tOptional(tString),
diff --git a/src/server/browserContext.ts b/src/server/browserContext.ts
index 6e08002b5a..07fa7038fb 100644
--- a/src/server/browserContext.ts
+++ b/src/server/browserContext.ts
@@ -76,6 +76,9 @@ export abstract class BrowserContext extends SdkObject {
if (this._options.recordHar)
this._harTracer = new HarTracer(this, this._options.recordHar);
this.tracing = new Tracing(this);
+
+ if (typeof this._options.defaultTimeout === 'number' && !debugMode())
+ this._timeoutSettings.setDefaultTimeout(this._options.defaultTimeout);
}
_setSelectors(selectors: Selectors) {
diff --git a/src/server/types.ts b/src/server/types.ts
index 24e04d2e7e..109f99afcc 100644
--- a/src/server/types.ts
+++ b/src/server/types.ts
@@ -252,6 +252,7 @@ export type BrowserContextOptions = {
},
proxy?: ProxySettings,
_debugName?: string,
+ defaultTimeout?: number,
};
export type EnvArray = { name: string, value: string }[];
diff --git a/tests/browsercontext-basic.spec.ts b/tests/browsercontext-basic.spec.ts
index baf963c975..f9c996f4ef 100644
--- a/tests/browsercontext-basic.spec.ts
+++ b/tests/browsercontext-basic.spec.ts
@@ -218,6 +218,16 @@ it('should be able to navigate after disabling javascript', async ({browser, ser
await context.close();
});
+it('should respect default timeout when configured', async ({browser, playwright}) => {
+ const context = await browser.newContext({ defaultTimeout: 5 });
+ const page = await context.newPage();
+ let error = null;
+ await page.waitForFunction('false').catch(e => error = e);
+ expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
+ expect(error.message).toContain('page.waitForFunction: Timeout 5ms exceeded');
+ await context.close();
+});
+
it('should work with offline option', async ({browser, server}) => {
const context = await browser.newContext({offline: true});
const page = await context.newPage();
diff --git a/types/types.d.ts b/types/types.d.ts
index b74ecccc11..4e3c38e573 100644
--- a/types/types.d.ts
+++ b/types/types.d.ts
@@ -6985,6 +6985,12 @@ export interface BrowserType<Unused = {}> {
*/
colorScheme?: "light"|"dark"|"no-preference";
+ /**
+ * Set the default browser context timeout for the new context. Equivalent to calling
+ * [browserContext.setDefaultTimeout(timeout)](https://playwright.dev/docs/api/class-browsercontext#browsercontextsetdefaulttimeouttimeout).
+ */
+ defaultTimeout?: number;
+
/**
* Specify device scale factor (can be thought of as dpr). Defaults to `1`.
*/
@@ -8096,6 +8102,12 @@ export interface AndroidDevice {
*/
command?: string;
+ /**
+ * Set the default browser context timeout for the new context. Equivalent to calling
+ * [browserContext.setDefaultTimeout(timeout)](https://playwright.dev/docs/api/class-browsercontext#browsercontextsetdefaulttimeouttimeout).
+ */
+ defaultTimeout?: number;
+
/**
* Specify device scale factor (can be thought of as dpr). Defaults to `1`.
*/
@@ -8850,6 +8862,12 @@ export interface Browser extends EventEmitter {
*/
colorScheme?: "light"|"dark"|"no-preference";
+ /**
+ * Set the default browser context timeout for the new context. Equivalent to calling
+ * [browserContext.setDefaultTimeout(timeout)](https://playwright.dev/docs/api/class-browsercontext#browsercontextsetdefaulttimeouttimeout).
+ */
+ defaultTimeout?: number;
+
/**
* Specify device scale factor (can be thought of as dpr). Defaults to `1`.
*/
@@ -10919,6 +10937,12 @@ export interface BrowserContextOptions {
*/
colorScheme?: "light"|"dark"|"no-preference";
+ /**
+ * Set the default browser context timeout for the new context. Equivalent to calling
+ * [browserContext.setDefaultTimeout(timeout)](https://playwright.dev/docs/api/class-browsercontext#browsercontextsetdefaulttimeouttimeout).
+ */
+ defaultTimeout?: number;
+
/**
* Specify device scale factor (can be thought of as dpr). Defaults to `1`.
*/