我正在尝试使用与 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