I am currently working with laravel dusk.
i am implementing dusk functionality on my controller. I am about to remote into github not via API. Let's say i have a controller that consist of 3 functions index
: showing login form ,login
:post credential to github, logout
: logout from github.
this is my controller :
<?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);
}
}
}
here is my route file :
<?php
Route::get('/', 'Modules\MainController@index');
Route::post('login', 'Modules\MainController@login');
Route::get('logout', 'Modules\MainController@logout');
the login function works great, but, whenever i try to logout by firing the logout
function, it appears error that i caught on my catch block, and it shows like this :
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: {▶} } the question is, how to continue this dusk process, it seems like whenever the request is done ( whether is return true or false ), dusk also stop and re-create new instance which is have no relation with the old-request at all.
please help.
best regards.
seems like i have to answer my own question again... i found the solution. Is store the generated cookies, and re-use this cookie for the next action, and so on..
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community