在 Controller 上实现 Laravel Dusk,并在每个 http 请求上保持它的进程

Implementing Laravel Dusk on Controller, and Keeping it's process on each http request

我目前正在与 laravel dusk 合作。

我正在我的控制器上实现 dusk 功能。我将要远程访问 github,而不是通过 API。假设我有一个包含 3 个功能的控制器 index:显示登录表单,login:post github 的凭据,logout:从 [= 注销42=].

这是我的控制器:

<?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: {▶} }

问题是,如何继续这个 dusk 过程,似乎每当请求完成时(return 是真还是假),dusk 也会停止并 re-create 与 old-request 完全没有关系的新实例。

请帮忙。

此致。

看来我必须再次回答我自己的问题...我找到了解决方案。是存储生成的cookies,并在下一个动作中重新使用这个cookie,等等..