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
Changes from 5 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
32 changes: 16 additions & 16 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,20 @@ public function stop(): void
*/
public function reset(): void
{
// 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());
$webDriver = $this->getWebDriver();

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

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

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

public function visit(string $url): void
Expand Down Expand Up @@ -175,13 +179,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
Loading