我目前正在处理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');
login函数很好用,但是,每当我尝试通过触发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)时,黄昏也会停止并重新创建与旧请求无关的新实例完全没有。
请帮助。
最诚挚的问候。
答案 0 :(得分:0)
似乎我必须再次回答自己的问题...我找到了解决方案。是存储生成的cookie,然后将该cookie重复用于下一步操作,依此类推。