Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw explanative LogicException when driver is not started yet #268

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public function findElements(WebDriverBy $locator)
public function executeScript($script, array $arguments = [])
{
if (!$this->webDriver instanceof JavaScriptExecutor) {
throw new \RuntimeException(sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), JavaScriptExecutor::class));
$this->throwException(JavaScriptExecutor::class);
}

return $this->webDriver->executeScript($script, $arguments);
Expand All @@ -429,7 +429,7 @@ public function executeScript($script, array $arguments = [])
public function executeAsyncScript($script, array $arguments = [])
{
if (!$this->webDriver instanceof JavaScriptExecutor) {
throw new \RuntimeException(sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), JavaScriptExecutor::class));
$this->throwException(JavaScriptExecutor::class);
}

return $this->webDriver->executeAsyncScript($script, $arguments);
Expand All @@ -438,7 +438,7 @@ public function executeAsyncScript($script, array $arguments = [])
public function getKeyboard()
{
if (!$this->webDriver instanceof WebDriverHasInputDevices) {
throw new \RuntimeException(sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), WebDriverHasInputDevices::class));
$this->throwException(WebDriverHasInputDevices::class);
}

return $this->webDriver->getKeyboard();
Expand All @@ -447,9 +447,17 @@ public function getKeyboard()
public function getMouse(): WebDriverMouse
{
if (!$this->webDriver instanceof WebDriverHasInputDevices) {
throw new \RuntimeException(sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), WebDriverHasInputDevices::class));
$this->throwException(WebDriverHasInputDevices::class);
}

return new WebDriverMouse($this->webDriver->getMouse(), $this);
}

private function throwException(string $implementableClass): void
{
if (null === $this->webDriver) {
throw new \LogicException(sprintf('WebDriver not started yet. Call method `start()` first before calling any `%s` method.', $implementableClass));
}
throw new \RuntimeException(sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), $implementableClass));
}
}
9 changes: 8 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Facebook\WebDriver\JavaScriptExecutor;
use Facebook\WebDriver\WebDriver;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Symfony\Component\BrowserKit\Client as BrowserKitClient;
use Symfony\Component\BrowserKit\AbstractBrowser as BrowserKitClient;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar as BrowserKitCookieJar;
use Symfony\Component\DomCrawler\Crawler as DomCrawlerCrawler;
Expand Down Expand Up @@ -90,6 +90,13 @@ public function testExecuteScript()
$this->assertSame('P1', $innerText);
}

public function testExecuteScriptLogicExceptionWhenDriverIsNotStartedYet()
{
$this->expectException(\LogicException::class);
$client = Client::createChromeClient();
$client->executeScript('return document.querySelector(arguments[0]).innerText;', ['.p-1']);
}

public function testExecuteAsyncScript()
{
$client = self::createPantherClient();
Expand Down