Skip to content

Commit

Permalink
Throw exception when getClient is not started
Browse files Browse the repository at this point in the history
This resolves amongst other things issues with behatch
also see:
Behatch/contexts#284
Behatch/contexts#292
  • Loading branch information
robertfausk committed Oct 8, 2020
1 parent ef04718 commit 644d9a8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 44 deletions.
91 changes: 47 additions & 44 deletions src/PantherDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PantherDriver extends CoreDriver
public const CHROME = 'chrome';
public const FIREFOX = 'firefox';

/** @var Client */
/** @var Client|null */
private $client;
private $started = false;
private $removeScriptFromUrl = false;
Expand All @@ -66,6 +66,9 @@ public function __construct(
*/
public function getClient()
{
if (!$this->isStarted()) {
throw new DriverException('Client is not (yet) started.');
}
return $this->client;
}

Expand Down Expand Up @@ -125,7 +128,7 @@ public function isStarted()
*/
public function stop()
{
$this->client->quit();
$this->getClient()->quit();
self::stopWebServer();
$this->started = false;
}
Expand All @@ -139,34 +142,34 @@ public function reset()
// $useSpeedUp = false;
$useSpeedUp = true;
if ($useSpeedUp) {
$this->client->getWebDriver()->manage()->deleteAllCookies();
$this->getClient()->getWebDriver()->manage()->deleteAllCookies();
try {
$history = $this->client->getHistory();
$history = $this->getClient()->getHistory();
if ($history) {
$history->clear();
}
} catch (\LogicException $e) {
// History is not available when using e.g. WebDriver.
}
if (
$this->client->getWebDriver() instanceof JavaScriptExecutor
&& !in_array($this->client->getCurrentURL(), ['', 'about:blank', 'data:,'], true)
$this->getClient()->getWebDriver() instanceof JavaScriptExecutor
&& !in_array($this->getClient()->getCurrentURL(), ['', 'about:blank', 'data:,'], true)
) {
$this->executeScript('localStorage.clear();');
}
// not sure if we should also close all windows
// $lastWindowHandle = \end($this->client->getWindowHandles());
// $lastWindowHandle = \end($this->getClient()->getWindowHandles());
// if ($lastWindowHandle) {
// $this->client->switchTo()->window($lastWindowHandle);
// $this->getClient()->switchTo()->window($lastWindowHandle);
// }
// $this->client->getWebDriver()->navigate()->refresh();
// $this->client->refreshCrawler();
// if (\count($this->client->getWindowHandles()) > 1) {
// $this->client->getWebDriver()->close();
// $this->getClient()->getWebDriver()->navigate()->refresh();
// $this->getClient()->refreshCrawler();
// if (\count($this->getClient()->getWindowHandles()) > 1) {
// $this->getClient()->getWebDriver()->close();
// }
} else {
// Restarting the client resets the cookies and the history
$this->client->restart();
$this->getClient()->restart();
}

}
Expand All @@ -176,48 +179,48 @@ public function reset()
*/
public function visit($url)
{
$this->client->get($this->prepareUrl($url));
$this->getClient()->get($this->prepareUrl($url));
}

/**
* {@inheritdoc}
*/
public function getCurrentUrl()
{
return $this->client->getCurrentURL();
return $this->getClient()->getCurrentURL();
}

/**
* {@inheritdoc}
*/
public function reload()
{
$this->client->reload();
$this->getClient()->reload();
}

/**
* {@inheritdoc}
*/
public function forward()
{
$this->client->forward();
$this->getClient()->forward();
}

/**
* {@inheritdoc}
*/
public function back()
{
$this->client->back();
$this->getClient()->back();
}

/**
* {@inheritdoc}
*/
public function switchToWindow($name = null)
{
$this->client->switchTo()->window($name);
$this->client->refreshCrawler();
$this->getClient()->switchTo()->window($name);
$this->getClient()->refreshCrawler();
}

