Last active
January 15, 2018 19:15
-
-
Save ux-engineer/29d050894ee4bdb912ec6433b6208c5b to your computer and use it in GitHub Desktop.
Related to laravel/dusk issue #437
This file contains 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; | |
use Closure; | |
use Exception; | |
use Throwable; | |
use Laravel\Dusk\Browser as DuskBrowser; | |
use Laravel\Dusk\Chrome\SupportsChrome; | |
use Facebook\WebDriver\Chrome\ChromeOptions; | |
use Facebook\WebDriver\Remote\RemoteWebDriver; | |
use Facebook\WebDriver\Remote\DesiredCapabilities; | |
class Browser | |
{ | |
use SupportsChrome; | |
private $browser; | |
public function __construct() | |
{ | |
static::startChromeDriver(); | |
} | |
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