Laravel DuskをDocker内で起動するまで
とりあえずDockerfile書く。
Dockerfile
FROM laradock/php-fpm:1.4-70
RUN apt-get -y update && apt-get -y install chromium libgconf-2-4
諸々インストールしたら、 --no-sandbox
フラグを追加。 Dockerだとroot
で起動することになるため必要。
tests/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',
'--no-sandbox', // <-- これ追加
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
}
経緯
こんなエラーが出た。
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"A
NY","chromeOptions":{"binary":"","args":["--disable-gpu","--headless","--no-sandbox"]}}}
Failed to connect to localhost port 9515: Connection refused
Dockerfileでのlibgconf
が盲点。
ldd ./vendor/laravel/dusk/bin/chromedriver-linux
で気付いた。