首页 > 解决方案 > 在 Controller 上实现 Laravel Dusk,并在每个 http 请求上保持它的进程

问题描述

我目前正在使用 laravel 黄昏。

我正在我的控制器上实现黄昏功能。我将不通过 API 远程访问 github。假设我有一个由 3 个功能组成的控制器index:显示登录表单,login:将凭据发布到 github,:logout从 github 注销。

这是我的控制器:

<?php

namespace App\Http\Controllers\Modules;

use App\Http\Controllers\Controller;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;
use Laravel\Dusk\ElementResolver;
use Illuminate\Http\Request;
use Exception;

class MainController extends Controller {

  public function index(Request $request) {
    return view('github.login');
  }

  public function login(Request $request) {
    $username = $request->input('username');
    $password = $request->input('password');

    $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)
      ->setPlatform('Linux');
    $driver       = retry(5, 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://github.com/login')
      ->type('login', $username)
      ->type('password', $password)
      ->click('#login > form > div.auth-form-body.mt-3 > input.btn.btn-primary.btn-block');
    $browser->driver->takeScreenshot(base_path('tests/Browser/screenshots/logged.png'));
    try {
      $browser->assertPresent('#js-pjax-container > div.shelf.intro-shelf.js-notice > div > div > h2');
      return view('github.dashboard');
    } catch (Exception $exception) {
      $browser->quit();
      $process->stop();
      dd($exception);
    }
  }

  public function logOut() {
    $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)
      ->setPlatform('Linux');
    $driver       = retry(5, function () use ($capabilities) {
      return RemoteWebDriver::create('http://localhost:9515', $capabilities, 60000, 60000);
    }, 50);

    $browser = new Browser($driver, new ElementResolver($driver, ''));
    $browser->resize(1920, 1080);
    try {
      $browser->click('#user-links > li:nth-child(3) > details > summary > span')
        ->click('#user-links > li:nth-child(3) > details > details-menu > ul > li:nth-child(10) > form > button')
        ->assertPresent('body > div.application-main > div.py-6.py-sm-8.jumbotron-codelines > div > div > div.col-md-7.text-center.text-md-left > h1');
      $browser->driver->takeScreenshot(base_path('tests/Browser/screenshots/logged_out.png'));
      return view('github.login');
    } catch (Exception $exception) {
      $browser->quit();
      $process->stop();
      dd($exception);
    }
  }

}

这是我的路线文件:

<?php

Route::get('/', 'Modules\MainController@index');
Route::post('login', 'Modules\MainController@login');
Route::get('logout', 'Modules\MainController@logout');

登录功能很好用,但是,每当我尝试通过触发该logout功能注销时,我在 catch 块上捕获的错误似乎是这样的,它显示如下: NoSuchElementException {#189 ▼ -results: array:3 [▶] #message: """ no such element: Unable to locate element: {"method":"css selector","selector":"#user-links > li:nth-child(3) > details > summary > span"}\n (Session info: headless chrome=69.0.3497.81)\n (Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.4.0-135-generic x86_64) """ #code: 0 #file: "/vagrant/proxypay/vendor/facebook/webdriver/lib/Exception/WebDriverException.php" #line: 102 trace: {▶} }

问题是,如何继续这个黄昏过程,似乎每当请求完成时(无论是返回 true 还是 false ),黄昏也会停止并重新创建与旧请求完全无关的新实例。

请帮忙。

此致。

标签: laravelheadless-browserlampplaravel-dusk

解决方案


似乎我必须再次回答我自己的问题......我找到了解决方案。是存储生成的 cookie,并将此 cookie 重新用于下一个操作,依此类推。


推荐阅读