Could not resolve host: selenium

3.4k Views Asked by At

I would like test my laravel projet with Dusk.

But when I run php artisan dusk I have this:

Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"binary":"","args":["--disable-gpu","--headle
ss","--window-size=1920,1080"]},"acceptInsecureCerts":true}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--disable-gpu","--headless","--window-size=1920,1080"]},"acceptInsecu
reCerts":true}}

Could not resolve host: selenium

I've configured dusk to use my selenium container.

My config:

<?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()
    {
//        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(
            'http://selenium:4444/wd/hub', DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
                )
            ->setCapability('acceptInsecureCerts', true), 60*1000, 60*1000
        );
    }
}

Selenium is in a docker container. Selenium's configuration :

selenium:
    image: selenium/standalone-chrome:3.11.0-antimony
    networks:
         - project

I know it's because my project do not arrive to connect to my container but I don't know how I can resolve this problem.

If someone can help me it will be nice ... Thanks a lot !

2

There are 2 best solutions below

1
On

http://selenium:4444/wd/hub

Is your hub exposed with this domain address ?? Selenium:4444

Change it to IP address than selenium

Remotedrver tries to connect to selenium:4444 . It's not available if it's not dns registered , change it to 127.0.0.1 or localhost if your running locally or add your selenium hub ip if running remotely

0
On

Run docker ps in your cmd. You should get the result like this:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
7b308d13a343        mongo               "docker-entrypoint.s…"   2 weeks ago         Up 13 seconds       0.0.0.0:27017->27017/tcp   mongo_mongo_1

But instead of mongo (in my case) you will have selenium.

Take the port (from PORTS column of console output) that is just before /tcp (in my case both container and host ports are identical, but in your case they might be different, so you need the one between -> and /tcp). Then in your configuration change http://selenium:4444/wd/hub to http://localhost:YOUR_PORT/wd/hub where YOUR_PORT is the port that you taken from just before /tcp

P.S. - This assumes that you are running your php at the same host where the container is running. Otherwise use your host machine IP address instead of localhost and make sure that exposed port is not blocked by any firewall policy.

P.P.S - Why selenium host is not working. Host name selenium is resolved to a proper container IP address only for other containers which are running within the same network as your selenium container is. For example it can be the network project (according to your configuration). Any process outside docker container outside the mentioned network will have to use host IP address and host port (not container port).