diff --git a/.github/workflows/webdriver-chrome-headless.yml b/.github/workflows/webdriver-chrome-headless.yml index 7c43517..ab7cc78 100644 --- a/.github/workflows/webdriver-chrome-headless.yml +++ b/.github/workflows/webdriver-chrome-headless.yml @@ -2,9 +2,9 @@ name: Chrome Headless Tests on: push: - branches: [ master ] + branches: [ "1.x" ] pull_request: - branches: [ master ] + branches: [ "1.x" ] jobs: build: diff --git a/.github/workflows/webdriver-chrome.yml b/.github/workflows/webdriver-chrome.yml index 3c82039..5ffb4d8 100644 --- a/.github/workflows/webdriver-chrome.yml +++ b/.github/workflows/webdriver-chrome.yml @@ -2,9 +2,9 @@ name: Chrome Tests on: push: - branches: [ master ] + branches: [ "1.x" ] pull_request: - branches: [ master ] + branches: [ "1.x" ] jobs: build: diff --git a/.github/workflows/webdriver-firefox.yml b/.github/workflows/webdriver-firefox.yml index 2cb82a7..72cffb2 100644 --- a/.github/workflows/webdriver-firefox.yml +++ b/.github/workflows/webdriver-firefox.yml @@ -2,9 +2,9 @@ name: Firefox Tests on: push: - branches: [ master ] + branches: [ "1.x" ] pull_request: - branches: [ master ] + branches: [ "1.x" ] jobs: build: diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index d6a0451..c6f4832 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -3454,7 +3454,11 @@ public function seeNumberOfTabs($number) */ public function closeTab() { + $currentTab = $this->webDriver->getWindowHandle(); $prevTab = $this->getRelativeTabHandle(-1); + if ($prevTab === $currentTab) { + throw new ModuleException($this, 'Will not close the last open tab'); + } $this->webDriver->close(); $this->webDriver->switchTo()->window($prevTab); } @@ -3503,8 +3507,12 @@ protected function getRelativeTabHandle($offset) } $handle = $this->webDriver->getWindowHandle(); $handles = $this->webDriver->getWindowHandles(); - $idx = array_search($handle, $handles); - return $handles[($idx + $offset) % count($handles)]; + $currentHandleIdx = array_search($handle, $handles); + $newHandleIdx = ($currentHandleIdx + $offset) % count($handles); + if ($newHandleIdx < 0) { + $newHandleIdx = count($handles) + $newHandleIdx; + } + return $handles[$newHandleIdx]; } /** diff --git a/tests/web/WebDriverTest.php b/tests/web/WebDriverTest.php index 7678de3..f2eeff4 100644 --- a/tests/web/WebDriverTest.php +++ b/tests/web/WebDriverTest.php @@ -1036,6 +1036,9 @@ public function testBrowserTabs() $this->module->switchToNextTab(2); $this->module->seeInCurrentUrl('example1'); $this->module->seeNumberOfTabs(3); + $this->module->closeTab(); + $this->module->seeNumberOfTabs(2); + $this->module->closeTab(); } public function testPerformOnWithArray()