我使用 Sail 启动了一个新的 Laravel 应用程序,以便在 Docker 中运行本地开发,因此它也已经使用了
selenium/standalone-chrome
容器启动。
按照文档安装了 Dusk,但从
php artisan dusk
容器运行 sail-8.3/app
出现以下错误:
FAILED Tests\Browser\HomePageTest > example UnknownErrorException
unknown error: net::ERR_CONNECTION_REFUSED
(Session info: chrome=126.0.6478.114)
at vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:146
142▕ throw new UnexpectedAlertOpenException($message, $results);
143▕ case 'unknown command':
144▕ throw new UnknownCommandException($message, $results);
145▕ case 'unknown error':
➜ 146▕ throw new UnknownErrorException($message, $results);
147▕ case 'unknown method':
148▕ throw new UnknownMethodException($message, $results);
149▕ case 'unsupported operation':
150▕ throw new UnsupportedOperationException($message, $results);
+5 vendor frames
6 tests/Browser/HomePageTest.php:18
7 tests/Browser/HomePageTest.php:17
按照其他对同一问题的回答,我尝试从应用程序容器内运行
php artisan serve
,然后运行php artisan dusk
,但得到了相同的结果。
abstract class DuskTestCase extends BaseTestCase
{
/**
* Prepare for Dusk test execution.
*/
#[BeforeClass]
public static function prepare(): void
{
if (! static::runningInSail()) {
static::startChromeDriver();
}
}
/**
* Create the RemoteWebDriver instance.
*/
protected function driver(): RemoteWebDriver
{
$options = (new ChromeOptions)->addArguments(collect([
$this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
])->unless($this->hasHeadlessDisabled(), function (Collection $items) {
return $items->merge([
'--disable-gpu',
'--headless=new',
]);
})->all());
return RemoteWebDriver::create(
$_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
}
class HomePageTest extends DuskTestCase
{
use DatabaseMigrations;
/**
* A Dusk test example.
*/
public function testExample(): void
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Login');
});
}
}
已添加
DUSK_DRIVER_URL=http://selenium:4444
07:43:16.595 INFO [LocalSessionMap.lambda$new$0] - Deleted session from local Session Map, Id: d6234a0ba83b4ac0de1e5c6b70fb616b
07:43:16.595 INFO [GridModel.release] - Releasing slot for session id d6234a0ba83b4ac0de1e5c6b70fb616b
07:43:16.596 INFO [SessionSlot.stop] - Stopping session d6234a0ba83b4ac0de1e5c6b70fb616b
07:43:17.150 INFO [LocalDistributor.newSession] - Session request received by the Distributor:
[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--window-size=1920,1080, --disable-gpu, --headless=new]}}]
07:43:17.312 INFO [LocalNode.newSession] - Session created by the Node. Id: 8b9c90b80c4e711d8781e204ab55331f, Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 126.0.6478.114, chrome: {chromedriverVersion: 126.0.6478.63 (df799988fdc9..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:45535}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:bidiEnabled: false, se:cdp: ws://172.22.0.6:4444/sessio..., se:cdpVersion: 126.0.6478.114, se:vnc: ws://172.22.0.6:4444/sessio..., se:vncEnabled: true, se:vncLocalAddress: ws://172.22.0.6:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
07:43:17.315 INFO [LocalDistributor.newSession] - Session created by the Distributor. Id: 8b9c90b80c4e711d8781e204ab55331f
Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 126.0.6478.114, chrome: {chromedriverVersion: 126.0.6478.63 (df799988fdc9..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:45535}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:bidiEnabled: false, se:cdp: ws://172.22.0.6:4444/sessio..., se:cdpVersion: 126.0.6478.114, se:vnc: ws://172.22.0.6:4444/sessio..., se:vncEnabled: true, se:vncLocalAddress: ws://172.22.0.6:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
07:43:17.441 WARN [SeleniumSpanExporter$1.lambda$export$3] - {"traceId": "a9a833dc373529e36ebc4b22b4b6b57d","eventTime": 1721893397440430947,"eventName": "HTTP request execution complete","attributes": {"http.flavor": 1,"http.handler_class": "org.openqa.selenium.remote.http.Route$PredicatedRoute","http.host": "selenium:4444","http.method": "POST","http.request_content_length": "35","http.scheme": "HTTP","http.status_code": 500,"http.target": "\u002fsession\u002f8b9c90b80c4e711d8781e204ab55331f\u002furl"}}
07:43:17.869 INFO [LocalSessionMap.lambda$new$0] - Deleted session from local Session Map, Id: 8b9c90b80c4e711d8781e204ab55331f
07:43:17.870 INFO [GridModel.release] - Releasing slot for session id 8b9c90b80c4e711d8781e204ab55331f
07:43:17.872 INFO [SessionSlot.stop] - Stopping session 8b9c90b80c4e711d8781e204ab55331f
我看到这里有一个警告,但无法理解。
好的,发现问题了,我错过了 Dusk 文档中与使用 Sail 运行它相关的步骤:https://laravel.com/docs/11.x/sail#laravel-dusk
我只需要使用 Sail 而不是普通的 Docker 命令来运行 Docker 容器,所以
# wrong
docker compose up -d
# correct
./vendor/bin/sail up -d
然后也通过 Sail 运行 Dusk 测试:
./vendor/bin/sail dusk