/**
Expand All @@ -226,14 +229,14 @@ public function switchToWindow($name = null)
public function switchToIFrame($name = null)
{
if (null === $name) {
$this->client->switchTo()->defaultContent();
$this->getClient()->switchTo()->defaultContent();
} elseif ($name) {
$iFrameElement = $this->getCrawlerElement($this->getFilteredCrawler(\sprintf("//iframe[@name='%s']", $name)));
$this->client->switchTo()->frame($iFrameElement);
$this->getClient()->switchTo()->frame($iFrameElement);
} else {
$this->client->switchTo()->frame(null);
$this->getClient()->switchTo()->frame(null);
}
$this->client->refreshCrawler();
$this->getClient()->refreshCrawler();
}

/**
Expand All @@ -247,7 +250,7 @@ public function setCookie($name, $value = null)
return;
}

$jar = $this->client->getCookieJar();
$jar = $this->getClient()->getCookieJar();
// @see: https://github.com/w3c/webdriver/issues/1238
$jar->set(new Cookie($name, \rawurlencode((string)$value)));
}
Expand All @@ -260,7 +263,7 @@ public function setCookie($name, $value = null)
private function deleteCookie($name)
{
$path = $this->getCookiePath();
$jar = $this->client->getCookieJar();
$jar = $this->getClient()->getCookieJar();

do {
if (null !== $jar->get($name, $path)) {
Expand Down Expand Up @@ -292,7 +295,7 @@ private function getCookiePath()
*/
public function getCookie($name)
{
$cookies = $this->client->getCookieJar()->all();
$cookies = $this->getClient()->getCookieJar()->all();

foreach ($cookies as $cookie) {
if ($cookie->getName() === $name) {
Expand All @@ -308,31 +311,31 @@ public function getCookie($name)
*/
public function getContent()
{
return $this->client->getWebDriver()->getPageSource();
return $this->getClient()->getWebDriver()->getPageSource();
}

/**
* {@inheritdoc}
*/
public function getScreenshot($saveAs = null): string
{
return $this->client->takeScreenshot($saveAs);
return $this->getClient()->takeScreenshot($saveAs);
}

/**
* {@inheritdoc}
*/
public function getWindowNames()
{
return $this->client->getWindowHandles();
return $this->getClient()->getWindowHandles();
}

/**
* {@inheritdoc}
*/
public function getWindowName()
{
return $this->client->getWindowHandle();
return $this->getClient()->getWindowHandle();
}

/**
Expand All @@ -348,7 +351,7 @@ public function isVisible($xpath)
*/
public function mouseOver($xpath)
{
$this->client->getMouse()->mouseMove($this->toCoordinates($xpath));
$this->getClient()->getMouse()->mouseMove($this->toCoordinates($xpath));
}

/**
Expand Down Expand Up @@ -604,24 +607,24 @@ public function selectOption($xpath, $value, $multiple = false)
*/
public function click($xpath)
{
$this->client->getMouse()->click($this->toCoordinates($xpath));
$this->client->refreshCrawler();
$this->getClient()->getMouse()->click($this->toCoordinates($xpath));
$this->getClient()->refreshCrawler();
}

/**
* {@inheritdoc}
*/
public function doubleClick($xpath)
{
$this->client->getMouse()->doubleClick($this->toCoordinates($xpath));
$this->getClient()->getMouse()->doubleClick($this->toCoordinates($xpath));
}

/**
* {@inheritdoc}
*/
public function rightClick($xpath)
{
$this->client->getMouse()->contextClick($this->toCoordinates($xpath));
$this->getClient()->getMouse()->contextClick($this->toCoordinates($xpath));
}

/**
Expand Down Expand Up @@ -669,7 +672,7 @@ public function executeScript($script)
$script = '(' . $script . ')';
}

return $this->client->executeScript($script);
return $this->getClient()->executeScript($script);
}

/**
Expand All @@ -681,7 +684,7 @@ public function evaluateScript($script)
$script = 'return ' . $script;
}

return $this->client->executeScript($script);
return $this->getClient()->executeScript($script);
}

/**
Expand All @@ -707,7 +710,7 @@ public function wait($timeout, $condition)
public function resizeWindow($width, $height, $name = null)
{
$size = new WebDriverDimension($width, $height);
$this->client->getWebDriver()->manage()->window()->setSize($size);
$this->getClient()->getWebDriver()->manage()->window()->setSize($size);
}

/**
Expand All @@ -727,8 +730,8 @@ public function submitForm($xpath)
{
$crawler = $this->getFilteredCrawler($xpath);

$this->client->submit($crawler->form());
$this->client->refreshCrawler();
$this->getClient()->submit($crawler->form());
$this->getClient()->refreshCrawler();
}

/**
Expand All @@ -738,7 +741,7 @@ public function submitForm($xpath)
*/
protected function getResponse()
{
$response = $this->client->getInternalResponse();
$response = $this->getClient()->getInternalResponse();

if (null === $response) {
throw new DriverException('Unable to access the response before visiting a page');
Expand Down Expand Up @@ -936,7 +939,7 @@ private function getFilteredCrawler($xpath): Crawler
*/
private function getCrawler(): Crawler
{
$crawler = $this->client->getCrawler();
$crawler = $this->getClient()->getCrawler();

if (null === $crawler) {
throw new DriverException('Unable to access the response content before visiting a page');
Expand Down Expand Up @@ -965,7 +968,7 @@ private function toCoordinates(string $xpath): WebDriverCoordinates

private function getWebDriverActions(): WebDriverActions
{
$webDriver = $this->client->getWebDriver();
$webDriver = $this->getClient()->getWebDriver();
if (!$webDriver instanceof WebDriverHasInputDevices) {
throw new UnsupportedDriverActionException('Mouse manipulations are not supported by %s', $this);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/GetContentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace Behat\Mink\Tests\Driver;

use Behat\Mink\Exception\DriverException;

class GetContentTest extends TestCase
{
public function test_throws_an_exception_if_driver_not_startet():void
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('Client is not (yet) started.');
$driver = $this->getSession()->getDriver();
if ($driver->isStarted()) {
$driver->stop();
}
$driver->getContent();
}
}

0 comments on commit 644d9a8

Please sign in to comment.