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

时间:2019-05-25 17:12:02

标签: php laravel testing phpunit laravel-dusk

我想创建一个要在Controller中使用的测试,所以我写:

<?php

namespace App\Http\Controllers\Modules;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Laravel\Dusk\ElementResolver;
use Exception;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;

class TestController extends Controller {



  public function test() {

    $process = (new ChromeProcess)->toProcess();
    if ($process->isStarted()) {
      $process->stop();
    }
    $process->start();

    $options      = (new ChromeOptions)->addArguments(['--disable-gpu', '--headless', '--no-sandbox']);
    $capabilities = DesiredCapabilities::chrome()
      ->setCapability(ChromeOptions::CAPABILITY, $options);

          $driver = retry(1, function () use ($capabilities) {
      return RemoteWebDriver::create('http://localhost:9515', $capabilities, 60000, 60000);
    }, 50);

    $browser = new Browser($driver, new ElementResolver($driver, ''));
    $browser->resize(1920, 1080);
    $browser->visit('https://example.com/login')->click('#.btn > form > div.auth-form-body.mt-3 > input.btn.btn-primary.btn-block');
    $browser->driver->takeScreenshot(base_path('tests/Browser/screenshots/logged.png'));

  }


}

当我使用localhost:8000 / test运行此脚本时,收到以下消息:

  

Facebook \ WebDriver \异常\ WebDriverCurlException卷曲错误   使用参数将HTTP POST抛出到/ session:   {“ desiredCapabilities”:{“ browserName”:“ chrome”,“ platform”:“ ANY”,“ chromeOptions”:{“ binary”:“”,“ args”:[“-disable-gpu”,“-无头“,”-无沙箱“]}}}   无法连接到本地主机端口9515:连接被拒绝

enter image description here

我该如何解决这个问题?

当前,我在Win10上使用WAMP服务器进行本地测试,但随后我将在Linux Ubuntu 18上移动代码。

1 个答案:

答案 0 :(得分:1)

我无法完全解释它,但这在Windows上对我有用:

$process = (new ChromeProcess)->toProcess();
if ($process->isStarted()) {
  $process->stop();
}

$process->start(null, [
    'SystemRoot' => 'C:\\WINDOWS',
    'TEMP' => 'C:\Users\<User>\AppData\Local\Temp',
]);

[...]

用您的用户目录名称替换<User>