Skip to content

Commit

Permalink
add web driver manager with selenium
Browse files Browse the repository at this point in the history
  • Loading branch information
pyd committed Nov 13, 2018
1 parent b3ffec8 commit 66a2dfe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
28 changes: 16 additions & 12 deletions src/web/driver/selenium/Manager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
namespace pyd\testkit\web\driver\selenium;

use pyd\testkit\web\Driver;
use pyd\testkit\web\RemoteDriver;


/**
* Manage - create/destroy - the 'webdriver client' instance used to talk with
* the selenium server.
Expand All @@ -17,7 +21,7 @@ class Manager extends \yii\base\BaseObject implements \pyd\testkit\web\driver\Ma
public $url = 'http://localhost:4444/wd/hub';

protected $desiredCapabilities;

public $connectionTimeout;

public $requestTimeout;
Expand All @@ -39,6 +43,9 @@ public function setDesiredCapabilities(\DesiredCapabilities $dc)
*/
public function getDriver()
{
if (!$this->driverIsReady()) {
$this->createDriver();
}
return $this->driver;
}

Expand All @@ -65,26 +72,23 @@ public function driverIsReady()
*
* @throws \yii\base\InvalidCallException
*/
protected function createDriver()
private function createDriver()
{
try {
$driverClass = $this->driverClass;
$this->driver = $driverClass::create(
$this->url,
$this->desiredCapabilities,
$this->connectionTimeout,
$this->requestTimeout);
} catch (\WebDriverCurlException $e) {
throw new \yii\base\InvalidCallException("Cannot create web driver: " . $e->getMessage());
if (null === $this->desiredCapabilities) {
$this->desiredCapabilities = \DesiredCapabilities::firefox();
}

$this->driver = RemoteDriver::create($this->url, $this->desiredCapabilities, $this->connectionTimeout, $this->requestTimeout);
}

/**
* Close web driver session and destroy driver instance.
*/
protected function destroyDriver()
{
$this->driver->quit();
if ($this->driverIsReady()) {
$this->driver->quit();
}
$this->driver = null;
}
}
11 changes: 2 additions & 9 deletions src/web/driver/selenium/ManagerEventObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ public function onSetUp(\pyd\testkit\events\SetUp $event)
{
if ($this->driverRequired) {

if (!$this->driverIsReady()) {
$this->createDriver();
} else if (!$this->shareDriver) {
if (!$this->shareDriver) {
$this->destroyDriver();
$this->createDriver();
} else if ($this->clearCookies) {
$this->getDriver()->cookies()->deleteAll();
}
Expand All @@ -85,11 +82,7 @@ public function onSetUp(\pyd\testkit\events\SetUp $event)
public function onEndTestCase(\pyd\testkit\events\EndTestCase $event)
{
if ($this->driverRequired) {

if ($this->driverIsReady()) {
$this->destroyDriver();
}

$this->destroyDriver();
$this->shareDriver = null;
$this->clearCookies = null;
$this->driverRequired = null;
Expand Down

0 comments on commit 66a2dfe

Please sign in to comment.