Skip to content

Commit 61b12d0

Browse files
authored
Simplify custom tests (#43)
1 parent 9afb55f commit 61b12d0

File tree

5 files changed

+73
-83
lines changed

5 files changed

+73
-83
lines changed

src/WebdriverClassicDriver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,12 +956,15 @@ private function applyTimeouts(): void
956956
case 'script':
957957
$timeouts->setScriptTimeout($param / 1000);
958958
break;
959+
959960
case 'implicit':
960961
$timeouts->implicitlyWait($param / 1000);
961962
break;
963+
962964
case 'page':
963965
$timeouts->pageLoadTimeout($param / 1000);
964966
break;
967+
965968
default:
966969
throw new DriverException("Invalid timeout type: $type");
967970
}

tests/Custom/SessionTest.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,20 @@
22

33
namespace Mink\WebdriverClassicDriver\Tests\Custom;
44

5-
use Behat\Mink\Exception\DriverException;
6-
use Behat\Mink\Tests\Driver\TestCase;
7-
use Mink\WebdriverClassicDriver\Tests\WebdriverClassicConfig;
8-
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;
9-
105
class SessionTest extends TestCase
116
{
12-
protected function setUp(): void
7+
public function testNewDriverShouldNotHaveSessionId(): void
138
{
14-
parent::setUp();
9+
$driver = $this->driver;
1510

16-
$this->getSession()->start();
11+
$this->assertNull($driver->getWebDriverSessionId(), 'Non-started session should not have an ID');
1712
}
1813

19-
protected function tearDown(): void
14+
public function testStartedDriverShouldHaveSessionId(): void
2015
{
21-
$this->getSession()->stop();
22-
23-
parent::tearDown();
24-
}
16+
$driver = $this->driver;
17+
$driver->start();
2518

26-
public function testGetWebDriverSessionId(): void
27-
{
28-
$driver = $this->getSession()->getDriver();
29-
assert($driver instanceof WebdriverClassicDriver);
3019
$this->assertNotEmpty($driver->getWebDriverSessionId(), 'Started session should have an ID');
31-
32-
$driver = new WebdriverClassicDriver();
33-
$this->assertNull($driver->getWebDriverSessionId(), 'Non-started session should not have an ID');
3420
}
3521
}

tests/Custom/TestCase.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Mink\WebdriverClassicDriver\Tests\Custom;
4+
5+
use Mink\WebdriverClassicDriver\Tests\WebdriverClassicConfig;
6+
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;
7+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
8+
9+
class TestCase extends \PHPUnit\Framework\TestCase
10+
{
11+
protected WebdriverClassicDriver $driver;
12+
13+
use ExpectDeprecationTrait;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->driver = $this->getConfig()->createDriver();
20+
}
21+
22+
protected function tearDown(): void
23+
{
24+
parent::tearDown();
25+
26+
if ($this->driver->isStarted()) {
27+
$this->driver->stop();
28+
}
29+
}
30+
31+
protected function pathTo(string $path): string
32+
{
33+
return rtrim($this->getConfig()->getWebFixturesUrl(), '/') . '/' . ltrim($path, '/');
34+
}
35+
36+
protected function getConfig(): WebdriverClassicConfig
37+
{
38+
return WebdriverClassicConfig::getInstance();
39+
}
40+
}

tests/Custom/TimeoutTest.php

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,60 @@
33
namespace Mink\WebdriverClassicDriver\Tests\Custom;
44

55
use Behat\Mink\Exception\DriverException;
6-
use Behat\Mink\Tests\Driver\TestCase;
7-
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;
86

