Skip to content

Commit c20b757

Browse files
authored
Merge pull request #108 from webmaster777/negative-tab-offset-crashes
Negative tab offset crashes
2 parents e11ba49 + 6374d5f commit c20b757

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Codeception/Module/WebDriver.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,7 +3525,11 @@ public function seeNumberOfTabs(int $number): void
35253525
*/
35263526
public function closeTab(): void
35273527
{
3528+
$currentTab = $this->webDriver->getWindowHandle();
35283529
$prevTab = $this->getRelativeTabHandle(-1);
3530+
if ($prevTab === $currentTab) {
3531+
throw new ModuleException($this, 'Will not close the last open tab');
3532+
}
35293533
$this->webDriver->close();
35303534
$this->webDriver->switchTo()->window($prevTab);
35313535
}
@@ -3573,8 +3577,12 @@ protected function getRelativeTabHandle($offset)
35733577

35743578
$handle = $this->webDriver->getWindowHandle();
35753579
$handles = $this->webDriver->getWindowHandles();
3576-
$idx = array_search($handle, $handles);
3577-
return $handles[($idx + $offset) % count($handles)];
3580+
$currentHandleIdx = array_search($handle, $handles);
3581+
$newHandleIdx = ($currentHandleIdx + $offset) % count($handles);
3582+
if ($newHandleIdx < 0) {
3583+
$newHandleIdx = count($handles) + $newHandleIdx;
3584+
}
3585+
return $handles[$newHandleIdx];
35783586
}
35793587

35803588
/**

tests/web/WebDriverTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,9 @@ public function testBrowserTabs()
10401040
$this->module->switchToNextTab(2);
10411041
$this->module->seeInCurrentUrl('example1');
10421042
$this->module->seeNumberOfTabs(3);
1043+
$this->module->closeTab();
1044+
$this->module->seeNumberOfTabs(2);
1045+
$this->module->closeTab();
10431046
}
10441047

10451048
public function testPerformOnWithArray()

0 commit comments

Comments
 (0)