Laravel Dusk is not working on Docker/ docker-compose.yaml

388 views Asked by At

I am working on a Laravel project. I started writing browser tests using Dusk. I am using docker as my development environment. When I run the tests, I am getting the "Connection refused" error.

This is my docker-compose.yaml file.

version: '3'
services:
  apache:
    container_name: res_apache
    image: webdevops/apache:ubuntu-16.04
    environment:
      WEB_DOCUMENT_ROOT: /var/www/public
      WEB_ALIAS_DOMAIN: restaurant.localhost
      WEB_PHP_SOCKET: php-fpm:9000
    volumes: # Only shared dirs to apache (to be served)
      - ./public:/var/www/public:cached
      - ./storage:/var/www/storage:cached
    networks:
      - res-network
    ports:
      - "8081:80"
      - "443:443"
  php-fpm:
    container_name: res_php
    image: jguyomard/laravel-php:7.3
    volumes:
      - ./:/var/www/
      - ./ci:/var/www/ci:cached
      - ./vendor:/var/www/vendor:delegated
      - ./storage:/var/www/storage:delegated
      - ./node_modules:/var/www/node_modules:cached
      - ~/.ssh:/root/.ssh:cached
      - ./composer.json:/var/www/composer.json
      - ./composer.lock:/var/www/composer.lock
      - ~/.composer/cache:/root/.composer/cache:delegated
    networks:
      - res-network
  db:
    container_name: res_db
    image: mariadb:10.2
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: restaurant
      MYSQL_USER: restaurant
      MYSQL_PASSWORD: secret
    volumes:
      - res-data:/var/lib/mysql
    networks:
      - res-network
    ports:
      - "33060:3306"
  chrome:
    image: robcherry/docker-chromedriver
    networks:
      - res-network
    environment:
      CHROMEDRIVER_WHITELISTED_IPS: ""
      CHROMEDRIVER_PORT: "9515"
    ports:
      - 9515:9515
    cap_add:
      - "SYS_ADMIN"
networks:
  res-network:
    driver: "bridge"
volumes:
  res-data:
    driver: "local"

The following is the driver function in DuskTestCase.php class

/**
     * Create the RemoteWebDriver instance.
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu',
            '--headless',
            '--window-size=1920,1080',
        ]);

        return RemoteWebDriver::create(
            'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
            ChromeOptions::CAPABILITY, $options
        )
        );
    }

I run the tests running the following command.

docker-compose exec php-fpm php artisan dusk

Then I get the following error.

Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"args":["--disable-gpu","--headless","--windo
w-size=1920,1080"]}}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"args":["--disable-gpu","--headless","--window-size=1920,1080"]}}}

Failed to connect to localhost port 9515: Connection refused

/var/www/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:331

What is wrong with my configuration and how can I fix it?

0

There are 0 answers