feat(connect): add connectOptions.timeout (#13163)

This commit is contained in:
Dmitry Gozman 2022-03-29 15:03:43 -07:00 committed by GitHub
parent aa1daeba85
commit eb09306db2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 5 deletions

View file

@ -115,7 +115,7 @@ Logger sink for Playwright logging. Optional.
- `timeout` <[float]>
Maximum time in milliseconds to wait for the connection to be established. Defaults to
`30000` (30 seconds). Pass `0` to disable timeout.
`0` (no timeout).
## async method: BrowserType.connectOverCDP
- returns: <[Browser]>

View file

@ -114,6 +114,7 @@ Learn more about [various timeouts](../test-timeouts.md).
- type: <[void]|[Object]>
- `wsEndpoint` <[string]> A browser websocket endpoint to connect to.
- `headers` <[void]|[Object]<[string], [string]>> Additional HTTP headers to be sent with web socket connect request. Optional.
- `timeout` <[int]> Timeout in milliseconds for the connection to be established. Optional, defaults to no timeout.
When connect options are specified, default [`property: Fixtures.browser`], [`property: Fixtures.context`] and [`property: Fixtures.page`] use the remote browser instead of launching a browser locally, and any launch options like [`property: TestOptions.headless`] or [`property: TestOptions.channel`] are ignored.

View file

@ -80,7 +80,8 @@ export class WebSocketTransport implements ConnectionTransport {
this._ws = new WebSocket(url, [], {
perMessageDeflate: false,
maxPayload: 256 * 1024 * 1024, // 256Mb,
handshakeTimeout: progress.timeUntilDeadline(),
// Prevent internal http client error when passing negative timeout.
handshakeTimeout: Math.max(progress.timeUntilDeadline(), 1),
headers,
followRedirects,
});

View file

@ -15715,8 +15715,7 @@ export interface ConnectOptions {
slowMo?: number;
/**
* Maximum time in milliseconds to wait for the connection to be established. Defaults to `30000` (30 seconds). Pass `0` to
* disable timeout.
* Maximum time in milliseconds to wait for the connection to be established. Defaults to `0` (no timeout).
*/
timeout?: number;
}

View file

@ -115,7 +115,8 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({
'x-playwright-browser': channel || browserName,
'x-playwright-headless': headless ? '1' : '0',
...connectOptions.headers,
}
},
timeout: connectOptions.timeout,
});
await use(browser);
await browser.close();

View file

@ -2853,6 +2853,11 @@ type ConnectOptions = {
* Additional HTTP headers to be sent with web socket connect request.
*/
headers?: { [key: string]: string; };
/**
* Timeout in milliseconds for the connection to be established. Optional, defaults to no timeout.
*/
timeout?: number;
};
/**

View file

@ -620,3 +620,26 @@ test('should throw with bad connectOptions', async ({ runInlineTest }) => {
expect(result.passed).toBe(0);
expect(result.output).toContain('browserType.connect:');
});
test('should respect connectOptions.timeout', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.js': `
module.exports = {
use: {
connectOptions: {
wsEndpoint: 'wss://locahost:5678',
timeout: 1,
},
},
};
`,
'a.test.ts': `
const { test } = pwt;
test('pass', async ({ page }) => {
});
`,
});
expect(result.exitCode).toBe(1);
expect(result.passed).toBe(0);
expect(result.output).toContain('browserType.connect: Timeout 1ms exceeded.');
});

View file

@ -369,6 +369,11 @@ type ConnectOptions = {
* Additional HTTP headers to be sent with web socket connect request.
*/
headers?: { [key: string]: string; };
/**
* Timeout in milliseconds for the connection to be established. Optional, defaults to no timeout.
*/
timeout?: number;
};
export interface PlaywrightWorkerOptions {