Laravel Dusk 错误:无法连接到本地主机端口 9515:连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42040362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:16:11  来源:igfitidea点击:

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

phplaravelvagrantvirtualboxhomestead

提问by Ortix92

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:

正如标题所说,我已经全新安装了 Laravel 5.4 和最新的 Homestead (1.0.1)。但是,当我运行一个简单的 Dusk 测试用例时,出现以下错误:

Failed to connect to localhost port 9515: Connection refused

无法连接到本地主机端口 9515:连接被拒绝

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

有谁知道如何处理这个问题?我尝试将端口更改为其他内容,例如8888无济于事。

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

编辑:我已经能够更深入地挖掘并发现chromedriver可执行文件实际上并不是可执行文件 ( chmod)。现在我已经修复了当我手动尝试运行它时出现此错误的问题。

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

./chromedriver:加载共享库时出错:libnss3.so:无法打开共享对象文件:没有这样的文件或目录

回答by SimonDepelchin

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

我今天遇到了这个问题,解决方案是在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 &

回答by stephangroen

On Ubuntu Linux 16.04, I got this to work:

在 Ubuntu Linux 16.04 上,我让它工作了:

Install Chromium & dependencies for headless testing

安装 Chromium 和依赖项以进行无头测试

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

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

创建自定义DuskCommand

Which extends the original, with this handlemethod:

使用此handle方法扩展了原始内容:

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.

这将在执行测试之前启动 Xvfb 进行无头测试,并在测试完成后停止该过程。

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

编辑:并确保vendor/laravel/dusk/bin/chromedriver-linux是可执行的。

回答by Utkarsh Vishnoi

It appears your chrome-driver installation is broken.

看来您的 chrome 驱动程序安装已损坏。

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

回答by Ryan

Create a customDuskCommand

创建自定义DuskCommand

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.

感谢https://stackoverflow.com/a/44322930/470749。它已经过时并且不起作用,所以我提供了一个有效的更新答案。



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 foundbecause I'd forgotten that my composer.jsononly 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\DuskCommandprobably is not for you (since the DuskCommanddependency won't exist in production).

我个人不再遵循这种方法。在我部署到生产环境后,我收到了这个错误:E_ERROR: Class 'Laravel\Dusk\Console\DuskCommand' not found因为我忘记了我composer.json只在开发环境中而不是在生产环境中安装了 Dusk。如果您坚持不应将“测试代码”部署到生产中的原则,那么这种编写扩展的自定义类的方法\Laravel\Dusk\Console\DuskCommand可能不适合您(因为DuskCommand生产中不存在依赖项)。

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.

无论如何,我都会把这个答案留在这里,因为它希望对人们来说是一个有价值的警告。如果您认为我应该删除它,请发表评论。顺便说一句,Jonas Staudenmeir 往往有很好的答案,所以这看起来很有趣作为替代

回答by user6437700

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

这应该可以帮助您下​​载最新版本的 chrome 驱动程序并正确解压缩。

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/;

回答by ndberg

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

使用最新的 laravel/homestead box v.6.0.0,它开箱即用

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

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