feat: support timeout for page.pdf() call (#7508)

This commit is contained in:
Li Wenyan 2021-09-15 00:11:14 +08:00 committed by GitHub
parent a0b1f6b401
commit f90af6639d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 3 deletions

View File

@ -1712,7 +1712,6 @@ await page.evaluate(() => matchMedia('print').matches);
> **NOTE** This does not affect WebSockets and WebRTC PeerConnections (see https://crbug.com/563644). To set the page offline, you can use [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled).
```js
const puppeteer = require('puppeteer');
const slow3G = puppeteer.networkConditions['Slow 3G'];
@ -2086,6 +2085,7 @@ Page is guaranteed to have a main frame which persists during navigations.
- `left` <[string]|[number]> Left margin, accepts values labeled with units.
- `preferCSSPageSize` <[boolean]> Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size.
- `omitBackground` <[boolean]> Hides default white background and allows capturing screenshots with transparency. Defaults to `false`.
- `timeout` <[number]> Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout.
- returns: <[Promise]<[Buffer]>> Promise which resolves with PDF buffer.
> **NOTE** Generating a pdf is currently only supported in Chrome headless.
@ -2328,7 +2328,7 @@ await page.setGeolocation({ latitude: 59.95, longitude: 30.31667 });
#### page.setOfflineMode(enabled)
- `enabled` <[boolean]> When `true`, enables offline mode for the page.
- `enabled` <[boolean]> When `true`, enables offline mode for the page.
- returns: <[Promise]>
> **NOTE** while this method sets the network connection to offline, it does not change the parameters used in [page.emulateNetworkConditions(networkConditions)](#pageemulatenetworkconditionsnetworkconditions).

View File

@ -156,6 +156,11 @@ export interface PDFOptions {
* @defaultValue false
*/
omitBackground?: boolean;
/**
* Timeout in milliseconds
* @defaultValue 30000
*/
timeout?: number;
}
/**

View File

@ -2748,6 +2748,7 @@ export class Page extends EventEmitter {
preferCSSPageSize = false,
margin = {},
omitBackground = false,
timeout = 30000,
} = options;
let paperWidth = 8.5;
@ -2772,7 +2773,7 @@ export class Page extends EventEmitter {
await this._setTransparentBackgroundColor();
}
const result = await this._client.send('Page.printToPDF', {
const printCommandPromise = this._client.send('Page.printToPDF', {
transferMode: 'ReturnAsStream',
landscape,
displayHeaderFooter,
@ -2790,6 +2791,12 @@ export class Page extends EventEmitter {
preferCSSPageSize,
});
const result = await helper.waitWithTimeout(
printCommandPromise,
'Page.printToPDF',
timeout
);
if (omitBackground) {
await this._resetDefaultBackgroundColor();
}

11
test/assets/pdf.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PDF</title>
</head>
<body>
<div>PDF Content</div>
</body>
</html>

View File

@ -1639,6 +1639,17 @@ describe('Page', function () {
}
expect(size).toBeGreaterThan(0);
});
it('should respect timeout', async () => {
const { isHeadless, page, server, puppeteer } = getTestState();
if (!isHeadless) return;
await page.goto(server.PREFIX + '/pdf.html');
let error = null;
await page.pdf({ timeout: 1 }).catch((_error) => (error = _error));
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
});
});
describe('Page.title', function () {