From 4bacd3003f38da1b2b2f00794f34cd82539e6f29 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 2 Nov 2024 19:20:02 +0200 Subject: [PATCH] Wrap WebDriver exception about failed to load page into DriverException Adjustments per review --- src/WebdriverClassicDriver.php | 8 +++++++- tests/Custom/TimeoutTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/WebdriverClassicDriver.php b/src/WebdriverClassicDriver.php index b251678..cd30244 100644 --- a/src/WebdriverClassicDriver.php +++ b/src/WebdriverClassicDriver.php @@ -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; @@ -155,7 +157,11 @@ public function reset(): void public function visit(string $url): void { - $this->getWebDriver()->navigate()->to($url); + try { + $this->getWebDriver()->navigate()->to($url); + } catch (TimeoutException|ScriptTimeoutException $e) { + throw new DriverException('Page failed to load: ' . $e->getMessage(), 0, $e); + } } public function getCurrentUrl(): string diff --git a/tests/Custom/TimeoutTest.php b/tests/Custom/TimeoutTest.php index 50ff1ed..5aa11b7 100644 --- a/tests/Custom/TimeoutTest.php +++ b/tests/Custom/TimeoutTest.php @@ -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]); } @@ -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(['page' => 500]); + + $this->expectException(DriverException::class); + $this->expectExceptionMessage('Page failed to load: '); + $session->visit($this->pathTo('/page_load.php?sleep=2')); + } }