dpbv85276 2019-06-04 11:32
浏览 72
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • douyin2962 2019-06-04 13:25
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?