我正在运行浏览器的 Laravel Dusk 示例测试,但是当我执行 php artisan dusk 时出现错误
使用: * 乌班图18 * 拉拉维尔 5.8 *黄昏5.1 * Chrome驱动程序74 * 阿帕奇2
这是我的 DuskTestCase.php:
<?php
namespace Tests;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
{
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',
'--disable-dev-shm-usage',
'--no-sandbox'
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
// 'http://localhost:9515', DesiredCapabilities::phantomjs()
// 'http://localhost:9515', DesiredCapabilities::chrome()
);
}
}
这是错误:
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\UnknownServerException: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /snap/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.18.0-20-generic x86_64)
就我而言,我发现我需要将
--no-sandbox
添加到 tests/DuskTestCase.php
,因为我在 lxd 容器上以 root 身份运行。
我通过运行:
vendor/laravel/dusk/bin/chromedriver-linux --log-level=ALL
并在另一个终端中运行php artisan dusk
发现了错误。它将显示日志,我能够从那里推断出问题。
这与问题没有直接关系,但由于我没有轻易找到对 Laravel 7+ 和 Homestead 10.0.0 上发生的相同错误的任何修复,我将介绍经过数小时的研究和尝试后提出的解决方案希望它能帮助其他遇到这个问题的人。
Homestead 似乎不再支持 Dusk 开箱即用。要安装使用 Chromium 的先决条件,您必须将 Webdriver 功能添加到您的
homestead.yaml
:
features:
- webdriver: true
然后通过运行
homestead halt && homestead up --provision
重新配置。
之后,通过在
driverChrome()
中的 tests/DuskTestCase.php
方法中添加附加参数,确保 Chromium 以无头模式启动:
protected function driverChrome()
{
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options)
);
}
大多数人会建议也使用
--no-sandbox
和 --disable-dev-shm-usage
标志,但是在使用安装 google-chrome-stable
而不是 chromium-browser
的 Webdriver 功能正确配置 homestead 后,我不需要这些来正确运行。
您可以尝试从以下位置手动下载 ChromeDriver 官方 ChromeDriver 下载页面。
wget https://chromedriver.storage.googleapis.com/87.0.4280.88/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
./chromedriver
我遇到了同样的问题,并根据上面@Martins Balodis 的评论解决了它。我猜原因与我的双显示器设置有关。 我没能通过在 Dusk 的环境文件中添加条目来改变这一点。相反,我在测试文件的顶部添加了这一行。
<?php
namespace Tests\Browser;
$_ENV['DISPLAY'] = getenv('DISPLAY');
我测试了一些有关向 chrome 驱动程序添加参数的建议,但这是唯一有效的修改,因为我想使用
--browse
运行测试。