Skip to content

Commit

Permalink
Rewrite timeout handling code to support Selenium Server 3
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Nov 2, 2024
1 parent 28f43ec commit e5ab502
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use WebDriver\Element;
use WebDriver\Exception\InvalidArgument;
use WebDriver\Exception\NoSuchElement;
use WebDriver\Exception\ScriptTimeout;
use WebDriver\Exception\StaleElementReference;
use WebDriver\Exception\Timeout;
use WebDriver\Exception\UnknownCommand;
use WebDriver\Exception\UnknownError;
use WebDriver\Key;
Expand Down Expand Up @@ -61,6 +63,11 @@ class Selenium2Driver extends CoreDriver
*/
private $wdSession;

/**
* @var bool
*/
private $isW3C = false;

/**
* The timeout configuration
* @var array{script?: int, implicit?: int, page?: int}
Expand Down Expand Up @@ -343,14 +350,17 @@ public function start()
{
try {
$this->wdSession = $this->webDriver->session($this->browserName, $this->desiredCapabilities);

$status = $this->webDriver->status();
$this->isW3C = version_compare($status['build']['version'], '3.0.0', '>=');

$this->applyTimeouts();
$this->initialWindowHandle = $this->getWebDriverSession()->window_handle();
} catch (\Exception $e) {
throw new DriverException('Could not open connection: '.$e->getMessage(), 0, $e);
}

$this->started = true;

$this->applyTimeouts();
$this->initialWindowHandle = $this->getWebDriverSession()->window_handle();
}

/**
Expand All @@ -376,9 +386,25 @@ public function setTimeouts(array $timeouts)
*/
private function applyTimeouts(): void
{
$validTimeoutTypes = array('script', 'implicit', 'page load', 'pageLoad');

try {
foreach ($this->timeouts as $type => $param) {
$this->getWebDriverSession()->timeouts($type, $param);
if (!in_array($type, $validTimeoutTypes)) {
throw new DriverException('Invalid timeout type: ' . $type);
}

if ($type === 'page load' && $this->isW3C) {
$type = 'pageLoad';
} elseif ($type === 'pageLoad' && !$this->isW3C) {
$type = 'page load';
}

if ($this->isW3C) {
$this->getWebDriverSession()->timeouts(array($type => $param));
} else {
$this->getWebDriverSession()->timeouts($type, $param);
}
}
} catch (UnknownError $e) {
// Selenium 2.x.
Expand All @@ -401,6 +427,7 @@ public function stop()
}

$this->started = false;
$this->isW3C = false;
try {
$this->wdSession->close();
} catch (\Exception $e) {
Expand Down Expand Up @@ -428,7 +455,15 @@ public function reset()

public function visit(string $url)
{
$this->getWebDriverSession()->open($url);
try {
$this->getWebDriverSession()->open($url);
} catch (Timeout $e) {
// The W3C compatible Selenium Server.
throw new DriverException('Page failed to load: ' . $e->getMessage(), 0, $e);
} catch (ScriptTimeout $e) {
// The Non-W3C compatible Selenium Server.
throw new DriverException('Page failed to load: ' . $e->getMessage(), 0, $e);
}
}

public function getCurrentUrl()
Expand Down
25 changes: 25 additions & 0 deletions tests/Custom/TimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testInvalidTimeoutSettingThrowsException()
\assert($driver instanceof Selenium2Driver);

$this->expectException('\Behat\Mink\Exception\DriverException');
$this->expectExceptionMessage('Invalid timeout type: invalid');
$driver->setTimeouts(array('invalid' => 0));
}

Expand Down Expand Up @@ -70,4 +71,28 @@ public function testLongTimeoutWaitsForElementToAppear()

$this->assertNotNull($element);
}

/**
* @dataProvider shortPageLoadTimeoutThrowsExceptionDataProvider
*/
public function testShortPageLoadTimeoutThrowsException(string $timeoutType)
{
$session = $this->getSession();
$driver = $session->getDriver();
\assert($driver instanceof Selenium2Driver);

$driver->setTimeouts(array($timeoutType => 500));

$this->expectException('\Behat\Mink\Exception\DriverException');
$this->expectExceptionMessage('Page failed to load: ');
$session->visit('https://www.webpagetest.org/'); // Use external URL, because it loads slower, then local.
}

public static function shortPageLoadTimeoutThrowsExceptionDataProvider(): array
{
return array(
'w3c style' => array('pageLoad'),
'non-w3c style' => array('page load'),
);
}
}

0 comments on commit e5ab502

Please sign in to comment.