26
16

More than 3 years have passed since last update.

TimeoutError: Navigation timeout of 30000 ms exceededの解消方法

Last updated at Posted at 2020-08-31

Jestでpuppeteerを実行しているとTimeoutエラーが発生することがあります。

TimeoutError: Navigation timeout of 30000 ms exceeded

対策としてページ遷移系の命令のTimeout時間を無効化する方法が2つあります。

スクリプト全体に設定する

全てのページ取得命令に対して一括でTimeoutを設定します

await page.setDefaultNavigationTimeout(0); 
await page.goto('https://www.example.com');

スクリプト全体

top.test.js
describe('Top', () => {
  beforeAll(async () => {
    await page.setDefaultNavigationTimeout(0); 
    await page.goto('https://www.example.com');
  });

  it('should be titled "Example Domain"', async () => {
    await expect(page.title()).resolves.toMatch('Example Domain');
  });
});

ページ取得のオプションを設定する

個別のページ取得に対してTimeoutを設定します


await page.goto('https://www.example.com', {
  waitUntil:'load',
  timeout:0
});

スクリプト全体

top.test.js
describe('Top', () => {
  beforeAll(async () => {
    await page.goto('https://www.example.com', {
      waitUntil:'load',
      timeout:0
    });
  });

  it('should be titled "Example Domain"', async () => {
    await expect(page.title()).resolves.toMatch('Example Domain');
  });
});

参考

page.setDefaultNavigationTimeout() のリファレンス
page.goto() のリファレンス

最後に

30秒タイムアウトの出所ですが、コードを読むとデフォルト値として設定されていました

TimeoutSettings.ts
const DEFAULT_TIMEOUT = 30000;

そして同じデフォルト値を使う設定メソッドがもう1つあることを発見しました

setDefaultTimeout(timeout: number): void {
  this._defaultTimeout = timeout;
}

マニュアルによれば、こちらはページ遷移メソッドに加えて、リクエスト時間などについてもTimeout値を変更できるようです
page.setDefaultTimeout() のリファレンス

案外読みやすいコードだったので、これからはソースコードも確認しようと思いました。

26
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
16