Laravel Dusk error: Failed to connect to localhost port 9515: Connection refused
Asked Answered
S

9

47

As the title says, I've go a clean install of Laravel 5.4 and the latest Homestead (1.0.1). However, when I run a simple Dusk test case I get the following error:

Failed to connect to localhost port 9515: Connection refused

Anyone know how to deal with this? I tried changing the port to something else such as 8888 to no avail.

EDIT: I've been able to dig a little deeper and found out that the chromedriver executable was not actually executable (chmod). Now that I've fixed that I get this error when I manually try to run it.

./chromedriver: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory

Sapienza answered 4/2, 2017 at 12:28 Comment(7)
For me it was enough to chmod the executable in vendor/laravel/dusk/bin/chromedriver-linux. There is also a macOS chromedriver-linux and windows chromedriver-win.exe version.Attis
here is a discussion on the issue. github.com/laravel/dusk/issues/10Turpeth
Are you run command php artisan dusk via SSH in your server or Homestead? you need chrome browser to run browser test, try to run command in host OS instead.Saltarello
some reference run dusk in homestead jesusamieiro.com/using-laravel-dusk-with-vagrant-homesteadSaltarello
I ran into the same issue while running php artisan serve, turns out that you cannot have the php artisan serve running when you start duskOke
sudo apt-get install libnss3-devSlr
Can you please try to isolate the problem, i'm not a laravel developer but it looks like its not a php problem, you can try to connect to that port using telnet and see if its a laravel problem or the service you are trying to reach, you can also use postman(getpostman.com) to test. Hope this helpsSkid
U
15

For those looking for a solution on Mac, I've just had to restart Chrome. Yes, Chrome, the browser. It seems it'd a pending update (it said that in the upper right corner).

Restarting it, and later chromedriver, make everything went fine

Unrestraint answered 17/4, 2021 at 21:43 Comment(1)
It required a full reboot, but that worked.Idempotent
D
6

I had this issue today and the solution is on Laracasts.

Here's a copy.

# makes sure all your repos are up to date
sudo apt-get update

# chrome dependencies I think
sudo apt-get -y install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4

# chromium is what I had success with on Codeship, so seemed a good option
sudo apt-get install chromium-browser

# XVFB for headless applications
sudo apt-get -y install xvfb gtk2-engines-pixbuf

# fonts for the browser
sudo apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base         xfonts-scalable

# support for screenshot capturing
sudo apt-get -y install imagemagick x11-apps

# Once all this has run through, you need to fire up xvfb on your homestead box. If you’re planning to # do this on a regular basis, you’ll want to get this setup on boot, but for the sake of testing things out:
Xvfb -ac :0 -screen 0 1280x1024x16 &
Donets answered 20/1, 2019 at 18:35 Comment(0)
F
3

On Ubuntu Linux 16.04, I got this to work:

Install Chromium & dependencies for headless testing

sudo apt-get -y install chromium-browser xvfb gtk2-engines-pixbuf xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable imagemagick x11-apps

Create a customDuskCommand

Which extends the original, with this handle method:

public function handle()
{
    $xvfb = (new ProcessBuilder())
        ->setTimeout(null)
        ->setPrefix('/usr/bin/Xvfb')
        ->setArguments(['-ac',  ':0', '-screen', '0', '1280x1024x16'])
        ->getProcess();

    $xvfb->start();

    try {
        parent::handle();
    } finally {
        $xvfb->stop();
    }

    return;
}

This will start Xvfb for headless testing before executing the tests and stop the process after testing completes.

Edit: And make sure vendor/laravel/dusk/bin/chromedriver-linux is executable.

Frederick answered 2/6, 2017 at 7:12 Comment(0)
C
2

Failed to connect to localhost port 9515 after 0 ms: Connection refused

Solution:

php artisan dusk:install
php artisan dusk:chrome-driver 65
Chirrupy answered 12/1, 2022 at 14:4 Comment(0)
S
1

It appears your chrome-driver installation is broken.

You can try to install it from scratch

CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`

wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
rm ~/chromedriver_linux64.zip
sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod 0755 /usr/local/bin/chromedriver
Scramble answered 20/8, 2018 at 11:52 Comment(0)
H
1

This should help you download the latest version of chrome driver and unpack it properly.

LATEST_VERSION=$(curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE) && wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.co /$LATEST_VERSION/chromedriver_linux64.zip && sudo unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/;
Hellfire answered 30/10, 2018 at 23:56 Comment(0)
M
1

Create a customDuskCommand

namespace App\Console\Commands;

use Symfony\Component\Process\Process;

class DuskCommand extends \Laravel\Dusk\Console\DuskCommand {

    public function handle() {
        $xvfb = (new Process(['/usr/bin/Xvfb', '-ac', ':0', '-screen', '0', '1280x1024x16']))
                ->setTimeout(null);

        $xvfb->start();

        try {
            parent::handle();
        } finally {
            $xvfb->stop();
        }

        return;
    }

}

Thanks to https://mcmap.net/q/362613/-laravel-dusk-error-failed-to-connect-to-localhost-port-9515-connection-refused. It was outdated and didn't work, so I'm providing an updated answer that works.


UPDATE:

I personally don't follow this approach anymore. After I deployed to production, I got this error: E_ERROR: Class 'Laravel\Dusk\Console\DuskCommand' not found because I'd forgotten that my composer.json only installed Dusk in the dev environment rather than also in production. If you adhere to the principle that "test code" shouldn't be deployed to production, then this approach of writing a custom class that extends \Laravel\Dusk\Console\DuskCommand probably is not for you (since the DuskCommand dependency won't exist in production).

I will leave this answer here anyway since it's hopefully a valuable warning to people. Please comment if you think I should delete it instead. By the way, Jonas Staudenmeir tends to have great answers, so this looks interesting as an alternative.

Multivalent answered 16/3, 2019 at 21:9 Comment(0)
S
1

If your tests were previously working and now you find one or two randomly getting this error (and not necessarily always the same one or two), this is likely down to Chrome automatically updating itself and making Dusk's Chrome binaries out of date. In this case running the following seems to fix the issue and make all tests go back to consistently passing:

php artisan dusk:install
php artisan dusk:chrome-driver
Succory answered 12/2 at 2:29 Comment(0)
P
0

With the latest laravel/homestead box v.6.0.0 it's working out of the box

https://app.vagrantup.com/laravel/boxes/homestead

Palatal answered 13/5, 2018 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.