3

我正在尝试使用与 Laravel Sail 集成的 Laravel Dusk,如下所述:https ://laravel.com/docs/8.x/sail#laravel-dusk

在进行这项工作时,我面临一些问题:

  • 如果使用 .env.dusk 或 .env.dusk.local 文件并使用 'sail Huang' 运行测试,我会收到 CONNECTION REFUSED ERROR(更多详情见下文)

  • 例如,如果使用 .env.testing 来设置 ENV 变量并运行 'sail Huang --env=testing',则 CONNECTION REFUSED ERROR 消失,但应用程序继续使用默认的 .env 文件,而黄昏测试使用 . env.testing,导致很多错误。

码头工人-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

数据库.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
            )
        );
    }
}

当我运行'sail Huang' 时,出现以下错误:

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
4

2 回答 2

1

真正的问题似乎是容器的使用和配置必须与之匹配。当前的解决方案假定运行 Laravel 的容器的名称仍设置为laravel.test

您的.env.dusk.local文件需要包含一个名为DUSK_DRIVER_URL的附加环境变量,其值为http://selenium:4444. 如果您检查 selenium 容器,那么它将提到两个端口,两个端口都未绑定:44445900. 似乎第一个是运行RemoteWebDriver.

另一个变量是APP_URL应该设置为http://laravel.test,至少在默认服务容器名称没有更改的情况下。

这两个更改应该允许 Dusk 测试按预期运行。

我从讨论线程的已接受评论中找到了难题的第二部分。

于 2021-10-26T15:30:50.370 回答
-1

尝试使用

APP_URL=http://laravel.test

由于测试是在 selenium docker 中运行的,因此这会引用您的 laravel docker 容器。

于 2021-03-24T23:31:17.910 回答