Got unknown error: net::ERR_CONNECTION_REFUSED with laravel dusk in docker

Got unknown error: net::ERR_CONNECTION_REFUSED with laravel dusk in docker

我正在尝试在 docker 环境中使用 Laravel Dusk 进行浏览器测试。 但是当我 运行 命令 php artisan dusk 进行测试时(在我的 php 容器中)它显示我所有测试用例的错误:

1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\UnknownErrorException: unknown error: net::ERR_CONNECTION_REFUSED
  (Session info: headless chrome=96.0.4664.45)

/var/www/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:139
/var/www/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:372
/var/www/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:585
/var/www/vendor/php-webdriver/webdriver/lib/Remote/RemoteExecuteMethod.php:27
/var/www/vendor/php-webdriver/webdriver/lib/WebDriverNavigation.php:41
/var/www/vendor/laravel/dusk/src/Browser.php:153
/var/www/tests/Browser/ExampleTest.php:19
/var/www/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:69
/var/www/tests/Browser/ExampleTest.php:21

这是我的配置:

我通过这个 introduction

添加 selenium 容器到 docker-compose 文件
version: '3.7'

networks:
    laravel:

services:
    php:
        build:
            context: .
            dockerfile: dockers/php/Dockerfile
        container_name: php
        extra_hosts: 
            - "host.docker.internal:host-gateway"
        volumes:
            - ./:/var/www/
        ports:
            - "9000:9000"
        networks:
            - laravel
        depends_on:
            - selenium
    selenium:
        container_name: selenium
        image: 'selenium/standalone-chrome'
        volumes:
            - './selenium:/selenium'
        networks:
            - laravel

我已按照 laravel 文档页面 here 中的介绍将 APP_URL 配置更改为 http://selenium:4444/wd/hub

    public static function prepare()
    {
        if (! static::runningInSail()) {
            static::startChromeDriver();
        }
    }

    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments(collect([
            '--window-size=1920,1080',
        ])->unless($this->hasHeadlessDisabled(), function ($items) {
            return $items->merge([
                '--disable-gpu',
                '--headless'
            ]);
        })->all());

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

然后当我 运行 在 php 容器中测试时,它显示上面的错误。

更新

我检查了我的 selenium 日志,每当我 运行 命令 php artisan dusk

时它都会显示此错误
Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 58188

Only local connections are allowed.

Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.

ChromeDriver was started successfully.

[1639062447.255][SEVERE]: bind() failed: Cannot assign requested address (99)

我想知道这个错误是来自我的配置还是我的实施步骤。所以,这是我用来实施测试的步骤的详细信息:

docker-compose build
docker-compose up -d
docker-compose exe php bash
// php container
php artisan config:clear
php artisan dusk

希望对解决问题有所帮助

您的 selenium 实例缺少 shm_size,它可能不起作用。这是示例 docker-compose 文件:https://github.com/SeleniumHQ/docker-selenium/blob/trunk/docker-compose-v3.yml

注意应该>=2gb。

刚刚解决了这个问题。我在 .env.dusk.local 中配置错误 APP_URL。 它应该是 APP_URL=http://nginxnginx 是包含 nginx 服务器的容器)