Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Oct 3, 2024
1 parent bc90c44 commit 7961853
Show file tree
Hide file tree
Showing 29 changed files with 79 additions and 70 deletions.
19 changes: 10 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Symfony\Component\Panther\DomCrawler\Link as PantherLink;
use Symfony\Component\Panther\Exception\InvalidArgumentException;
use Symfony\Component\Panther\Exception\LogicException;
use Symfony\Component\Panther\Exception\RuntimeException;
use Symfony\Component\Panther\ProcessManager\BrowserManagerInterface;
use Symfony\Component\Panther\ProcessManager\ChromeManager;
use Symfony\Component\Panther\ProcessManager\FirefoxManager;
Expand All @@ -66,25 +67,25 @@ final class Client extends AbstractBrowser implements WebDriver, JavaScriptExecu
/**
* @param string[]|null $arguments
*/
public static function createChromeClient(string $chromeDriverBinary = null, array $arguments = null, array $options = [], string $baseUri = null): self
public static function createChromeClient(?string $chromeDriverBinary = null, ?array $arguments = null, array $options = [], ?string $baseUri = null): self
{
return new self(new ChromeManager($chromeDriverBinary, $arguments, $options), $baseUri);
}

/**
* @param string[]|null $arguments
*/
public static function createFirefoxClient(string $geckodriverBinary = null, array $arguments = null, array $options = [], string $baseUri = null): self
public static function createFirefoxClient(?string $geckodriverBinary = null, ?array $arguments = null, array $options = [], ?string $baseUri = null): self
{
return new self(new FirefoxManager($geckodriverBinary, $arguments, $options), $baseUri);
}

public static function createSeleniumClient(string $host = null, WebDriverCapabilities $capabilities = null, string $baseUri = null, array $options = []): self
public static function createSeleniumClient(?string $host = null, ?WebDriverCapabilities $capabilities = null, ?string $baseUri = null, array $options = []): self
{
return new self(new SeleniumManager($host, $capabilities, $options), $baseUri);
}

public function __construct(BrowserManagerInterface $browserManager, string $baseUri = null)
public function __construct(BrowserManagerInterface $browserManager, ?string $baseUri = null)
{
$this->browserManager = $browserManager;
$this->baseUri = $baseUri;
Expand Down Expand Up @@ -254,7 +255,7 @@ public function refreshCrawler(): PantherCrawler
return $this->crawler = $this->createCrawler();
}

public function request(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], string $content = null, bool $changeHistory = true): PantherCrawler
public function request(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], ?string $content = null, bool $changeHistory = true): PantherCrawler
{
if ('GET' !== $method) {
throw new InvalidArgumentException('Only the GET method is supported when using WebDriver.');
Expand All @@ -268,7 +269,7 @@ public function request(string $method, string $uri, array $parameters = [], arr

foreach (['parameters', 'files', 'server'] as $arg) {
if ([] !== $$arg) {
throw new InvalidArgumentException(sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
throw new InvalidArgumentException(\sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
}
}

Expand Down Expand Up @@ -770,14 +771,14 @@ public function ping(int $timeout = 1000): bool
}

/**
* @return \LogicException|\RuntimeException
* @return LogicException|RuntimeException
*/
private function createException(string $implementableClass): \Exception
{
if (null === $this->webDriver) {
return new \LogicException(sprintf('WebDriver not started yet. Call method `start()` first before calling any `%s` method.', $implementableClass));
return new LogicException(\sprintf('WebDriver not started yet. Call method `start()` first before calling any `%s` method.', $implementableClass));
}

return new \RuntimeException(sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), $implementableClass));
return new RuntimeException(\sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), $implementableClass));
}
}
2 changes: 1 addition & 1 deletion src/Cookie/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function webDriverToSymfony(WebDriverCookie $cookie): Cookie
return new Cookie($cookie->getName(), $cookie->getValue(), $expiry, $cookie->getPath(), (string) $cookie->getDomain(), (bool) $cookie->isSecure(), (bool) $cookie->isHttpOnly());
}

