C0lliNN
C0lliNN

Reputation: 71

Connection Refused while Runing Laravel Dusk with Laravel Sail

I'm trying to use Laravel Dusk integrated with Laravel Sail as described in: https://laravel.com/docs/8.x/sail#laravel-dusk

I'm facing some problems when it comes to make this work:

docker-compose.yml

# 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
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}: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
        healthcheck:
            test: ['CMD', 'mysqladmin', 'ping']
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
            test: ['CMD', 'redis-cli', 'ping']
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local

.env.dusk

APP_NAME="Plataforma Brasileira SE"
APP_URL="http://plataformabrasileira-se.test"
APP_ENV=testing
APP_KEY=base64:bc0IGvpHb/ymQD7vUTJa60qhG7T2dtozC6urijChcbo=
DB_CONNECTION=dusk
MAIL_MAILER=array
QUEUE_CONNECTION=sync
SESSION_DRIVER=array
TELESCOPE_ENABLED=false

database.php

...
'dusk' => [
            'driver' => 'sqlite',
            'database' => database_path('database.sqlite'),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
 ],
 ...

DuskTestCase.php

<?php

namespace Tests;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\TestCase 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 \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu',
            '--headless',
            '--window-size=1920,1080',
        ]);

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

When I run 'sail dusk', I get the following error:

1) Tests\Browser\LoginTest::test_user_cannot_login_with_invalid_data
Facebook\WebDriver\Exception\UnknownErrorException: unknown error: net::ERR_CONNECTION_REFUSED
  (Session info: headless chrome=88.0.4324.96)

/var/www/html/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:139
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:371
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:612
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/RemoteExecuteMethod.php:27
/var/www/html/vendor/php-webdriver/webdriver/lib/WebDriverNavigation.php:41
/var/www/html/vendor/laravel/dusk/src/Browser.php:153
/var/www/html/tests/Browser/LoginTest.php:18
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:68
/var/www/html/tests/Browser/LoginTest.php:28

Upvotes: 7

Views: 3949

Answers (2)

Pjotr
Pjotr

Reputation: 531

The real issue seems to be usage of containers and configuration has to match it. Current solution assumes that the name of the container running Laravel is still set to laravel.test.

Your .env.dusk.local file would need to contain an additional environment variable named DUSK_DRIVER_URL with value of http://selenium:4444. If you inspect the selenium container, then it will have two ports mentioned, with both of those not bound: 4444 and 5900. It seems that the first one is the one running the RemoteWebDriver.

The other variable is APP_URL that should be set to http://laravel.test, at least if the default service container name has not been changed.

Those two changes should allow for the Dusk tests to be running as they were supposed to.

I've found the second part of the puzzle from an accepted comment for this discussion thread.

Upvotes: 4

Marvin
Marvin

Reputation: 67

Try using

APP_URL=http://laravel.test

since the test is run in selenium docker, this references back to your laravel docker container.

Upvotes: 4

Related Questions