From e0aa26cad9b2786f2c29acac3f3a71ac5c5d0d23 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 27 Jul 2022 11:06:49 +0200 Subject: [PATCH 1/6] [module-webdriver] fix type error in PHP 8.1 when converting ms to sec (#103) * [module-webdriver] fix type error in PHP 8.1 when converting milliseconds to seconds * simplify using intval Co-authored-by: Patrick Mac Gregor --- src/Codeception/Module/WebDriver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index c11c5b3..2d8a385 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -698,7 +698,7 @@ protected function formatLogEntries(array $logEntries): string foreach ($logEntries as $logEntry) { // Timestamp is in milliseconds, but date() requires seconds. - $time = date('H:i:s', $logEntry['timestamp'] / 1000) . + $time = date('H:i:s', intval($logEntry['timestamp'] / 1000)) . // Append the milliseconds to the end of the time string '.' . ($logEntry['timestamp'] % 1000); $formattedLogs .= "{$time} {$logEntry['level']} - {$logEntry['message']}\n"; @@ -718,7 +718,7 @@ protected function logJSErrors(ScenarioDriven $test, array $browserLogEntries): && $this->isJSError($logEntry['level'], $logEntry['message']) ) { // Timestamp is in milliseconds, but date() requires seconds. - $time = date('H:i:s', $logEntry['timestamp'] / 1000) . + $time = date('H:i:s', intval($logEntry['timestamp'] / 1000)) . // Append the milliseconds to the end of the time string '.' . ($logEntry['timestamp'] % 1000); $test->getScenario()->comment("{$time} {$logEntry['level']} - {$logEntry['message']}"); From 0eaf97e578bb8d2c4fbf0d2564a8a4eadde19287 Mon Sep 17 00:00:00 2001 From: Meindert-Jan Kroese Date: Thu, 8 Sep 2022 10:14:01 +0200 Subject: [PATCH 2/6] prevent negative array-offset getting The modulo operation can result in a negative number, while the list of window handles is always a 0-based array --- src/Codeception/Module/WebDriver.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index 2d8a385..1681bc8 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -3553,8 +3553,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]; } /** From 9919a2a76654a9cd86de43523cf069c458e3bb03 Mon Sep 17 00:00:00 2001 From: Meindert-Jan Kroese Date: Thu, 8 Sep 2022 10:15:13 +0200 Subject: [PATCH 3/6] prevent switching to a tab if the last tab is closed --- src/Codeception/Module/WebDriver.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index 1681bc8..44ae036 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -3505,9 +3505,12 @@ public function seeNumberOfTabs($number): void */ public function closeTab(): void { + $currentTab = $this->webDriver->getWindowHandle(); $prevTab = $this->getRelativeTabHandle(-1); $this->webDriver->close(); - $this->webDriver->switchTo()->window($prevTab); + if ($prevTab !== $currentTab) { + $this->webDriver->switchTo()->window($prevTab); + } } /** From 1563c118a15f99613e2b37d112c6ba396d8d45d2 Mon Sep 17 00:00:00 2001 From: Meindert-Jan Kroese Date: Thu, 8 Sep 2022 10:17:35 +0200 Subject: [PATCH 4/6] Test: close first tab and close all tabs --- tests/web/WebDriverTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/web/WebDriverTest.php b/tests/web/WebDriverTest.php index d74d106..cde61cd 100644 --- a/tests/web/WebDriverTest.php +++ b/tests/web/WebDriverTest.php @@ -1024,6 +1024,10 @@ 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(); + $this->module->closeTab(); } public function testPerformOnWithArray() From f91575c0a94a93d2267263dc1ed3a4ea48c1932f Mon Sep 17 00:00:00 2001 From: Meindert-Jan Kroese Date: Thu, 8 Sep 2022 10:41:01 +0300 Subject: [PATCH 5/6] prevent closing the final tab, as it will destroy the session --- src/Codeception/Module/WebDriver.php | 7 ++++--- tests/web/WebDriverTest.php | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index 44ae036..da96c29 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -3507,10 +3507,11 @@ public function closeTab(): void { $currentTab = $this->webDriver->getWindowHandle(); $prevTab = $this->getRelativeTabHandle(-1); - $this->webDriver->close(); - if ($prevTab !== $currentTab) { - $this->webDriver->switchTo()->window($prevTab); + if ($prevTab === $currentTab) { + throw new ModuleException($this, 'Will not close the last open tab'); } + $this->webDriver->close(); + $this->webDriver->switchTo()->window($prevTab); } /** diff --git a/tests/web/WebDriverTest.php b/tests/web/WebDriverTest.php index cde61cd..8048b49 100644 --- a/tests/web/WebDriverTest.php +++ b/tests/web/WebDriverTest.php @@ -1027,7 +1027,6 @@ public function testBrowserTabs() $this->module->closeTab(); $this->module->seeNumberOfTabs(2); $this->module->closeTab(); - $this->module->closeTab(); } public function testPerformOnWithArray() From 8f555fa9037ac0f8dae62b2d6ced5cee31811b28 Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Mon, 12 Sep 2022 08:08:38 +0300 Subject: [PATCH 6/6] Github actions: Run jobs on 2.0 branch --- .github/workflows/webdriver-chrome-headless.yml | 4 ++-- .github/workflows/webdriver-chrome.yml | 4 ++-- .github/workflows/webdriver-firefox.yml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/webdriver-chrome-headless.yml b/.github/workflows/webdriver-chrome-headless.yml index 7c43517..ac4a224 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: [ "2.0" ] pull_request: - branches: [ master ] + branches: [ "2.0" ] jobs: build: diff --git a/.github/workflows/webdriver-chrome.yml b/.github/workflows/webdriver-chrome.yml index 3c82039..e346edf 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: [ "2.0" ] pull_request: - branches: [ master ] + branches: [ "2.0" ] jobs: build: diff --git a/.github/workflows/webdriver-firefox.yml b/.github/workflows/webdriver-firefox.yml index 2cb82a7..4cafa58 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: [ "2.0" ] pull_request: - branches: [ master ] + branches: [ "2.0" ] jobs: build: