1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jest-puppeteerでパスワード付きProxyを越える

Posted at

jest-puppeteer.config.json のブラウザ起動オプションにProxyのアドレスを設定する。

jest-puppeteer.config.json
module.exports = {
  launch: {
    args: [
      '--proxy-server=your.proxy.domain:port',
    ]
  }
}

一見、 --proxy-server オプションにユーザー名もパスも含めて user:pass@domain:port のように指定すれば良さそうに思えるが、これは上手く動作しない。
コード上で認証情報を設定する必要がある。

今回は custom-environment.js に設定する例を記す。

custom-environment.js
const PuppeteerEnvironment = require('jest-environment-puppeteer');

class CustomEnvironment extends PuppeteerEnvironment {
  async setup() {
    await super.setup();

    const page = this.context.page;
    // authenticateメソッドで認証を行う
    // ユーザー名、パスワードは環境変数から取るようにしたほうが良いだろう
    await page.authenticate({
      username: process.env.PROXY_USER,
      password: process.env.PROXY_PASS
    });
  }
}

module.exports = CustomEnvironment;

余談だが、ブラウザの起動オプションは他にも色々指定することが出来、オプションはコチラにリストアップされているようだ。
他にも幾つかProxyに関するオプションがあるので参考にすると良い。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?