private function getWebDriverCookie(string $name, string $path = '/', string $domain = null): ?WebDriverCookie
private function getWebDriverCookie(string $name, string $path = '/', ?string $domain = null): ?WebDriverCookie
{
try {
$cookie = $this->webDriver->manage()->getCookieNamed($name);
Expand Down
18 changes: 9 additions & 9 deletions src/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class Crawler extends BaseCrawler implements WebDriverElement
/**
* @param WebDriverElement[] $elements
*/
public function __construct(array $elements = [], WebDriver $webDriver = null, string $uri = null)
public function __construct(array $elements = [], ?WebDriver $webDriver = null, ?string $uri = null)
{
$this->uri = $uri;
$this->webDriver = $webDriver;
Expand Down Expand Up @@ -179,7 +179,7 @@ public function ancestors(): static
/**
* @see https://github.com/symfony/symfony/issues/26432
*/
public function children(string $selector = null): static
public function children(?string $selector = null): static
{
$xpath = 'child::*';
if (null !== $selector) {
Expand All @@ -205,7 +205,7 @@ public function nodeName(): string
return $this->getElementOrThrow()->getTagName();
}

public function text(string $default = null, bool $normalizeWhitespace = true): string
public function text(?string $default = null, bool $normalizeWhitespace = true): string
{
if (!$normalizeWhitespace) {
throw new InvalidArgumentException('Panther only supports getting normalized text.');
Expand All @@ -222,7 +222,7 @@ public function text(string $default = null, bool $normalizeWhitespace = true):
}
}

public function html(string $default = null): string
public function html(?string $default = null): string
{
try {
$element = $this->getElementOrThrow();
Expand Down Expand Up @@ -276,19 +276,19 @@ public function filter($selector): static
public function selectLink($value): static
{
return $this->selectFromXpath(
sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %1$s) or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %1$s)]]', self::xpathLiteral(' '.$value.' '))
\sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %1$s) or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %1$s)]]', self::xpathLiteral(' '.$value.' '))
);
}

