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

后端 未结 6 961
粉色の甜心
粉色の甜心 2021-02-05 01:16

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:

6条回答
  •  借酒劲吻你
    2021-02-05 01:30

    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://stackoverflow.com/a/44322930/470749. 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.

提交回复
热议问题