How to setup Codeception, Selenium and Docker Machine

How to setup Codeception, Selenium and Docker Machine

Running automated acceptance tests on a website can be tricky when using Selenium and Docker.  This post highlights the important configuration that is needed and what to look out for when testing your web app in Docker containers.

The setup

  • The web app that needs testing is a in a Docker container it is called cms
  • The web app is also conneted to other Docker containers in a user defined network
  • All of the Docker cotnainers are running within a Docker Machine so they should not be treated like they are running on localhost
  • Codecepetion is used for acceptance testing

Getting the Docker Machine details

Since this application is running in a Docker machine and not a typical localhost the machines details are required. Run the following to get the details

$ docker-machine env howtocodewell-site-dev

This will display the following details‌‌‌‌

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.101:2376"
export DOCKER_CERT_PATH="/Users/peterfisher/.docker/machine/machines/howtocodewell-site-dev"
export DOCKER_MACHINE_NAME="howtocodewell-site-dev"
# Run this command to configure your shell:
# eval $(docker-machine env howtocodewell-site-dev)

Here we can see the Docker host IP which we will need later.

Make sure you have evaluated the shell to use the Docker Machine

eval $(docker-machine env howtocodewell-site-dev)

Configuring Docker compose

To make things easier we are going to use Docker compose to setup the Docker services.  This is the service for the cms:

  cms:
    build:
      context: .
      target: webserver-dev
    networks:
      - howtocodewell

I've omitted alot of cms configuration. The only important configuration for this task is the service name and it's assigned network.

This is the Docker service for the Selenium container

  test:
    image: selenium/standalone-chrome:4.0.0-alpha-7-prerelease-20200921
    ports:
      - 4444:4444
    container_name: test
    networks:
      - howtocodewell

Notice that we have called both the service and container test.  This makes it easier to identify the contaner in the future.  The image is selenium/standalone-chrome:4.0.0-alpha-7-prerelease-20200921 which may not be the same one that you need.
The container is also assigned the same network as the cms service and we've exposed and published port 4444

Configure Codeception

Edit  tests/acceptance.suite.yml and add the following

actor: AcceptanceTester
modules:
    enabled:
        - \App\Tests\Helper\Acceptance
        - WebDriver:
             url: 'http://cms'
             host: '192.168.99.101'
             port: 4444
             browser: chrome

Notice that we have used the Docker Machines IP as the host and the url is the Docker service name for the cms.

Spin up those Docker services

Now that the Docker services and Codeception have been configured it is time to get the containers running. We do this like so:

docker-compose up -d --build

Run those tests

It's almost time to run the tests but first open up a terminal and tail the logs for test container like so

docker logs -f test

Now open up another terminal and run the tests. Make sure that both terminal windows have evaluated the Docker machine shell.

docker-compose exec api bin/codecept run acceptance

Keep an eye on the test logs to see if any errors appear.

If done sucessfully you should see passing acceptance tests.