fix: improve screenshot method types (#9529)

The only drawback of adding overrides is that our documentation
generator would generate a page for each overload which might be hard to
read/find on the documentation website.

Closes #9512 #9385
This commit is contained in:
Alex Rudenko 2023-01-18 15:06:20 +01:00 committed by GitHub
parent 153d65dd83
commit 6847f8835f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 7 deletions

View File

@ -69,7 +69,7 @@ The constructor for this class is marked as internal. Third-party code should no
| [hover(this)](./puppeteer.elementhandle.hover.md) | | This method scrolls element into view if needed, and then uses [Page.mouse](./puppeteer.page.mouse.md) to hover over the center of the element. If the element is detached from DOM, the method throws an error. |
| [isIntersectingViewport(this, options)](./puppeteer.elementhandle.isintersectingviewport.md) | | Resolves to true if the element is visible in the current viewport. |
| [press(key, options)](./puppeteer.elementhandle.press.md) | | Focuses the element, and then uses [Keyboard.down()](./puppeteer.keyboard.down.md) and [Keyboard.up()](./puppeteer.keyboard.up.md). |
| [screenshot(this, options)](./puppeteer.elementhandle.screenshot.md) | | This method scrolls element into view if needed, and then uses [Page.screenshot()](./puppeteer.page.screenshot.md) to take a screenshot of the element. If the element is detached from DOM, the method throws an error. |
| [screenshot(this, options)](./puppeteer.elementhandle.screenshot.md) | | This method scrolls element into view if needed, and then uses to take a screenshot of the element. If the element is detached from DOM, the method throws an error. |
| [select(values)](./puppeteer.elementhandle.select.md) | | Triggers a <code>change</code> and <code>input</code> event once all the provided options have been selected. If there's no <code>&lt;select&gt;</code> element matching <code>selector</code>, the method throws an error. |
| [tap(this)](./puppeteer.elementhandle.tap.md) | | This method scrolls element into view if needed, and then uses [Touchscreen.tap()](./puppeteer.touchscreen.tap.md) to tap in the center of the element. If the element is detached from DOM, the method throws an error. |
| [toElement(tagName)](./puppeteer.elementhandle.toelement.md) | | Converts the current handle to the given element type. |

View File

@ -4,7 +4,7 @@ sidebar_label: ElementHandle.screenshot
# ElementHandle.screenshot() method
This method scrolls element into view if needed, and then uses [Page.screenshot()](./puppeteer.page.screenshot.md) to take a screenshot of the element. If the element is detached from DOM, the method throws an error.
This method scrolls element into view if needed, and then uses to take a screenshot of the element. If the element is detached from DOM, the method throws an error.
#### Signature:

View File

@ -126,6 +126,8 @@ page.off('request', logRequest);
| [queryObjects(prototypeHandle)](./puppeteer.page.queryobjects.md) | | This method iterates the JavaScript heap and finds all objects with the given prototype. |
| [reload(options)](./puppeteer.page.reload.md) | | |
| [screenshot(options)](./puppeteer.page.screenshot.md) | | |
| [screenshot(options)](./puppeteer.page.screenshot_1.md) | | |
| [screenshot(options)](./puppeteer.page.screenshot_2.md) | | |
| [select(selector, values)](./puppeteer.page.select.md) | | Triggers a <code>change</code> and <code>input</code> event once all the provided options have been selected. If there's no <code>&lt;select&gt;</code> element matching <code>selector</code>, the method throws an error. |
| [setBypassCSP(enabled)](./puppeteer.page.setbypasscsp.md) | | Toggles bypassing page's Content-Security-Policy. |
| [setCacheEnabled(enabled)](./puppeteer.page.setcacheenabled.md) | | Toggles ignoring cache for each request based on the enabled state. By default, caching is enabled. |

View File

@ -8,19 +8,23 @@ sidebar_label: Page.screenshot
```typescript
class Page {
screenshot(options?: ScreenshotOptions): Promise<Buffer | string>;
screenshot(
options: ScreenshotOptions & {
encoding: 'base64';
}
): Promise<string>;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | ----------------------------------------------------- | ----------------- |
| options | [ScreenshotOptions](./puppeteer.screenshotoptions.md) | <i>(Optional)</i> |
| Parameter | Type | Description |
| --------- | ----------------------------------------------------------------------------------- | ----------- |
| options | [ScreenshotOptions](./puppeteer.screenshotoptions.md) &amp; { encoding: 'base64'; } | |
**Returns:**
Promise&lt;Buffer \| string&gt;
Promise&lt;string&gt;
Promise which resolves to buffer or a base64 string (depending on the value of `encoding`) with captured screenshot.

View File

@ -0,0 +1,27 @@
---
sidebar_label: Page.screenshot_1
---
# Page.screenshot() method
#### Signature:
```typescript
class Page {
screenshot(
options?: ScreenshotOptions & {
encoding?: 'binary';
}
): Promise<Buffer>;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | ------------------------------------------------------------------------------------ | ----------------- |
| options | [ScreenshotOptions](./puppeteer.screenshotoptions.md) &amp; { encoding?: 'binary'; } | <i>(Optional)</i> |
**Returns:**
Promise&lt;Buffer&gt;

View File

@ -0,0 +1,23 @@
---
sidebar_label: Page.screenshot_2
---
# Page.screenshot() method
#### Signature:
```typescript
class Page {
screenshot(options?: ScreenshotOptions): Promise<Buffer | string>;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | ----------------------------------------------------- | ----------------- |
| options | [ScreenshotOptions](./puppeteer.screenshotoptions.md) | <i>(Optional)</i> |
**Returns:**
Promise&lt;Buffer \| string&gt;

View File

@ -2123,6 +2123,12 @@ export class Page extends EventEmitter {
* @returns Promise which resolves to buffer or a base64 string (depending on
* the value of `encoding`) with captured screenshot.
*/
screenshot(
options: ScreenshotOptions & {encoding: 'base64'}
): Promise<string>;
screenshot(
options?: ScreenshotOptions & {encoding?: 'binary'}
): Promise<Buffer>;
async screenshot(options?: ScreenshotOptions): Promise<Buffer | string>;
async screenshot(): Promise<Buffer | string> {
throw new Error('Not implemented');

View File

@ -1270,6 +1270,12 @@ export class CDPPage extends Page {
await this.#frameManager.networkManager.setCacheEnabled(enabled);
}
override screenshot(
options: ScreenshotOptions & {encoding: 'base64'}
): Promise<string>;
override screenshot(
options?: ScreenshotOptions & {encoding?: 'binary'}
): Promise<Buffer>;
override async screenshot(
options: ScreenshotOptions = {}
): Promise<Buffer | string> {