Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harden window closing code upon session reset #27

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class WebdriverClassicDriver extends CoreDriver

private string $webDriverHost;

private ?string $initialWindowName = null;
private ?string $initialWindowHandle = null;

/**
* @param string $browserName One of 'edge', 'firefox', 'chrome' or any one of {@see WebDriverBrowserType} constants.
Expand All @@ -104,7 +104,7 @@ public function start(): void
try {
$this->webDriver = RemoteWebDriver::create($this->webDriverHost, $this->desiredCapabilities);
$this->applyTimeouts();
$this->initialWindowName = $this->getWindowName();
$this->initialWindowHandle = $this->getWebDriver()->getWindowHandle();
} catch (\Throwable $e) {
throw new DriverException("Could not start driver: {$e->getMessage()}", 0, $e);
}
Expand Down Expand Up @@ -136,16 +136,23 @@ public function stop(): void
*/
public function reset(): void
{
// switch to default window..
$webDriver = $this->getWebDriver();

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

// Close all windows except the initial one.
foreach ($webDriver->getWindowHandles() as $windowHandle) {
if ($windowHandle === $this->initialWindowHandle) {
continue;
}

$webDriver->switchTo()->window($windowHandle);
$webDriver->close();
$this->switchToWindow();
}

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

public function visit(string $url): void
Expand Down Expand Up @@ -175,13 +182,9 @@ public function back(): void

public function switchToWindow(?string $name = null): void
{
if ($name === null) {
$name = $this->initialWindowName;
}

if (is_string($name)) {
$name = $this->getWindowHandleFromName($name);
}
$name = $name === null
aik099 marked this conversation as resolved.
Show resolved Hide resolved
? $this->initialWindowHandle
: $this->getWindowHandleFromName($name);

$this->getWebDriver()->switchTo()->window((string)$name);
}
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): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof Would it make sense to put this test in driver-testsuite?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this test into the driver's test suite instead of the Mink driver test suite because the window closing behavior upon session reset is non-standard or advertised anywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest adding that in the driver testsuite as I think it makes sense for any driver supporting to open new windows to close them on reset.

We can update the phpdoc of the DriverInterface to suggest drivers to close extra windows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest adding that in the driver testsuite as I think it makes sense for any driver supporting to open new windows to close them on reset.

Done in the minkphp/driver-testsuite#96 .

We can update the phpdoc of the DriverInterface to suggest drivers to close extra windows.

@stof , please review my other PRs (7 by today). It's been over a month now, and I don't know if you've looked at them and they're OK to merge or I need to change something.

Also what can I do to speed up the review process?

Once that is arranged I can try creating a DriverInterface-related PR.

{
$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);
uuf6429 marked this conversation as resolved.
Show resolved Hide resolved

$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();
}
}
Loading