简体   繁体   中英

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("hello); or 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. 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:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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