97
class TimeoutTest extends TestCase
108
{
11-
/**
12-
* @after
13-
*/
14-
protected function resetSessions(): void
9+
protected function tearDown(): void
1510
{
16-
$session = $this->getSession();
17-
$driver = $this->getSession()->getDriver();
18-
assert($driver instanceof WebdriverClassicDriver);
11+
$this->driver->setTimeouts([
12+
'script' => 30000,
13+
'page' => 300000,
14+
'implicit' => 0,
15+
]);
1916

20-
// Stop the session instead of only resetting it, as timeouts are not reset (they are configuring the session itself)
21-
if ($session->isStarted()) {
22-
$session->stop();
23-
}
24-
25-
// Reset the array of timeouts to avoid impacting other tests
26-
$driver->setTimeouts([]);
27-
28-
parent::resetSessions();
17+
parent::tearDown();
2918
}
3019

3120
public function testInvalidTimeoutSettingThrowsException(): void
3221
{
33-
$this->getSession()->start();
34-
$driver = $this->getSession()->getDriver();
35-
assert($driver instanceof WebdriverClassicDriver);
22+
$this->driver->start();
3623

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

40-
$driver->setTimeouts(['invalid' => 0]);
27+
$this->driver->setTimeouts(['invalid' => 0]);
4128
}
4229

4330
public function testShortTimeoutDoesNotWaitForElementToAppear(): void
4431
{
45-
$driver = $this->getSession()->getDriver();
46-
assert($driver instanceof WebdriverClassicDriver);
47-
$driver->setTimeouts(['implicit' => 0]);
32+
$this->driver->start();
33+
$this->driver->setTimeouts(['implicit' => 0]);
4834

49-
$this->getSession()->visit($this->pathTo('/js_test.html'));
50-
$this->findById('waitable')->click();
51-
$element = $this->getSession()->getPage()->find('css', '#waitable > div');
35+
$this->driver->visit($this->pathTo('/js_test.html'));
36+
$this->driver->click('//div[@id="waitable"]');
5237

53-
$this->assertNull($element);
38+
$this->assertEmpty($this->driver->getText('//div[@id="waitable"]'));
5439
}
5540

5641
public function testLongTimeoutWaitsForElementToAppear(): void
5742
{
58-
$driver = $this->getSession()->getDriver();
59-
assert($driver instanceof WebdriverClassicDriver);
60-
$driver->setTimeouts(['implicit' => 5000]);
43+
$this->driver->start();
44+
$this->driver->setTimeouts(['implicit' => 5000]);
6145

62-
$this->getSession()->visit($this->pathTo('/js_test.html'));
63-
$this->findById('waitable')->click();
64-
$element = $this->getSession()->getPage()->find('css', '#waitable > div');
46+
$this->driver->visit($this->pathTo('/js_test.html'));
47+
$this->driver->click('//div[@id="waitable"]');
6548

66-
$this->assertNotNull($element);
49+
$this->assertNotEmpty($this->driver->getText('//div[@id="waitable"]/div'));
6750
}
6851

6952
public function testShortPageLoadTimeoutThrowsException(): void
7053
{
71-
$session = $this->getSession();
72-
$driver = $session->getDriver();
73-
\assert($driver instanceof WebdriverClassicDriver);
74-
75-
$driver->setTimeouts(['page' => 500]);
54+
$this->driver->start();
55+
$this->driver->setTimeouts(['page' => 500]);
7656

7757
$this->expectException(DriverException::class);
7858
$this->expectExceptionMessage('Page failed to load: ');
79-
$session->visit($this->pathTo('/page_load.php?sleep=2'));
59+
60+
$this->driver->visit($this->pathTo('/page_load.php?sleep=2'));
8061
}
8162
}

tests/Custom/WebDriverTest.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,10 @@
33
namespace Mink\WebdriverClassicDriver\Tests\Custom;
44

55
use Behat\Mink\Exception\DriverException;
6-
use Mink\WebdriverClassicDriver\Tests\WebdriverClassicConfig;
76
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;
8-
use PHPUnit\Framework\TestCase;
97

108
class WebDriverTest extends TestCase
119
{
12-
private WebdriverClassicDriver $driver;
13-
14-
protected function setUp(): void
15-
{
16-
parent::setUp();
17-
18-
$this->driver = WebdriverClassicConfig::getInstance()->createDriver();
19-
}
20-
21-
protected function tearDown(): void
22-
{
23-
if ($this->driver->isStarted()) {
24-
$this->driver->stop();
25-
}
26-
27-
parent::tearDown();
28-
}
29-
3010
public function testDriverMustBeStartedBeforeUse(): void
3111
{
3212
$this->expectException(DriverException::class);
@@ -88,7 +68,7 @@ public function testDriverCatchesUpstreamErrorsDuringStop(): void
8868
public function testClassicDriverCanProvideBrowserName(): void
8969
{
9070
$this->assertSame(
91-
WebdriverClassicConfig::getInstance()->getBrowserName(),
71+
$this->getConfig()->getBrowserName(),
9272
$this->driver->getBrowserName()
9373
);
9474
}

0 commit comments

Comments
 (0)