darudaru

だるだるしてるエンジニア

サクッとDockerでCodeception

CodeceptionのAcceptance TestsをDockerで動かします。ドライバはSelenium Webdriverを使います。あ、メリークリスマス!

Dockerの公式サイトに「Each container should have only one concern」と書かれているように、Dockerは1コンテナにつき1サービスを割り当てることを推奨してます。なので、CodeceptionとSelenium Webdriverは別のコンテナに分けて、Docker Composeで全てのコンテナを起動するようにします。 CodeceptionもSelenium Webdriverもimageがすでに作られているので、そちらを活用。

ちなみにdocker-compose.ymlも公式がサンプルを提供してくれてます。優しいね!

github.com

Dcokerの設定

docker-compose.yml

version: '2'
services:
  codecept:
      image: codeception/codeception
      depends_on:
        - chrome
        - firefox
      volumes:
          - './tests:/project/tests'
          - './codeception.yml:/project/codeception.yml'
  chrome:
      image: 'selenium/standalone-chrome-debug:2.53.1'
      ports:
          - '5900'
      # workaround for Docker for Mac, see https://github.com/SeleniumHQ/docker-selenium/issues/227#issuecomment-224865735
      dns: 8.8.4.4
      environment:
        - no_proxy=localhost
  firefox:
      image: 'selenium/standalone-firefox-debug:2.53.1'
      ports:
          - '5900'
      # workaround for Docker for Mac, see https://github.com/SeleniumHQ/docker-selenium/issues/227#issuecomment-224865735
      dns: 8.8.4.4
      environment:
        - no_proxy=localhost

ほぼ提供されているサンプルから設定は変えていません。

Codeceptionの設定

acceptance.suite.yml

# Codeception Test Suite Configuration

# suite for acceptance tests.
# perform tests in browser using the Selenium-like tools.
# powered by Mink (http://mink.behat.org).
# (tip: that's what your customer will see).
# (tip: test your ajax and javascript by one of Mink drivers).

# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

class_name: AcceptanceTester
modules:
    enabled:
        - WebDriver
        - Asserts
        - Helper\AcceptanceHelper
    config:
        WebDriver:
            host: firefox
            url: http://www.example.com/
            browser: firefox
            port: 4444
            window_size: 1024x768
            restart: true
env:
    chrome:
         modules:
            config:
                WebDriver:
                    browser: 'chrome'
                    host: chrome
                    window_size: 1440x960

    firefox:
        # nothing changed

hostをfirefoxに設定。

テストの実行

docker-compose run --rm codecept run -d acceptance example

コンテナが立ち上がってCodeceptionのテストが実行されます。
テスト結果はtests/_outputに出力されます。

ちなみにrunコマンド叩けば、ローカルにイメージなければDocker hubからとって来てくれます。なので、Web Driverのインストールも起動もCodeceptionをcomposerでとってきてーだの全部このコマンド1個叩けば終わりです。楽ですね。

最後にproxyでハマった話

Docker for macでローカルでDocker起動しようとしてたのですが、proxyに引っかかってコンテナからネットが繋がらず困った。

ちなみにDocker for macのproxy設定方法はこちら。
Get started with Docker for Mac | Docker Documentation

結局Macを再起動したところ、無事にテストが実行できるようになりました。proxyの設定反映がうまくいってなかったのかな?