Skip to content

Commit 644d9a8

Browse files
committed
Throw exception when getClient is not started
This resolves amongst other things issues with behatch also see: Behatch/contexts#284 Behatch/contexts#292
1 parent ef04718 commit 644d9a8

2 files changed

Lines changed: 67 additions & 44 deletions

File tree

src/PantherDriver.php

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class PantherDriver extends CoreDriver
4545
public const CHROME = 'chrome';
4646
public const FIREFOX = 'firefox';
4747

48-
/** @var Client */
48+
/** @var Client|null */
4949
private $client;
5050
private $started = false;
5151
private $removeScriptFromUrl = false;
@@ -66,6 +66,9 @@ public function __construct(
6666
*/
6767
public function getClient()
6868
{
69+
if (!$this->isStarted()) {
70+
throw new DriverException('Client is not (yet) started.');
71+
}
6972
return $this->client;
7073
}
7174

@@ -125,7 +128,7 @@ public function isStarted()
125128
*/
126129
public function stop()
127130
{
128-
$this->client->quit();
131+
$this->getClient()->quit();
129132
self::stopWebServer();
130133
$this->started = false;
131134
}
@@ -139,34 +142,34 @@ public function reset()
139142
// $useSpeedUp = false;
140143
$useSpeedUp = true;
141144
if ($useSpeedUp) {
142-
$this->client->getWebDriver()->manage()->deleteAllCookies();
145+
$this->getClient()->getWebDriver()->manage()->deleteAllCookies();
143146
try {
144-
$history = $this->client->getHistory();
147+
$history = $this->getClient()->getHistory();
145148
if ($history) {
146149
$history->clear();
147150
}
148151
} catch (\LogicException $e) {
149152
// History is not available when using e.g. WebDriver.
150153
}
151154
if (
152-
$this->client->getWebDriver() instanceof JavaScriptExecutor
153-
&& !in_array($this->client->getCurrentURL(), ['', 'about:blank', 'data:,'], true)
155+
$this->getClient()->getWebDriver() instanceof JavaScriptExecutor
156+
&& !in_array($this->getClient()->getCurrentURL(), ['', 'about:blank', 'data:,'], true)
154157
) {
155158
$this->executeScript('localStorage.clear();');
156159
}
157160
// not sure if we should also close all windows
158-
// $lastWindowHandle = \end($this->client->getWindowHandles());
161+
// $lastWindowHandle = \end($this->getClient()->getWindowHandles());
159162
// if ($lastWindowHandle) {
160-
// $this->client->switchTo()->window($lastWindowHandle);
163+
// $this->getClient()->switchTo()->window($lastWindowHandle);
161164
// }
162-
// $this->client->getWebDriver()->navigate()->refresh();
163-
// $this->client->refreshCrawler();
164-
// if (\count($this->client->getWindowHandles()) > 1) {
165-
// $this->client->getWebDriver()->close();
165+
// $this->getClient()->getWebDriver()->navigate()->refresh();
166+
// $this->getClient()->refreshCrawler();
167+
// if (\count($this->getClient()->getWindowHandles()) > 1) {
168+
// $this->getClient()->getWebDriver()->close();
166169
// }
167170
} else {
168171
// Restarting the client resets the cookies and the history
169-
$this->client->restart();
172+
$this->getClient()->restart();
170173
}
171174

172175
}
@@ -176,48 +179,48 @@ public function reset()
176179
*/
177180
public function visit($url)
178181
{
179-
$this->client->get($this->prepareUrl($url));
182+
$this->getClient()->get($this->prepareUrl($url));
180183
}
181184

182185
/**
183186
* {@inheritdoc}
184187
*/
185188
public function getCurrentUrl()
186189
{
187-
return $this->client->getCurrentURL();
190+
return $this->getClient()->getCurrentURL();
188191
}
189192

190193
/**
191194
* {@inheritdoc}
192195
*/
193196
public function reload()
194197
{
195-
$this->client->reload();
198+
$this->getClient()->reload();
196199
}
197200

198201
/**
199202
* {@inheritdoc}
200203
*/
201204
public function forward()
202205
{
203-
$this->client->forward();
206+
$this->getClient()->forward();
204207
}
205208

206209
/**
207210
* {@inheritdoc}
208211
*/
209212
public function back()
210213
{
211-
$this->client->back();
214+
$this->getClient()->back();
212215
}
213216

214217
/**
215218
* {@inheritdoc}
216219
*/
217220
public function switchToWindow($name = null)
218221
{
219-
$this->client->switchTo()->window($name);
220-
$this->client->refreshCrawler();
222+
$this->getClient()->switchTo()->window($name);
223+
$this->getClient()->refreshCrawler();
221224
}
222225

