diff --git a/src/WebdriverClassicDriver.php b/src/WebdriverClassicDriver.php index 62a6b2b..e185940 100644 --- a/src/WebdriverClassicDriver.php +++ b/src/WebdriverClassicDriver.php @@ -956,12 +956,15 @@ private function applyTimeouts(): void case 'script': $timeouts->setScriptTimeout($param / 1000); break; + case 'implicit': $timeouts->implicitlyWait($param / 1000); break; + case 'page': $timeouts->pageLoadTimeout($param / 1000); break; + default: throw new DriverException("Invalid timeout type: $type"); } diff --git a/tests/Custom/SessionTest.php b/tests/Custom/SessionTest.php index 7756d27..dabe04d 100644 --- a/tests/Custom/SessionTest.php +++ b/tests/Custom/SessionTest.php @@ -2,34 +2,20 @@ namespace Mink\WebdriverClassicDriver\Tests\Custom; -use Behat\Mink\Exception\DriverException; -use Behat\Mink\Tests\Driver\TestCase; -use Mink\WebdriverClassicDriver\Tests\WebdriverClassicConfig; -use Mink\WebdriverClassicDriver\WebdriverClassicDriver; - class SessionTest extends TestCase { - protected function setUp(): void + public function testNewDriverShouldNotHaveSessionId(): void { - parent::setUp(); + $driver = $this->driver; - $this->getSession()->start(); + $this->assertNull($driver->getWebDriverSessionId(), 'Non-started session should not have an ID'); } - protected function tearDown(): void + public function testStartedDriverShouldHaveSessionId(): void { - $this->getSession()->stop(); - - parent::tearDown(); - } + $driver = $this->driver; + $driver->start(); - public function testGetWebDriverSessionId(): void - { - $driver = $this->getSession()->getDriver(); - assert($driver instanceof WebdriverClassicDriver); $this->assertNotEmpty($driver->getWebDriverSessionId(), 'Started session should have an ID'); - - $driver = new WebdriverClassicDriver(); - $this->assertNull($driver->getWebDriverSessionId(), 'Non-started session should not have an ID'); } } diff --git a/tests/Custom/TestCase.php b/tests/Custom/TestCase.php new file mode 100644 index 0000000..5592231 --- /dev/null +++ b/tests/Custom/TestCase.php @@ -0,0 +1,40 @@ +driver = $this->getConfig()->createDriver(); + } + + protected function tearDown(): void + { + parent::tearDown(); + + if ($this->driver->isStarted()) { + $this->driver->stop(); + } + } + + protected function pathTo(string $path): string + { + return rtrim($this->getConfig()->getWebFixturesUrl(), '/') . '/' . ltrim($path, '/'); + } + + protected function getConfig(): WebdriverClassicConfig + { + return WebdriverClassicConfig::getInstance(); + } +} diff --git a/tests/Custom/TimeoutTest.php b/tests/Custom/TimeoutTest.php index 5aa11b7..f0c1914 100644 --- a/tests/Custom/TimeoutTest.php +++ b/tests/Custom/TimeoutTest.php @@ -3,79 +3,60 @@ namespace Mink\WebdriverClassicDriver\Tests\Custom; use Behat\Mink\Exception\DriverException; -use Behat\Mink\Tests\Driver\TestCase; -use Mink\WebdriverClassicDriver\WebdriverClassicDriver; class TimeoutTest extends TestCase { - /** - * @after - */ - protected function resetSessions(): void + protected function tearDown(): void { - $session = $this->getSession(); - $driver = $this->getSession()->getDriver(); - assert($driver instanceof WebdriverClassicDriver); + $this->driver->setTimeouts([ + 'script' => 30000, + 'page' => 300000, + 'implicit' => 0, + ]); - // Stop the session instead of only resetting it, as timeouts are not reset (they are configuring the session itself) - if ($session->isStarted()) { - $session->stop(); - } - - // Reset the array of timeouts to avoid impacting other tests - $driver->setTimeouts([]); - - parent::resetSessions(); + parent::tearDown(); } public function testInvalidTimeoutSettingThrowsException(): void { - $this->getSession()->start(); - $driver = $this->getSession()->getDriver(); - assert($driver instanceof WebdriverClassicDriver); + $this->driver->start(); $this->expectException(DriverException::class); $this->expectExceptionMessage('Invalid timeout type: invalid'); - $driver->setTimeouts(['invalid' => 0]); + $this->driver->setTimeouts(['invalid' => 0]); } public function testShortTimeoutDoesNotWaitForElementToAppear(): void { - $driver = $this->getSession()->getDriver(); - assert($driver instanceof WebdriverClassicDriver); - $driver->setTimeouts(['implicit' => 0]); + $this->driver->start(); + $this->driver->setTimeouts(['implicit' => 0]); - $this->getSession()->visit($this->pathTo('/js_test.html')); - $this->findById('waitable')->click(); - $element = $this->getSession()->getPage()->find('css', '#waitable > div'); + $this->driver->visit($this->pathTo('/js_test.html')); + $this->driver->click('//div[@id="waitable"]'); - $this->assertNull($element); + $this->assertEmpty($this->driver->getText('//div[@id="waitable"]')); } public function testLongTimeoutWaitsForElementToAppear(): void { - $driver = $this->getSession()->getDriver(); - assert($driver instanceof WebdriverClassicDriver); - $driver->setTimeouts(['implicit' => 5000]); + $this->driver->start(); + $this->driver->setTimeouts(['implicit' => 5000]); - $this->getSession()->visit($this->pathTo('/js_test.html')); - $this->findById('waitable')->click(); - $element = $this->getSession()->getPage()->find('css', '#waitable > div'); + $this->driver->visit($this->pathTo('/js_test.html')); + $this->driver->click('//div[@id="waitable"]'); - $this->assertNotNull($element); + $this->assertNotEmpty($this->driver->getText('//div[@id="waitable"]/div')); } public function testShortPageLoadTimeoutThrowsException(): void { - $session = $this->getSession(); - $driver = $session->getDriver(); - \assert($driver instanceof WebdriverClassicDriver); - - $driver->setTimeouts(['page' => 500]); + $this->driver->start(); + $this->driver->setTimeouts(['page' => 500]); $this->expectException(DriverException::class); $this->expectExceptionMessage('Page failed to load: '); - $session->visit($this->pathTo('/page_load.php?sleep=2')); + + $this->driver->visit($this->pathTo('/page_load.php?sleep=2')); } } diff --git a/tests/Custom/WebDriverTest.php b/tests/Custom/WebDriverTest.php index 0411cfc..1c4ff40 100644 --- a/tests/Custom/WebDriverTest.php +++ b/tests/Custom/WebDriverTest.php @@ -3,30 +3,10 @@ namespace Mink\WebdriverClassicDriver\Tests\Custom; use Behat\Mink\Exception\DriverException; -use Mink\WebdriverClassicDriver\Tests\WebdriverClassicConfig; use Mink\WebdriverClassicDriver\WebdriverClassicDriver; -use PHPUnit\Framework\TestCase; class WebDriverTest extends TestCase { - private WebdriverClassicDriver $driver; - - protected function setUp(): void - { - parent::setUp(); - - $this->driver = WebdriverClassicConfig::getInstance()->createDriver(); - } - - protected function tearDown(): void - { - if ($this->driver->isStarted()) { - $this->driver->stop(); - } - - parent::tearDown(); - } - public function testDriverMustBeStartedBeforeUse(): void { $this->expectException(DriverException::class); @@ -88,7 +68,7 @@ public function testDriverCatchesUpstreamErrorsDuringStop(): void public function testClassicDriverCanProvideBrowserName(): void { $this->assertSame( - WebdriverClassicConfig::getInstance()->getBrowserName(), + $this->getConfig()->getBrowserName(), $this->driver->getBrowserName() ); }