public function selectImage($value): static
{
return $this->selectFromXpath(sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', self::xpathLiteral($value)));
return $this->selectFromXpath(\sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', self::xpathLiteral($value)));
}

public function selectButton($value): static
{
return $this->selectFromXpath(
sprintf(
\sprintf(
'descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or @id=%3$s or @name=%3$s]',
'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")',
self::xpathLiteral(' '.$value.' '),
Expand Down Expand Up @@ -332,7 +332,7 @@ public function images(): array
return $images;
}

public function form(array $values = null, $method = null): Form
public function form(?array $values = null, $method = null): Form
{
$form = new Form($this->getElementOrThrow(), $this->webDriver);
if (null !== $values) {
Expand Down Expand Up @@ -395,7 +395,7 @@ private function selectFromXpath(string $xpath): self
/**
* @param WebDriverElement[]|null $nodes
*/
private function createSubCrawler(array $nodes = null): self
private function createSubCrawler(?array $nodes = null): self
{
return new self($nodes ?? [], $this->webDriver, $this->uri);
}
Expand Down
10 changes: 5 additions & 5 deletions src/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function select($value): void
public function tick(): void
{
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
throw new LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
throw new LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
}

$this->setValue(true);
Expand All @@ -68,7 +68,7 @@ public function tick(): void
public function untick(): void
{
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
throw new LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
throw new LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
}

$this->setValue(false);
Expand Down Expand Up @@ -116,7 +116,7 @@ public function setValue($value): void
{
if (\is_bool($value)) {
if ('checkbox' !== $this->type) {
throw new InvalidArgumentException(sprintf('Invalid argument of type "%s"', \gettype($value)));
throw new InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value)));
}

if ($value) {
Expand Down Expand Up @@ -191,12 +191,12 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName && 'select' !== $tagName) {
throw new LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
throw new LogicException(\sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
}

$type = strtolower((string) $this->element->getAttribute('type'));
if ('input' === $tagName && 'checkbox' !== $type && 'radio' !== $type) {
throw new LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type));
throw new LogicException(\sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type));
}

$this->type = 'select' === $tagName ? 'select' : $type;
Expand Down
4 changes: 2 additions & 2 deletions src/DomCrawler/Field/FileFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName) {
throw new LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
throw new LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
}

$type = strtolower($this->element->getAttribute('type'));
if ('file' !== $type) {
throw new LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
throw new LogicException(\sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
}

$value = $this->element->getAttribute('value');
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Field/InputFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName && 'button' !== $tagName) {
throw new LogicException(sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
throw new LogicException(\sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
}

$type = strtolower((string) $this->element->getAttribute('type'));
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Field/TextareaFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('textarea' !== $tagName) {
throw new LogicException(sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
throw new LogicException(\sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
}
}
}
8 changes: 4 additions & 4 deletions src/DomCrawler/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function setElement(WebDriverElement $element): void
try {
$form = $this->webDriver->findElement(WebDriverBy::id($formId));
} catch (NoSuchElementException $e) {
throw new LogicException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
throw new LogicException(\sprintf('The selected node has an invalid form attribute (%s).', $formId));
}

$this->element = $form;
Expand All @@ -78,7 +78,7 @@ private function setElement(WebDriverElement $element): void
}
} while ('form' !== $element->getTagName());
} elseif ('form' !== $tagName = $element->getTagName()) {
throw new LogicException(sprintf('Unable to submit on a "%s" tag.', $tagName));
throw new LogicException(\sprintf('Unable to submit on a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down Expand Up @@ -168,7 +168,7 @@ public function getFiles(): array
continue;
}

if ($field instanceof Field\FileFormField) {
if ($field instanceof FileFormField) {
$files[$field->getName()] = $field->getValue();
}
}
Expand Down Expand Up @@ -272,7 +272,7 @@ protected function getRawUri(): string
private function getFormElement(string $name): WebDriverElement
{
return $this->element->findElement(WebDriverBy::xpath(
sprintf('.//input[@name=%1$s] | .//textarea[@name=%1$s] | .//select[@name=%1$s] | .//button[@name=%1$s] | .//input[@name=%2$s] | .//textarea[@name=%2$s] | .//select[@name=%2$s] | .//button[@name=%2$s]', XPathEscaper::escapeQuotes($name), XPathEscaper::escapeQuotes($name.'[]'))
\sprintf('.//input[@name=%1$s] | .//textarea[@name=%1$s] | .//select[@name=%1$s] | .//button[@name=%1$s] | .//input[@name=%2$s] | .//textarea[@name=%2$s] | .//select[@name=%2$s] | .//button[@name=%2$s]', XPathEscaper::escapeQuotes($name), XPathEscaper::escapeQuotes($name.'[]'))
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Image extends BaseImage
public function __construct(WebDriverElement $element)
{
if ('img' !== $tagName = $element->getTagName()) {
throw new LogicException(sprintf('Unable to visualize a "%s" tag.', $tagName));
throw new LogicException(\sprintf('Unable to visualize a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(WebDriverElement $element, string $currentUri)
{
$tagName = $element->getTagName();
if ('a' !== $tagName && 'area' !== $tagName && 'link' !== $tagName) {
throw new LogicException(sprintf('Unable to navigate from a "%s" tag.', $tagName));
throw new LogicException(\sprintf('Unable to navigate from a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\Component\Panther\Exception;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\Component\Panther\Exception;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\Component\Panther\Exception;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\Component\Panther\Exception;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ trait ExceptionThrower
{
private function createNotSupportedException(string $method): \InvalidArgumentException
{
return new \InvalidArgumentException(sprintf('The "%s" method is not supported when using WebDriver.', $method));
return new \InvalidArgumentException(\sprintf('The "%s" method is not supported when using WebDriver.', $method));
}
}
Loading

0 comments on commit 7961853

Please sign in to comment.