skip to Main Content

I have a Laravel with Sail and I want to make automated test with Laravel Dusk. I followed all the instructions in Dusk Documentation and Sail + Dusk Installation, but when I run the default test I receive this error mensage:

TestsBrowserExampleTest::testBasicExample
FacebookWebDriverExceptionWebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"args":["--disable-gpu","--headless","--no-sandbox","--window-size=1920,1080"]},"acceptInsecureCerts":true}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"args":["--disable-gpu","--headless","--no-sandbox","--window-size=1920,1080"]},"acceptInsecureCerts":true}}

Failed to connect to localhost port 4444: Connection refused

/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:333
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:136
/var/www/html/tests/DuskTestCase.php:69
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:218
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/helpers.php:234
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:219
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:97
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:65
/var/www/html/tests/Browser/ExampleTest.php:21

But if execute the request in Insomnia it runs without any problem:

Insomnia request

Here is my docker-compose:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
  laravel.test:
    build:
      context: ./vendor/laravel/sail/runtimes/8.0
      dockerfile: Dockerfile
      args:
        WWWGROUP: '${WWWGROUP}'
    image: sail-8.0/app
    ports:
      - '${APP_PORT:-80}:80'
    environment:
      WWWUSER: '${WWWUSER}'
      LARAVEL_SAIL: 1
    volumes:
      - '.:/var/www/html'
    networks:
      - sail
    depends_on:
      - mysql
      - redis
      - selenium
  selenium:
    image: 'selenium/standalone-chrome'
    volumes:
      - '/dev/shm:/dev/shm'
    networks:
      - sail
    ports:
      - 4444:4444
    #depends_on:
    #  - laravel.test
  mysql:
    image: 'mysql:8.0'
    ports:
      - '${DB_PORT}:3306'
    environment:
      MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
      MYSQL_DATABASE: '${DB_DATABASE}'
      MYSQL_USER: '${DB_USERNAME}'
      MYSQL_PASSWORD: '${DB_PASSWORD}'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    volumes:
      - 'sailmysql:/var/lib/mysql'
    networks:
      - sail
  redis:
    image: 'redis:alpine'
    ports:
      - '${REDIS_PORT}:6379'
    volumes:
      - 'sailredis:/data'
    networks:
      - sail
  # memcached:
  #     image: 'memcached:alpine'
  #     ports:
  #         - '11211:11211'
  #     networks:
  #         - sail
  mailhog:
    image: 'mailhog/mailhog:latest'
    ports:
      - 1025:1025
      - 8025:8025
    networks:
      - sail
networks:
  sail:
    driver: bridge
volumes:
  sailmysql:
    driver: local
  sailredis:
    driver: local

And my DuskTestCase

<?php

namespace Tests;

use FacebookWebDriverChromeChromeOptions;
use FacebookWebDriverRemoteDesiredCapabilities;
use FacebookWebDriverRemoteRemoteWebDriver;
use LaravelDuskTestCase as BaseTestCase;

abstract class DuskTestCase extends BaseTestCase {
    use CreatesApplication;

    /**
     * Prepare for Dusk test execution.
     *
     * @beforeClass
     * @return void
     */
    public static function prepare() {
        if (!static::runningInSail()) {
            static::startChromeDriver();
        }
    }

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

        return RemoteWebDriver::create(
            'http://localhost:4444/wd/hub', DesiredCapabilities::chrome()->setCapability(
            ChromeOptions::CAPABILITY, $options
        )->setCapability('acceptInsecureCerts', TRUE)
        );
    }
}

I really don’t know what I’m doing wrong. Sorry for my English, I’m brazillian.

3

Answers


  1. Chosen as BEST ANSWER

    After many days I finally managed to solve my problem. Reading the Docker documentation I found that I can use the image name as a URL, so I switched to http://selenium:4444 and the tests worked perfectly!

    Here are my new code:

    <?php
    
    namespace Tests;
    
    use FacebookWebDriverChromeChromeOptions;
    use FacebookWebDriverRemoteDesiredCapabilities;
    use FacebookWebDriverRemoteRemoteWebDriver;
    use LaravelDuskTestCase as BaseTestCase;
    
    abstract class DuskTestCase extends BaseTestCase {
        use CreatesApplication;
    
        /**
         * Prepare for Dusk test execution.
         *
         * @beforeClass
         * @return void
         */
        public static function prepare() {
            if (!static::runningInSail()) {
                static::startChromeDriver();
            }
        }
    
        /**
         * Create the RemoteWebDriver instance.
         *
         * @return FacebookWebDriverRemoteRemoteWebDriver
         */
        protected function driver() {
            $options = (new ChromeOptions)->addArguments([
                '--disable-gpu',
                '--headless',
                '--no-sandbox',
                '--window-size=1920,1080',
            ]);
    
            return RemoteWebDriver::create(
                'http://selenium:4444/wd/hub', DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )->setCapability('acceptInsecureCerts', TRUE)
            );
        }
    }
    

    I hope I can help someone with the same problem!


  2. I’ll tell you what worked for me:

    1 – sail composer update

    2 – docker system prune -a (to delete)

    3- APP_URL=http://localhost:80 in the .env file

    4- sail up

    I’m not sure why but after I did it it worked

    Login or Signup to reply.
  3. The WebDriver used by Dusk is looking for the selenium docker image on port 4444.

    be sure to add to the .env file:

    DUSK_DRIVER_URL='http://selenium:4444'
    

    and an APP_URL that targets a local host on port 80:

    APP_URL="http://laravel.test:80"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search