Last active
March 25, 2022 00:12
-
-
Save peterfox/5114bf735e0c7062e4d64f49f67bc497 to your computer and use it in GitHub Desktop.
Rough example of how to use Laravel Dusk directly in production code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'screenshot_path' => storage_path('app/automator'), | |
'chrome' => [ | |
'arguments' => [ | |
// '--headless', | |
] | |
] | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Facebook\WebDriver\Chrome\ChromeOptions; | |
use Facebook\WebDriver\Remote\DesiredCapabilities; | |
use Facebook\WebDriver\Remote\RemoteWebDriver; | |
use Illuminate\Support\ServiceProvider; | |
use Laravel\Dusk\Browser; | |
use Laravel\Dusk\Chrome\ChromeProcess; | |
class BrowserAutomationServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->singleton('chrome', function () { | |
return (new ChromeProcess())->toProcess(); | |
}); | |
$this->app->bind(\Facebook\WebDriver\WebDriver::class, function () { | |
$options = (new ChromeOptions())->addArguments(config('automation.chrome.arguments')); | |
return RemoteWebDriver::create( | |
'http://localhost:9515', | |
DesiredCapabilities::chrome()->setCapability( | |
ChromeOptions::CAPABILITY, | |
$options | |
) | |
); | |
}); | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Browser::$storeScreenshotsAt = config('automation.screenshot_path'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
app('chrome')->start(); | |
$browser = new \Laravel\Dusk\Browser(app()->make(\Facebook\WebDriver\WebDriver::class)); | |
$browser->visit('https://reddit.com'); | |
app('chrome')->stop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment