fix: deprecate indirect error imports (#9072)

This commit is contained in:
jrandolf 2022-10-06 16:21:24 +02:00 committed by GitHub
parent 3675f168ef
commit 6ab823ef34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 133 additions and 138 deletions

View File

@ -121,13 +121,13 @@ sidebar_label: API
## Variables ## Variables
| Variable | Description | | Variable | Description |
| --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [connect](./puppeteer.connect.md) | | | [connect](./puppeteer.connect.md) | |
| [createBrowserFetcher](./puppeteer.createbrowserfetcher.md) | | | [createBrowserFetcher](./puppeteer.createbrowserfetcher.md) | |
| [DEFAULT_INTERCEPT_RESOLUTION_PRIORITY](./puppeteer.default_intercept_resolution_priority.md) | The default cooperative request interception resolution priority | | [DEFAULT_INTERCEPT_RESOLUTION_PRIORITY](./puppeteer.default_intercept_resolution_priority.md) | The default cooperative request interception resolution priority |
| [defaultArgs](./puppeteer.defaultargs.md) | | | [defaultArgs](./puppeteer.defaultargs.md) | |
| [devices](./puppeteer.devices.md) | A list of devices to be used with <code>page.emulate(options)</code>. Actual list of devices can be found in [src/common/DeviceDescriptors.ts](https://github.com/puppeteer/puppeteer/blob/main/src/common/DeviceDescriptors.ts). | | [devices](./puppeteer.devices.md) | A list of devices to be used with <code>page.emulate(options)</code>. Actual list of devices can be found in [src/common/DeviceDescriptors.ts](https://github.com/puppeteer/puppeteer/blob/main/src/common/DeviceDescriptors.ts). |
| [errors](./puppeteer.errors.md) | <p>Puppeteer methods might throw errors if they are unable to fulfill a request. For example, <code>page.waitForSelector(selector[, options])</code> might fail if the selector doesn't match any nodes during the given timeframe.</p><p>For certain types of errors Puppeteer uses specific error classes. These classes are available via <code>puppeteer.errors</code>.</p> | | [errors](./puppeteer.errors.md) | |
| [EVALUATION_SCRIPT_URL](./puppeteer.evaluation_script_url.md) | | | [EVALUATION_SCRIPT_URL](./puppeteer.evaluation_script_url.md) | |
| [executablePath](./puppeteer.executablepath.md) | | | [executablePath](./puppeteer.executablepath.md) | |
| [launch](./puppeteer.launch.md) | | | [launch](./puppeteer.launch.md) | |

View File

@ -1,21 +0,0 @@
---
sidebar_label: CustomError.(constructor)
---
# CustomError.(constructor)
Constructs a new instance of the `CustomError` class
**Signature:**
```typescript
class CustomError {
constructor(message?: string);
}
```
## Parameters
| Parameter | Type | Description |
| --------- | ------ | ----------------- |
| message | string | <i>(Optional)</i> |

View File

@ -4,6 +4,10 @@ sidebar_label: CustomError
# CustomError class # CustomError class
> Warning: This API is now obsolete.
>
> Do not use.
**Signature:** **Signature:**
```typescript ```typescript
@ -12,8 +16,6 @@ export declare class CustomError extends Error
**Extends:** Error **Extends:** Error
## Constructors ## Remarks
| Constructor | Modifiers | Description | The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `CustomError` class.
| ------------------------------------------------------------------ | --------- | --------------------------------------------------------------- |
| [(constructor)(message)](./puppeteer.customerror._constructor_.md) | | Constructs a new instance of the <code>CustomError</code> class |

View File

@ -4,9 +4,13 @@ sidebar_label: errors
# errors variable # errors variable
Puppeteer methods might throw errors if they are unable to fulfill a request. For example, `page.waitForSelector(selector[, options])` might fail if the selector doesn't match any nodes during the given timeframe. > Warning: This API is now obsolete.
>
For certain types of errors Puppeteer uses specific error classes. These classes are available via `puppeteer.errors`. > Import error classes directly.
>
> Puppeteer methods might throw errors if they are unable to fulfill a request. For example, `page.waitForSelector(selector[, options])` might fail if the selector doesn't match any nodes during the given timeframe.
>
> For certain types of errors Puppeteer uses specific error classes. These classes are available via `puppeteer.errors`.
**Signature:** **Signature:**
@ -22,7 +26,7 @@ An example of handling a timeout error:
try { try {
await page.waitForSelector('.foo'); await page.waitForSelector('.foo');
} catch (e) { } catch (e) {
if (e instanceof puppeteer.errors.TimeoutError) { if (e instanceof TimeoutError) {
// Do something if this is a timeout. // Do something if this is a timeout.
} }
} }

View File

@ -8,6 +8,6 @@ sidebar_label: ProtocolError.code
```typescript ```typescript
class ProtocolError { class ProtocolError {
code?: number; set code(code: number | undefined);
} }
``` ```

View File

@ -17,6 +17,6 @@ export declare class ProtocolError extends CustomError
## Properties ## Properties
| Property | Modifiers | Type | Description | | Property | Modifiers | Type | Description |
| --------------------------------------------------------------- | --------- | ------ | ----------------- | | --------------------------------------------------------------- | --------- | ------------------- | ----------- |
| [code?](./puppeteer.protocolerror.code.md) | | number | <i>(Optional)</i> | | [code](./puppeteer.protocolerror.code.md) | | number \| undefined | |
| [originalMessage](./puppeteer.protocolerror.originalmessage.md) | | string | | | [originalMessage](./puppeteer.protocolerror.originalmessage.md) | | string | |

View File

@ -8,6 +8,6 @@ sidebar_label: ProtocolError.originalMessage
```typescript ```typescript
class ProtocolError { class ProtocolError {
originalMessage: string; set originalMessage(originalMessage: string);
} }
``` ```

View File

@ -1,23 +0,0 @@
---
sidebar_label: Puppeteer.errors
---
# Puppeteer.errors property
> Warning: This API is now obsolete.
>
> Import directly puppeteer.
**Signature:**
```typescript
class Puppeteer {
get errors(): typeof errors;
}
```
## Example
```ts
import {errors} from 'puppeteer';
```

View File

@ -23,7 +23,6 @@ The constructor for this class is marked as internal. Third-party code should no
| Property | Modifiers | Type | Description | | Property | Modifiers | Type | Description |
| --------------------------------------------------------------- | --------------------- | ------------------------------------------------------------ | ----------- | | --------------------------------------------------------------- | --------------------- | ------------------------------------------------------------ | ----------- |
| [devices](./puppeteer.puppeteer.devices.md) | <code>readonly</code> | typeof [devices](./puppeteer.devices.md) | | | [devices](./puppeteer.puppeteer.devices.md) | <code>readonly</code> | typeof [devices](./puppeteer.devices.md) | |
| [errors](./puppeteer.puppeteer.errors.md) | <code>readonly</code> | typeof [errors](./puppeteer.errors.md) | |
| [networkConditions](./puppeteer.puppeteer.networkconditions.md) | <code>readonly</code> | typeof [networkConditions](./puppeteer.networkconditions.md) | | | [networkConditions](./puppeteer.puppeteer.networkconditions.md) | <code>readonly</code> | typeof [networkConditions](./puppeteer.networkconditions.md) | |
## Methods ## Methods

View File

@ -4,6 +4,10 @@ sidebar_label: PuppeteerErrors
# PuppeteerErrors interface # PuppeteerErrors interface
> Warning: This API is now obsolete.
>
> Do not use.
**Signature:** **Signature:**
```typescript ```typescript

View File

@ -15,9 +15,14 @@
*/ */
/** /**
* @deprecated Do not use.
*
* @public * @public
*/ */
export class CustomError extends Error { export class CustomError extends Error {
/**
* @internal
*/
constructor(message?: string) { constructor(message?: string) {
super(message); super(message);
this.name = this.constructor.name; this.name = this.constructor.name;
@ -43,11 +48,39 @@ export class TimeoutError extends CustomError {}
* @public * @public
*/ */
export class ProtocolError extends CustomError { export class ProtocolError extends CustomError {
public code?: number; #code?: number;
public originalMessage = ''; #originalMessage = '';
/**
* @internal
*/
set code(code: number | undefined) {
this.#code = code;
}
/**
* @public
*/
get code(): number | undefined {
return this.#code;
}
/**
* @internal
*/
set originalMessage(originalMessage: string) {
this.#originalMessage = originalMessage;
}
/**
* @public
*/
get originalMessage(): string {
return this.#originalMessage;
}
} }
/** /**
* @deprecated Do not use.
*
* @public * @public
*/ */
export interface PuppeteerErrors { export interface PuppeteerErrors {
@ -56,6 +89,8 @@ export interface PuppeteerErrors {
} }
/** /**
* @deprecated Import error classes directly.
*
* Puppeteer methods might throw errors if they are unable to fulfill a request. * Puppeteer methods might throw errors if they are unable to fulfill a request.
* For example, `page.waitForSelector(selector[, options])` might fail if the * For example, `page.waitForSelector(selector[, options])` might fail if the
* selector doesn't match any nodes during the given timeframe. * selector doesn't match any nodes during the given timeframe.
@ -70,7 +105,7 @@ export interface PuppeteerErrors {
* try { * try {
* await page.waitForSelector('.foo'); * await page.waitForSelector('.foo');
* } catch (e) { * } catch (e) {
* if (e instanceof puppeteer.errors.TimeoutError) { * if (e instanceof TimeoutError) {
* // Do something if this is a timeout. * // Do something if this is a timeout.
* } * }
* } * }

View File

@ -20,7 +20,6 @@ import {
} from './BrowserConnector.js'; } from './BrowserConnector.js';
import {ConnectionTransport} from './ConnectionTransport.js'; import {ConnectionTransport} from './ConnectionTransport.js';
import {devices} from './DeviceDescriptors.js'; import {devices} from './DeviceDescriptors.js';
import {errors} from './Errors.js';
import {networkConditions} from './NetworkConditions.js'; import {networkConditions} from './NetworkConditions.js';
import { import {
clearCustomQueryHandlers, clearCustomQueryHandlers,
@ -99,18 +98,6 @@ export class Puppeteer {
return devices; return devices;
} }
/**
* @deprecated Import directly puppeteer.
* @example
*
* ```ts
* import {errors} from 'puppeteer';
* ```
*/
get errors(): typeof errors {
return errors;
}
/** /**
* @deprecated Import directly puppeteer. * @deprecated Import directly puppeteer.
* @example * @example

View File

@ -24,6 +24,7 @@ import {
import {ElementHandle} from 'puppeteer-core/internal/common/ElementHandle.js'; import {ElementHandle} from 'puppeteer-core/internal/common/ElementHandle.js';
import utils from './utils.js'; import utils from './utils.js';
import assert from 'assert'; import assert from 'assert';
import {TimeoutError} from 'puppeteer';
describe('AriaQueryHandler', () => { describe('AriaQueryHandler', () => {
setupTestBrowserHooks(); setupTestBrowserHooks();
@ -516,7 +517,7 @@ describe('AriaQueryHandler', () => {
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
const error = await page const error = await page
.waitForSelector('aria/[role="button"]', { .waitForSelector('aria/[role="button"]', {
@ -528,7 +529,7 @@ describe('AriaQueryHandler', () => {
expect(error.message).toContain( expect(error.message).toContain(
'Waiting for selector `[role="button"]` failed: Waiting failed: 10ms exceeded' 'Waiting for selector `[role="button"]` failed: Waiting failed: 10ms exceeded'
); );
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should have an error message specifically for awaiting an element to be hidden', async () => { it('should have an error message specifically for awaiting an element to be hidden', async () => {

View File

@ -15,6 +15,7 @@
*/ */
import expect from 'expect'; import expect from 'expect';
import {TimeoutError} from 'puppeteer';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js'; import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {waitEvent} from './utils.js'; import {waitEvent} from './utils.js';
@ -96,7 +97,7 @@ describe('BrowserContext', function () {
await context.close(); await context.close();
}); });
it('should wait for a target', async () => { it('should wait for a target', async () => {
const {browser, puppeteer, server} = getTestState(); const {browser, server} = getTestState();
const context = await browser.createIncognitoBrowserContext(); const context = await browser.createIncognitoBrowserContext();
let resolved = false; let resolved = false;
@ -110,7 +111,7 @@ describe('BrowserContext', function () {
}) })
.catch(error => { .catch(error => {
resolved = true; resolved = true;
if (error instanceof puppeteer.errors.TimeoutError) { if (error instanceof TimeoutError) {
console.error(error); console.error(error);
} else { } else {
throw error; throw error;
@ -123,7 +124,7 @@ describe('BrowserContext', function () {
const target = await targetPromise; const target = await targetPromise;
expect(await target.page()).toBe(page); expect(await target.page()).toBe(page);
} catch (error) { } catch (error) {
if (error instanceof puppeteer.errors.TimeoutError) { if (error instanceof TimeoutError) {
console.error(error); console.error(error);
} else { } else {
throw error; throw error;
@ -133,7 +134,7 @@ describe('BrowserContext', function () {
}); });
it('should timeout waiting for a non-existent target', async () => { it('should timeout waiting for a non-existent target', async () => {
const {browser, puppeteer, server} = getTestState(); const {browser, server} = getTestState();
const context = await browser.createIncognitoBrowserContext(); const context = await browser.createIncognitoBrowserContext();
const error = await context const error = await context
@ -148,7 +149,7 @@ describe('BrowserContext', function () {
.catch(error_ => { .catch(error_ => {
return error_; return error_;
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
await context.close(); await context.close();
}); });

View File

@ -21,6 +21,7 @@ import {
setupTestBrowserHooks, setupTestBrowserHooks,
setupTestPageAndContextHooks, setupTestPageAndContextHooks,
} from './mocha-utils.js'; } from './mocha-utils.js';
import {TimeoutError} from 'puppeteer';
const FILE_TO_UPLOAD = path.join(__dirname, '/../assets/file-to-upload.txt'); const FILE_TO_UPLOAD = path.join(__dirname, '/../assets/file-to-upload.txt');
@ -100,33 +101,33 @@ describe('input tests', function () {
expect(chooser).toBeTruthy(); expect(chooser).toBeTruthy();
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
await page.waitForFileChooser({timeout: 1}).catch(error_ => { await page.waitForFileChooser({timeout: 1}).catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should respect default timeout when there is no custom timeout', async () => { it('should respect default timeout when there is no custom timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
page.setDefaultTimeout(1); page.setDefaultTimeout(1);
let error!: Error; let error!: Error;
await page.waitForFileChooser().catch(error_ => { await page.waitForFileChooser().catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should prioritize exact timeout over default timeout', async () => { it('should prioritize exact timeout over default timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
page.setDefaultTimeout(0); page.setDefaultTimeout(0);
let error!: Error; let error!: Error;
await page.waitForFileChooser({timeout: 1}).catch(error_ => { await page.waitForFileChooser({timeout: 1}).catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should work with no timeout', async () => { it('should work with no timeout', async () => {
const {page} = getTestState(); const {page} = getTestState();

View File

@ -26,6 +26,7 @@ import {Page} from 'puppeteer-core/internal/api/Page.js';
import {Product} from 'puppeteer-core/internal/common/Product.js'; import {Product} from 'puppeteer-core/internal/common/Product.js';
import {getTestState, itOnlyRegularInstall} from './mocha-utils.js'; import {getTestState, itOnlyRegularInstall} from './mocha-utils.js';
import utils from './utils.js'; import utils from './utils.js';
import {TimeoutError} from 'puppeteer';
const mkdtempAsync = promisify(fs.mkdtemp); const mkdtempAsync = promisify(fs.mkdtemp);
const readFileAsync = promisify(fs.readFile); const readFileAsync = promisify(fs.readFile);
@ -529,7 +530,7 @@ describe('Launcher specs', function () {
await puppeteer.launch(options).catch(error_ => { await puppeteer.launch(options).catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should work with timeout = 0', async () => { it('should work with timeout = 0', async () => {
const {puppeteer, defaultBrowserOptions} = getTestState(); const {puppeteer, defaultBrowserOptions} = getTestState();

View File

@ -24,6 +24,7 @@ import {
import os from 'os'; import os from 'os';
import {ServerResponse} from 'http'; import {ServerResponse} from 'http';
import {HTTPRequest} from 'puppeteer-core/internal/common/HTTPRequest.js'; import {HTTPRequest} from 'puppeteer-core/internal/common/HTTPRequest.js';
import {TimeoutError} from 'puppeteer';
describe('navigation', function () { describe('navigation', function () {
setupTestBrowserHooks(); setupTestBrowserHooks();
@ -228,7 +229,7 @@ describe('navigation', function () {
} }
}); });
it('should fail when exceeding maximum navigation timeout', async () => { it('should fail when exceeding maximum navigation timeout', async () => {
const {page, server, puppeteer} = getTestState(); const {page, server} = getTestState();
// Hang for request to the empty.html // Hang for request to the empty.html
server.setRoute('/empty.html', () => {}); server.setRoute('/empty.html', () => {});
@ -239,10 +240,10 @@ describe('navigation', function () {
return (error = error_); return (error = error_);
}); });
expect(error.message).toContain('Navigation timeout of 1 ms exceeded'); expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should fail when exceeding default maximum navigation timeout', async () => { it('should fail when exceeding default maximum navigation timeout', async () => {
const {page, server, puppeteer} = getTestState(); const {page, server} = getTestState();
// Hang for request to the empty.html // Hang for request to the empty.html
server.setRoute('/empty.html', () => {}); server.setRoute('/empty.html', () => {});
@ -252,10 +253,10 @@ describe('navigation', function () {
return (error = error_); return (error = error_);
}); });
expect(error.message).toContain('Navigation timeout of 1 ms exceeded'); expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should fail when exceeding default maximum timeout', async () => { it('should fail when exceeding default maximum timeout', async () => {
const {page, server, puppeteer} = getTestState(); const {page, server} = getTestState();
// Hang for request to the empty.html // Hang for request to the empty.html
server.setRoute('/empty.html', () => {}); server.setRoute('/empty.html', () => {});
@ -265,10 +266,10 @@ describe('navigation', function () {
return (error = error_); return (error = error_);
}); });
expect(error.message).toContain('Navigation timeout of 1 ms exceeded'); expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should prioritize default navigation timeout over default timeout', async () => { it('should prioritize default navigation timeout over default timeout', async () => {
const {page, server, puppeteer} = getTestState(); const {page, server} = getTestState();
// Hang for request to the empty.html // Hang for request to the empty.html
server.setRoute('/empty.html', () => {}); server.setRoute('/empty.html', () => {});
@ -279,7 +280,7 @@ describe('navigation', function () {
return (error = error_); return (error = error_);
}); });
expect(error.message).toContain('Navigation timeout of 1 ms exceeded'); expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should disable timeout when its set to 0', async () => { it('should disable timeout when its set to 0', async () => {
const {page, server} = getTestState(); const {page, server} = getTestState();

View File

@ -28,6 +28,7 @@ import {
} from './mocha-utils.js'; } from './mocha-utils.js';
import utils, {attachFrame, waitEvent} from './utils.js'; import utils, {attachFrame, waitEvent} from './utils.js';
import {CDPPage} from 'puppeteer-core/internal/common/Page.js'; import {CDPPage} from 'puppeteer-core/internal/common/Page.js';
import {TimeoutError} from 'puppeteer';
describe('Page', function () { describe('Page', function () {
setupTestBrowserHooks(); setupTestBrowserHooks();
@ -924,7 +925,7 @@ describe('Page', function () {
expect(request.url()).toBe(server.PREFIX + '/digits/2.png'); expect(request.url()).toBe(server.PREFIX + '/digits/2.png');
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
await page await page
@ -937,10 +938,10 @@ describe('Page', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should respect default timeout', async () => { it('should respect default timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
page.setDefaultTimeout(1); page.setDefaultTimeout(1);
@ -951,7 +952,7 @@ describe('Page', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should work with no timeout', async () => { it('should work with no timeout', async () => {
const {page, server} = getTestState(); const {page, server} = getTestState();
@ -987,7 +988,7 @@ describe('Page', function () {
expect(response.url()).toBe(server.PREFIX + '/digits/2.png'); expect(response.url()).toBe(server.PREFIX + '/digits/2.png');
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
await page await page
@ -1000,10 +1001,10 @@ describe('Page', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should respect default timeout', async () => { it('should respect default timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
page.setDefaultTimeout(1); page.setDefaultTimeout(1);
@ -1014,7 +1015,7 @@ describe('Page', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should work with predicate', async () => { it('should work with predicate', async () => {
const {page, server} = getTestState(); const {page, server} = getTestState();
@ -1101,12 +1102,12 @@ describe('Page', function () {
expect(t1 - t2).toBeGreaterThanOrEqual(400); expect(t1 - t2).toBeGreaterThanOrEqual(400);
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
await page.waitForNetworkIdle({timeout: 1}).catch(error_ => { await page.waitForNetworkIdle({timeout: 1}).catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should respect idleTime', async () => { it('should respect idleTime', async () => {
const {page, server} = getTestState(); const {page, server} = getTestState();
@ -1440,7 +1441,7 @@ describe('Page', function () {
expect(result).toBe(`${doctype}${expectedOutput}`); expect(result).toBe(`${doctype}${expectedOutput}`);
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, server, puppeteer} = getTestState(); const {page, server} = getTestState();
const imgPath = '/img.png'; const imgPath = '/img.png';
// stall for image // stall for image
@ -1453,10 +1454,10 @@ describe('Page', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should respect default navigation timeout', async () => { it('should respect default navigation timeout', async () => {
const {page, server, puppeteer} = getTestState(); const {page, server} = getTestState();
page.setDefaultNavigationTimeout(1); page.setDefaultNavigationTimeout(1);
const imgPath = '/img.png'; const imgPath = '/img.png';
@ -1468,7 +1469,7 @@ describe('Page', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
it('should await resources to load', async () => { it('should await resources to load', async () => {
const {page, server} = getTestState(); const {page, server} = getTestState();
@ -2053,7 +2054,7 @@ describe('Page', function () {
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {isHeadless, page, server, puppeteer} = getTestState(); const {isHeadless, page, server} = getTestState();
if (!isHeadless) { if (!isHeadless) {
return; return;
} }
@ -2064,7 +2065,7 @@ describe('Page', function () {
await page.pdf({timeout: 1}).catch(_error => { await page.pdf({timeout: 1}).catch(_error => {
return (error = _error); return (error = _error);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
}); });

View File

@ -16,6 +16,7 @@
import expect from 'expect'; import expect from 'expect';
import {ServerResponse} from 'http'; import {ServerResponse} from 'http';
import {TimeoutError} from 'puppeteer';
import {Page} from 'puppeteer-core/internal/api/Page.js'; import {Page} from 'puppeteer-core/internal/api/Page.js';
import {Target} from 'puppeteer-core/internal/common/Target.js'; import {Target} from 'puppeteer-core/internal/common/Target.js';
import { import {
@ -310,7 +311,7 @@ describe('Target', function () {
describe('Browser.waitForTarget', () => { describe('Browser.waitForTarget', () => {
it('should wait for a target', async () => { it('should wait for a target', async () => {
const {browser, puppeteer, server} = getTestState(); const {browser, server} = getTestState();
let resolved = false; let resolved = false;
const targetPromise = browser.waitForTarget(target => { const targetPromise = browser.waitForTarget(target => {
@ -322,7 +323,7 @@ describe('Target', function () {
}) })
.catch(error => { .catch(error => {
resolved = true; resolved = true;
if (error instanceof puppeteer.errors.TimeoutError) { if (error instanceof TimeoutError) {
console.error(error); console.error(error);
} else { } else {
throw error; throw error;
@ -335,7 +336,7 @@ describe('Target', function () {
const target = await targetPromise; const target = await targetPromise;
expect(await target.page()).toBe(page); expect(await target.page()).toBe(page);
} catch (error) { } catch (error) {
if (error instanceof puppeteer.errors.TimeoutError) { if (error instanceof TimeoutError) {
console.error(error); console.error(error);
} else { } else {
throw error; throw error;
@ -344,7 +345,7 @@ describe('Target', function () {
await page.close(); await page.close();
}); });
it('should timeout waiting for a non-existent target', async () => { it('should timeout waiting for a non-existent target', async () => {
const {browser, server, puppeteer} = getTestState(); const {browser, server} = getTestState();
let error!: Error; let error!: Error;
await browser await browser
@ -359,7 +360,7 @@ describe('Target', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
}); });
}); });
}); });

View File

@ -15,6 +15,7 @@
*/ */
import expect from 'expect'; import expect from 'expect';
import {TimeoutError} from 'puppeteer';
import {isErrorLike} from 'puppeteer-core/internal/util/ErrorLike.js'; import {isErrorLike} from 'puppeteer-core/internal/util/ErrorLike.js';
import { import {
createTimeout, createTimeout,
@ -240,7 +241,7 @@ describe('waittask specs', function () {
await waitForFunction; await waitForFunction;
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
await page await page
@ -254,11 +255,11 @@ describe('waittask specs', function () {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
expect(error?.message).toContain('Waiting failed: 10ms exceeded'); expect(error?.message).toContain('Waiting failed: 10ms exceeded');
}); });
it('should respect default timeout', async () => { it('should respect default timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
page.setDefaultTimeout(1); page.setDefaultTimeout(1);
let error!: Error; let error!: Error;
@ -269,7 +270,7 @@ describe('waittask specs', function () {
.catch(error_ => { .catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
expect(error?.message).toContain('Waiting failed: 1ms exceeded'); expect(error?.message).toContain('Waiting failed: 1ms exceeded');
}); });
it('should disable timeout when its set to 0', async () => { it('should disable timeout when its set to 0', async () => {
@ -669,13 +670,13 @@ describe('waittask specs', function () {
expect(handle).toBe(null); expect(handle).toBe(null);
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
await page.waitForSelector('div', {timeout: 10}).catch(error_ => { await page.waitForSelector('div', {timeout: 10}).catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
expect(error?.message).toContain( expect(error?.message).toContain(
'Waiting for selector `div` failed: Waiting failed: 10ms exceeded' 'Waiting for selector `div` failed: Waiting failed: 10ms exceeded'
); );
@ -755,13 +756,13 @@ describe('waittask specs', function () {
).toBe('hello world '); ).toBe('hello world ');
}); });
it('should respect timeout', async () => { it('should respect timeout', async () => {
const {page, puppeteer} = getTestState(); const {page} = getTestState();
let error!: Error; let error!: Error;
await page.waitForXPath('//div', {timeout: 10}).catch(error_ => { await page.waitForXPath('//div', {timeout: 10}).catch(error_ => {
return (error = error_); return (error = error_);
}); });
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError); expect(error).toBeInstanceOf(TimeoutError);
expect(error?.message).toContain('Waiting failed: 10ms exceeded'); expect(error?.message).toContain('Waiting failed: 10ms exceeded');
}); });
it('should run in specified frame', async () => { it('should run in specified frame', async () => {