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

时间:2019-06-04 11:32:14

标签: php laravel laravel-dusk

这很奇怪。因此,我将下面的代码移植到服务中,以便从控制器运行它并能够传递一些信息。

$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);
    });

预期结果将是DD的结果之一

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

实际结果:

Failed to connect to localhost port 9515: Connection refused

如果我在实际的dusk测试中运行并运行php artisan dusk,它将正常运行。有什么命令会先运行以便它可以到达外部吗?甚至可以做到吗?

1 个答案:

答案 0 :(得分:0)

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