简体   繁体   English

Laravel Dusk使用测试结构在服务中无法连接

[英]Laravel Dusk use test structure in service can't connect

This is a weird one. 这很奇怪。 So I have the code below that I have transplanted into a service in order to run it from a controller and to be able to pass in some information. 所以我有下面的代码,我已经移植到服务中,以便从控制器运行它,并能够传递一些信息。

$this->browse(function (Browser $browser) {
    $browser->visit('https://urlforsite.co.uk/find-an-engineer/')
        ->type("EngineerId", "2231321")
        ->click('#checkEngineer');

    if ($browser->assertSee("Engineer cannot be found")) {
        dd("hello");
    }

    $text = $browser->text('.engineer-search-results-container .search-result .col-md-8 .row .col-xs-10 h3');
        dd($text);
    });

Expected outcome would be one of the DD's 预期的结果将是DD的一个

dd("hello); or dd($text); dd("hello);dd($text);

Actual outcome: 实际结果:

Failed to connect to localhost port 9515: Connection refused

If I run it in an actual dusk test and run php artisan dusk it works correctly. 如果我在实际的dusk测试中运行它并运行php artisan dusk它可以正常工作。 Is there something that command runs first so it can get to the outside? 是否有一些命令首先运行,以便它可以到达外面? Can this even be done? 甚至可以这样做吗?

As mentioned by Arun Code in the link: https://github.com/laravel/dusk/issues/356 there is response that allows you to open a browser from a service/controller like so: 正如Arun Code在链接中提到的: https//github.com/laravel/dusk/issues/356有响应允许您从服务/控制器打开浏览器,如下所示:

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

$process = (new ChromeProcess)->toProcess();
$process->start();
$options = (new ChromeOptions)->addArguments(['--disable-gpu', '--headless']);
$capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = retry(5, function () use($capabilities) {
    return RemoteWebDriver::create('http://localhost:9515', $capabilities);
}, 50);
$browser = new Browser($driver);
$browser->visit('https://www.google.com');
$browser->quit();
$process->stop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM