LoginSignup
14
7

More than 3 years have passed since last update.

CircleCIでChromeDriverを立ち上げる

Posted at

なにこれ

CircleCIでGoogoleChromeをheadlessモードで動かす際に、エラーが出たので対応方法を残す。

どんなエラー?

LaravelをPHPUnitでテストしたときにテストコードで以下エラー発生

There was 1 error:

1) Tests\Unit\HogeTest::testGetUrl
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--headless","--no-sandbox","--disable-gpu","--window-size=1280,960","--user-agent=\"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/27.0.1453.116 Safari\/537.36\""]}}}

Failed to connect to 127.0.0.1 port 9515: Connection refused

WebDriverでChromeDriverにアクセスするときに、ChromeDriverが立ち上がっていなくて9515ポートにアクセスできていないようだった。

対応方法

  1. CircleCIのconfig.ymlを編集してGoogleChromeとChromeDriverをインストールする
  2. ChromeDriverをバックグラウンド起動する

GoogleChromeとChromeDriverのインストール

apt installを使っても良かったのですが、CircleCIのイメージで最初からGoogleChromeとChromeDriver(とFirefoxとGeckodriver)をインストールするイメージが提供されているので、そちらを利用する。イメージ名のあとに-browsersと追記するだけ。

config.yml
# 一部抜粋しています
version: 2
jobs:
  build:
    docker:
      # イメージ名の後に-browsersを追記するだけ
      - image: circleci/php:5.6-node-browsers
      - image: circleci/mysql:5.7

詳しい方法はこちらから
https://circleci.com/docs/2.0/browser-testing/

これだけでGoogleChromeとChromeDriverをインストールした状態を作れるのでありがたいですね。

ChromeDriverをバックグラウンド起動する

ちょいハマりした箇所。
普通に- run: chromedriver &とか記述してもバックグラウンド起動ができませんでした。
CircleCIのドキュメントを確認するとrunのオプションにはbackgroundというオプションが存在していて、これを使う必要があるらしい。
background オプションを指定したら、正しくバックグラウンドでChromeDriverが起動するようになった。

config.yml
version: 2
  jobs:
    build:
      docker:
        # specify the version you desire here
        - image: circleci/php:5.6-node-browsers
        - image: circleci/mysql:5.7

      working_directory: ~/repo

      steps:
        # chromedriverをバックグラウンドで起動する
        - run:
            command: chromedriver
            background: true

        - checkout
        - run: sudo apt update
        - run: sudo apt upgrade

        # 以下略

これでOK。

おわりに

seleniumでの記事だったらたくさん見つかったけど、ChromeDriver単体での記事が少なかったので書きました。

14
7
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
14
7