223226
/**
@@ -226,14 +229,14 @@ public function switchToWindow($name = null)
226229
public function switchToIFrame($name = null)
227230
{
228231
if (null === $name) {
229-
$this->client->switchTo()->defaultContent();
232+
$this->getClient()->switchTo()->defaultContent();
230233
} elseif ($name) {
231234
$iFrameElement = $this->getCrawlerElement($this->getFilteredCrawler(\sprintf("//iframe[@name='%s']", $name)));
232-
$this->client->switchTo()->frame($iFrameElement);
235+
$this->getClient()->switchTo()->frame($iFrameElement);
233236
} else {
234-
$this->client->switchTo()->frame(null);
237+
$this->getClient()->switchTo()->frame(null);
235238
}
236-
$this->client->refreshCrawler();
239+
$this->getClient()->refreshCrawler();
237240
}
238241

239242
/**
@@ -247,7 +250,7 @@ public function setCookie($name, $value = null)
247250
return;
248251
}
249252

250-
$jar = $this->client->getCookieJar();
253+
$jar = $this->getClient()->getCookieJar();
251254
// @see: https://github.com/w3c/webdriver/issues/1238
252255
$jar->set(new Cookie($name, \rawurlencode((string)$value)));
253256
}
@@ -260,7 +263,7 @@ public function setCookie($name, $value = null)
260263
private function deleteCookie($name)
261264
{
262265
$path = $this->getCookiePath();
263-
$jar = $this->client->getCookieJar();
266+
$jar = $this->getClient()->getCookieJar();
264267

265268
do {
266269
if (null !== $jar->get($name, $path)) {
@@ -292,7 +295,7 @@ private function getCookiePath()
292295
*/
293296
public function getCookie($name)
294297
{
295-
$cookies = $this->client->getCookieJar()->all();
298+
$cookies = $this->getClient()->getCookieJar()->all();
296299

297300
foreach ($cookies as $cookie) {
298301
if ($cookie->getName() === $name) {
@@ -308,31 +311,31 @@ public function getCookie($name)
308311
*/
309312
public function getContent()
310313
{
311-
return $this->client->getWebDriver()->getPageSource();
314+
return $this->getClient()->getWebDriver()->getPageSource();
312315
}
313316

314317
/**
315318
* {@inheritdoc}
316319
*/
317320
public function getScreenshot($saveAs = null): string
318321
{
319-
return $this->client->takeScreenshot($saveAs);
322+
return $this->getClient()->takeScreenshot($saveAs);
320323
}
321324

322325
/**
323326
* {@inheritdoc}
324327
*/
325328
public function getWindowNames()
326329
{
327-
return $this->client->getWindowHandles();
330+
return $this->getClient()->getWindowHandles();
328331
}
329332

330333
/**
331334
* {@inheritdoc}
332335
*/
333336
public function getWindowName()
334337
{
335-
return $this->client->getWindowHandle();
338+
return $this->getClient()->getWindowHandle();
336339
}
337340

338341
/**
@@ -348,7 +351,7 @@ public function isVisible($xpath)
348351
*/
349352
public function mouseOver($xpath)
350353
{
351-
$this->client->getMouse()->mouseMove($this->toCoordinates($xpath));
354+
$this->getClient()->getMouse()->mouseMove($this->toCoordinates($xpath));
352355
}
353356

354357
/**
@@ -604,24 +607,24 @@ public function selectOption($xpath, $value, $multiple = false)
604607
*/
605608
public function click($xpath)
606609
{
607-
$this->client->getMouse()->click($this->toCoordinates($xpath));
608-
$this->client->refreshCrawler();
610+
$this->getClient()->getMouse()->click($this->toCoordinates($xpath));
611+
$this->getClient()->refreshCrawler();
609612
}
610613

611614
/**
612615
* {@inheritdoc}
613616
*/
614617
public function doubleClick($xpath)
615618
{
616-
$this->client->getMouse()->doubleClick($this->toCoordinates($xpath));
619+
$this->getClient()->getMouse()->doubleClick($this->toCoordinates($xpath));
617620
}
618621

619622
/**
620623
* {@inheritdoc}
621624
*/
622625
public function rightClick($xpath)
623626
{
624-
$this->client->getMouse()->contextClick($this->toCoordinates($xpath));
627+
$this->getClient()->getMouse()->contextClick($this->toCoordinates($xpath));
625628
}
626629

627630
/**
@@ -669,7 +672,7 @@ public function executeScript($script)
669672
$script = '(' . $script . ')';
670673
}
671674

672-
return $this->client->executeScript($script);
675+
return $this->getClient()->executeScript($script);
673676
}
674677

675678
/**
@@ -681,7 +684,7 @@ public function evaluateScript($script)
681684
$script = 'return ' . $script;
682685
}
683686

684-
return $this->client->executeScript($script);
687+
return $this->getClient()->executeScript($script);
685688
}
686689

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

713716
/**
@@ -727,8 +730,8 @@ public function submitForm($xpath)
727730
{
728731
$crawler = $this->getFilteredCrawler($xpath);
729732

730-
$this->client->submit($crawler->form());
731-
$this->client->refreshCrawler();
733+
$this->getClient()->submit($crawler->form());
734+
$this->getClient()->refreshCrawler();
732735
}
733736

734737
/**
@@ -738,7 +741,7 @@ public function submitForm($xpath)
738741
*/
739742
protected function getResponse()
740743
{
741-
$response = $this->client->getInternalResponse();
744+
$response = $this->getClient()->getInternalResponse();
742745

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

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

966969
private function getWebDriverActions(): WebDriverActions
967970
{
968-
$webDriver = $this->client->getWebDriver();
971+
$webDriver = $this->getClient()->getWebDriver();
969972
if (!$webDriver instanceof WebDriverHasInputDevices) {
970973
throw new UnsupportedDriverActionException('Mouse manipulations are not supported by %s', $this);
971974
}

tests/GetContentTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Behat\Mink\Tests\Driver;
5+
6+
use Behat\Mink\Exception\DriverException;
7+
8+
class GetContentTest extends TestCase
9+
{
10+
public function test_throws_an_exception_if_driver_not_startet():void
11+
{
12+
$this->expectException(DriverException::class);
13+
$this->expectExceptionMessage('Client is not (yet) started.');
14+
$driver = $this->getSession()->getDriver();
15+
if ($driver->isStarted()) {
16+
$driver->stop();
17+
}
18+
$driver->getContent();
19+
}
20+
}

0 commit comments

Comments
 (0)