I’m trying to implement browser testing with Laravel Dusk in docker environment.
But when I run command php artisan dusk
to testing (in my php container) it display this error for all my tests case:
1) TestsBrowserExampleTest::testBasicExample
FacebookWebDriverExceptionUnknownErrorException: unknown error: net::ERR_CONNECTION_REFUSED
(Session info: headless chrome=96.0.4664.45)
/var/www/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:139
/var/www/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:372
/var/www/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:585
/var/www/vendor/php-webdriver/webdriver/lib/Remote/RemoteExecuteMethod.php:27
/var/www/vendor/php-webdriver/webdriver/lib/WebDriverNavigation.php:41
/var/www/vendor/laravel/dusk/src/Browser.php:153
/var/www/tests/Browser/ExampleTest.php:19
/var/www/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:69
/var/www/tests/Browser/ExampleTest.php:21
Here is my configuration:
I add selenium
container to docker-compose file by this introduction
docker-compose.yaml
version: '3.7'
networks:
laravel:
services:
php:
build:
context: .
dockerfile: dockers/php/Dockerfile
container_name: php
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- ./:/var/www/
ports:
- "9000:9000"
networks:
- laravel
depends_on:
- selenium
selenium:
container_name: selenium
image: 'selenium/standalone-chrome'
volumes:
- './selenium:/selenium'
networks:
- laravel
I have follow introduction in laravel documentation page here to change APP_URL
config to http://selenium:4444/wd/hub
DuskTestCase.php
public static function prepare()
{
if (! static::runningInSail()) {
static::startChromeDriver();
}
}
protected function driver()
{
$options = (new ChromeOptions)->addArguments(collect([
'--window-size=1920,1080',
])->unless($this->hasHeadlessDisabled(), function ($items) {
return $items->merge([
'--disable-gpu',
'--headless'
]);
})->all());
return RemoteWebDriver::create(
'http://selenium:4444/wd/hub', // change here
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
Then when I run testing in php
container it show error above.
UPDATE
I’ve checked my selenium
logs and it shows this error whenever I run command php artisan dusk
Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 58188
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1639062447.255][SEVERE]: bind() failed: Cannot assign requested address (99)
I wonder if this erro come from my configuration or my implement steps. So, this is detail of steps I used to implement testing:
docker-compose build
docker-compose up -d
docker-compose exe php bash
// php container
php artisan config:clear
php artisan dusk
Hope it help to figure out solution
2
Answers
Just fixed this issues. I've config wrong
APP_URL
in.env.dusk.local
. It should have beenAPP_URL=http://nginx
(withnginx
is container containing nginx server)You are missing shm_size on your selenium instance, it probably doesn’t work. Here’s the example docker-compose file: https://github.com/SeleniumHQ/docker-selenium/blob/trunk/docker-compose-v3.yml
Note that it should be >=2gb.