Skip to content

Commit

Permalink
Wrap WebDriver exception about failed to load page into DriverException
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Nov 2, 2024
1 parent eba0ed4 commit dca38d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Behat\Mink\Exception\DriverException;
use Facebook\WebDriver\Exception\NoSuchCookieException;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\ScriptTimeoutException;
use Facebook\WebDriver\Exception\TimeoutException;
use Facebook\WebDriver\Exception\UnsupportedOperationException;
use Facebook\WebDriver\Exception\WebDriverException;
use Facebook\WebDriver\Remote\DesiredCapabilities;
Expand Down Expand Up @@ -155,7 +157,15 @@ public function reset(): void

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

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

$this->expectException(DriverException::class);
$this->expectExceptionMessage('Invalid timeout type: invalid');

$driver->setTimeouts(['invalid' => 0]);
}
Expand Down Expand Up @@ -64,4 +65,17 @@ public function testLongTimeoutWaitsForElementToAppear(): void

$this->assertNotNull($element);
}

public function testShortPageLoadTimeoutThrowsException(): void
{
$session = $this->getSession();
$driver = $session->getDriver();
\assert($driver instanceof WebdriverClassicDriver);

$driver->setTimeouts(array('page' => 1)); // Use 1ms timeout to avoid any successful page load.

$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.
}
}

0 comments on commit dca38d8

Please sign in to comment.