Skip to content

Instantly share code, notes, and snippets.

@ux-engineer
Last active January 15, 2018 19:16
Show Gist options
  • Save ux-engineer/6c72a87d03311d282bd4010c9df90b3e to your computer and use it in GitHub Desktop.
Save ux-engineer/6c72a87d03311d282bd4010c9df90b3e to your computer and use it in GitHub Desktop.
Related to laravel/homestead issue #793
<?php
namespace App;
use Closure;
use Exception;
use Throwable;
use Laravel\Dusk\Browser as DuskBrowser;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
class Browser
{
private $browser;
public function browse(Closure $callback)
{
if (!$this->browser) {
$this->browser = $this->newBrowser($this->createWebDriver());
}
try {
$callback($this->browser);
} catch (Exception $e) {
throw $e;
} catch (Throwable $e) {
throw $e;
}
}
public function __destruct()
{
if ($this->browser) {
$this->closeBrowser();
}
}
protected function closeBrowser()
{
if (!$this->browser) {
throw new Exception("The browser hasn't been initialized yet");
}
$this->browser->quit();
$this->browser = null;
}
protected function newBrowser($driver)
{
return new DuskBrowser($driver);
}
/**
* Create the remote web driver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function createWebDriver()
{
return retry(5, function () {
return $this->driver();
}, 50);
}
protected function driver()
{
\Log::debug('getting driver');
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--no-sandbox',
'--no-first-run',
'--window-size=1600,1280'
]);
return RemoteWebDriver::create(
'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY,
$options
),
150000,
150000
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment