Laravel Sail + Dusk + Selenium: Connection refused

Rico Crivelli

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:

Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: 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 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',
            '--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.

Rico Crivelli

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 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',
            '--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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel Dusk - Failed to connect to localhost port 9515: Connection refused

Laravel Dusk: Facebook\WebDriver\Exception\UnknownErrorException: unknown error: net::ERR_CONNECTION_REFUSED

How to fix "Failed to connect to localhost port 9515: Connection refused" Laravel Dusk

Laravel dusk Your connection is not private

Laravel Connection Refused Error

Connection refused: Laravel

Python Selenium Geckodriver Connection refused

Connection refused on Docker Mysql with Laravel

Laravel - composer update: Connection refused

Connection to DB with socket refused in Laravel

Connection refused Docker, Laravel & MariaDB

Selenium remote webdriver error (Connection refused)

StaticLiveServerTestCase with Selenium: ERR_CONNECTION_REFUSED

Connection refused when using Scrapy with Selenium

Laravel dusk + docker-compose + selenium + firefox: Undefined index ELEMENT

Deploying Laravel to Elastic Beanstalk - DB Connection Refused

Laravel Homestead , Socket.io Connection Refused

Laravel Valet - Server Connection Refused or slow

Laravel Valet Share URL Leads to connection refused

Connection refused [tcp://127.0.0.1:6379] Laravel 5.6

Connection refused laravel SQL: select count(*) as aggregate

Selenium's htmlunit webdriver resulting in Error 10061, connection is actively refused

Selenium/TestBench failure: net::ERR_CONNECTION_REFUSED

selenium:socket.error: [Errno 61] Connection refused

Selenium - socket.error: [Errno 111] Connection refused

Getting Connection refused error when run robotframework testscripts in Selenium Grid

connection refused (os error 111) using python selenium & geckodriver

Handle python selenium webdriver Connection Refused error. (Chrome)

access logs on laravel sail