Skip to content

Commit

Permalink
Use initial window handle instead of name
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Apr 9, 2024
1 parent 3270459 commit 8198a7b
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 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->webDriver->getWindowHandle();

Check failure on line 107 in src/WebdriverClassicDriver.php

View workflow job for this annotation

GitHub Actions / Static analysis

Cannot call method getWindowHandle() on Facebook\WebDriver\Remote\RemoteWebDriver|null.
} catch (\Throwable $e) {
throw new DriverException("Could not start driver: {$e->getMessage()}", 0, $e);
}
Expand Down Expand Up @@ -138,17 +138,18 @@ public function reset(): void
{
$webDriver = $this->getWebDriver();

// switch to default window..
$initialWindow = substr((string)$this->initialWindowName, strlen(self::W3C_WINDOW_HANDLE_PREFIX));
$webDriver->switchTo()->window($initialWindow);
// Switch to default window.
$this->switchToWindow();

// ..and close all other windows
foreach ($webDriver->getWindowHandles() as $tempWindow) {
if ($tempWindow !== $initialWindow) {
$webDriver->switchTo()->window($tempWindow);
$webDriver->close();
$webDriver->switchTo()->window($initialWindow);
// 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();
}

$webDriver->manage()->deleteAllCookies();
Expand Down Expand Up @@ -181,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
? $this->initialWindowHandle
: $this->getWindowHandleFromName($name);

$this->getWebDriver()->switchTo()->window((string)$name);
}
Expand Down

0 comments on commit 8198a7b

Please sign in to comment.