Skip to content

Commit

Permalink
Throw an UnknownCommand Exception if isW3C() doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
justafish committed Oct 3, 2023
1 parent b84f67b commit 580ff48
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ public static function getDefaultCapabilities()
);
}

/**
* Checks if the WebDriver session is W3C compatible.
*
* @return bool
*
* @throws UnknownCommand
*/
public function isW3C() {
try {
// This method doesn't exist in older versions of php-webdriver.
/** @phpstan-ignore-next-line variable.undefined */
return $this->wdSession->isW3C();
} catch (UnknownCommand $e) {
return false;
}
}

/**
* Makes sure that the Syn event library has been injected into the current page,
* and return $this for a fluid interface,
Expand Down Expand Up @@ -317,7 +334,7 @@ private function executeJsOnElement(Element $element, string $script, bool $sync
{
$script = str_replace('{{ELEMENT}}', 'arguments[0]', $script);

if ($this->wdSession->isW3C()) {
if ($this->isW3C()) {
$options = array(
'script' => $script,
'args' => [
Expand Down Expand Up @@ -562,7 +579,9 @@ public function getAttribute(string $xpath, string $name)
public function getValue(string $xpath)
{
$element = $this->findElement($xpath);
if ($this->wdSession->isW3C()) {
if ($this->isW3C()) {
// This method doesn't exist in older versions of php-webdriver.
/** @phpstan-ignore-next-line variable.undefined */
return $element->property('value');
}
$elementName = strtolower($element->name());
Expand Down Expand Up @@ -674,7 +693,7 @@ public function setValue(string $xpath, $value)
throw new DriverException('Only string values can be used for a file input.');
}

if ($this->wdSession->isW3C()) {
if ($this->isW3C()) {
$element->postValue(array('text' => $value));
}
else {
Expand All @@ -696,7 +715,7 @@ public function setValue(string $xpath, $value)
$value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value;
}

if ($this->wdSession->isW3C()) {
if ($this->isW3C()) {
$element->postValue(array('text' => $value));
}
else {
Expand Down Expand Up @@ -826,7 +845,7 @@ public function attachFile(string $xpath, string $path)
$remotePath = $path;
}

if ($this->wdSession->isW3C()) {
if ($this->isW3C()) {
$element->postValue(array('text' => $remotePath));
}
else {
Expand Down Expand Up @@ -952,7 +971,9 @@ public function wait(int $timeout, string $condition)

public function resizeWindow(int $width, int $height, ?string $name = null)
{
if ($this->wdSession->isW3C()) {
if ($this->isW3C()) {
// This method doesn't exist in older versions of php-webdriver.
/** @phpstan-ignore-next-line variable.undefined */
$this->wdSession->window($name ? $name : 'current')->postRect(
array('width' => $width, 'height' => $height)
);
Expand Down

0 comments on commit 580ff48

Please sign in to comment.