Skip to content

Commit

Permalink
Allow a different port to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Apr 5, 2018
1 parent e3e89b6 commit 38f7397
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"require": {
"facebook/webdriver": "^1.0",
"laravel/dusk": ">=3.0.0,<3.1",
"symfony/process": "^4.0",
"php": "^7.1"
},
"require-dev": {
Expand Down
58 changes: 43 additions & 15 deletions src/Drivers/Chrome.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverCapabilities;
use Laravel\Dusk\Chrome\SupportsChrome;

class Chrome implements DriverInterface
{
use SupportsChrome;
/**
* The port to run on.
*
* @var int
*/
private $port;

/**
* @var callable $afterClass A function to call after the class is finished with.
* The Chromedriver process instance.
*
* @var \Symfony\Component\Process\Process
*/
private static $afterClass;
private $process;

/**
* @var WebDriverCapabilities $capabilities The capabilities in use.
Expand All @@ -26,9 +32,11 @@ class Chrome implements DriverInterface
/**
* Create a new instance and automatically start the driver.
*/
public function __construct()
public function __construct(int $port = 9515)
{
static::startChromeDriver();
$this->port = $port;

$this->start();

$capabilities = DesiredCapabilities::chrome();

Expand All @@ -53,29 +61,49 @@ public function setCapabilities(WebDriverCapabilities $capabilities): void
*/
public function getDriver(): RemoteWebDriver
{
return RemoteWebDriver::create("http://localhost:9515", $this->capabilities);
return RemoteWebDriver::create("http://localhost:{$this->port}", $this->capabilities);
}


/**
* Required for upstream compatibility.
* Start the Chromedriver process.
*
* @param callable $handler A function to call after the class is finished with.
*
* @return void
* @return $this
*/
protected static function afterClass($handler)
public function start(): DriverInterface
{
self::$afterClass = $handler;
if (!$this->process) {
$this->process = (new ChromeProcess($this->port))->toProcess();
$this->process->start();
}

return $this;
}


/**
* Ensure the driver is closed by the upstream library.
*
* @return $this
*/
public function stop(): DriverInterface
{
if ($this->process) {
$this->process->stop();
unset($this->process);
}

return $this;
}


/**
* Automatically end the driver when this class is done with.
*
* @return void
*/
public function __destruct()
{
$handler = self::$afterClass;
$handler();
$this->stop();
}
}
39 changes: 39 additions & 0 deletions src/Drivers/ChromeProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace duncan3dc\Laravel\Drivers;

use Symfony\Component\Process\Process;

class ChromeProcess extends \Laravel\Dusk\Chrome\ChromeProcess
{
/**
* The port to run the Chromedriver on.
*
* @var int
*/
private $port;

/**
* Create a new instance.
*
* @param int $port The port to run on
*/
public function __construct(int $port = null)
{
parent::__construct();
$this->port = $port ?: 9515;
}


/**
* Build the Chromedriver with Symfony Process.
*
* @return Process
*/
protected function process()
{
return (new Process(
[realpath($this->driver), " --port={$this->port}"], null, $this->chromeEnvironment()
));
}
}

0 comments on commit 38f7397

Please sign in to comment.