Skip to content

Commit

Permalink
Simplify custom tests (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 authored Nov 3, 2024
1 parent 9afb55f commit 61b12d0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 83 deletions.
3 changes: 3 additions & 0 deletions src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
26 changes: 6 additions & 20 deletions tests/Custom/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
40 changes: 40 additions & 0 deletions tests/Custom/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Mink\WebdriverClassicDriver\Tests\Custom;

use Mink\WebdriverClassicDriver\Tests\WebdriverClassicConfig;
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

class TestCase extends \PHPUnit\Framework\TestCase
{
protected WebdriverClassicDriver $driver;

use ExpectDeprecationTrait;

protected function setUp(): void
{
parent::setUp();

$this->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();
}
}
65 changes: 23 additions & 42 deletions tests/Custom/TimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
22 changes: 1 addition & 21 deletions tests/Custom/WebDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -88,7 +68,7 @@ public function testDriverCatchesUpstreamErrorsDuringStop(): void
public function testClassicDriverCanProvideBrowserName(): void
{
$this->assertSame(
WebdriverClassicConfig::getInstance()->getBrowserName(),
$this->getConfig()->getBrowserName(),
$this->driver->getBrowserName()
);
}
Expand Down

0 comments on commit 61b12d0

Please sign in to comment.