Skip to content

Commit

Permalink
Harden window closing code upon session reset
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Apr 7, 2024
1 parent 71c242b commit 870aa71
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,19 @@ public function reset(): void
{
// switch to default window..
$this->switchToWindow();
$actualInitialWindowName = $this->getWindowName(); // Account for initial window rename.
$webDriver = $this->getWebDriver();

// ..and close all other windows
foreach ($this->getWindowNames() as $name) {
if ($name !== $this->initialWindowName) {
$this->withWindow($name, fn() => $this->getWebDriver()->close());
if ($name !== $actualInitialWindowName) {
$this->switchToWindow($name);
$webDriver->close();
$this->switchToWindow();
}
}

$this->getWebDriver()->manage()->deleteAllCookies();
$webDriver->manage()->deleteAllCookies();
}

public function visit(string $url): void
Expand Down
62 changes: 62 additions & 0 deletions tests/Custom/SessionResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Mink\WebdriverClassDriver\Tests\Custom;

use Behat\Mink\Tests\Driver\TestCase;

class SessionResetTest extends TestCase
{
/**
* @dataProvider initialWindowNameDataProvider
*/
public function testSessionResetClosesWindows(?string $initialWindowName)

Check failure on line 12 in tests/Custom/SessionResetTest.php

View workflow job for this annotation

GitHub Actions / Static analysis

Method Mink\WebdriverClassDriver\Tests\Custom\SessionResetTest::testSessionResetClosesWindows() has no return type specified.
{
$session = $this->getSession();
$session->visit($this->pathTo('/window.html'));

if (null !== $initialWindowName) {
$session->executeScript('window.name = "'.$initialWindowName.'";');
}

$page = $session->getPage();

$page->clickLink('Popup #1');
$page->clickLink('Popup #2');

$expectedInitialWindowName = $session->evaluateScript('window.name');

$windowNames = $session->getWindowNames();
$this->assertCount(3, $windowNames);

$session->reset();

$windowNames = $session->getWindowNames();
$this->assertCount(1, $windowNames);

$actualInitialWindowName = $session->evaluateScript('window.name');
$this->assertEquals($expectedInitialWindowName, $actualInitialWindowName);
}

public static function initialWindowNameDataProvider(): array
{
return array(
'no name' => array(null),
'non-empty name' => array('initial-window'),
);
}

/**
* @after
*/
protected function resetSessions()
{
$session = $this->getSession();

// Stop the session instead of resetting, because resetting behavior is being tested.
if ($session->isStarted()) {
$session->stop();
}

parent::resetSessions();
}
}

0 comments on commit 870aa71

Please sign in to comment.