diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index 36a9353c..b1ae858b 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -992,11 +992,13 @@ public function wait(int $timeout, string $condition) public function resizeWindow(int $width, int $height, ?string $name = null) { - $window = $this->getWebDriverSession()->window($name ?: 'current'); - \assert($window instanceof Window); - $window->postSize( - array('width' => $width, 'height' => $height) - ); + $this->withWindow($name, function () use ($width, $height) { + $window = $this->getWebDriverSession()->window('current'); + \assert($window instanceof Window); + $window->postSize( + array('width' => $width, 'height' => $height) + ); + }); } public function submitForm(string $xpath) @@ -1006,9 +1008,34 @@ public function submitForm(string $xpath) public function maximizeWindow(?string $name = null) { - $window = $this->getWebDriverSession()->window($name ?: 'current'); - \assert($window instanceof Window); - $window->maximize(); + $this->withWindow($name, function () { + $window = $this->getWebDriverSession()->window('current'); + \assert($window instanceof Window); + $window->maximize(); + }); + } + + private function withWindow(?string $name, callable $callback): void + { + if ($name === null) { + $callback(); + + return; + } + + $origName = $this->getWindowName(); + + try { + if ($origName !== $name) { + $this->switchToWindow($name); + } + + $callback(); + } finally { + if ($origName !== $name) { + $this->switchToWindow($origName); + } + } } /**