I have a problem using “WebUI.closeWindowIndex(1)” It throws an error - “Unable to close window with index 1” and " invalid session id: session deleted as the browser has closed the connection"
I’ve asked the Katalon studio assistant for hints and help and nothing was working, it simply fails. That is happening on macOS and it seems that there is no problem with this on Windows. Is this a possible bug for macOS?
Katalon Studio version -10.1.1 and MacOS - Version 15.3.2
I don’t know why your code at present does not work and I will leave that for Katalon staff. However, reading it I am wondering why about???
You switch to window one, then count how many windows there are and then switch to window one again and then try to close it. Why don’t you just try to close window one after taking your screenshot? How about comment out your code from line 115 to before line 118 of the image above, but leave line 118 and see if that does what you want.
Edit: And, how about putting in some detection statement that you are actually on the page 2 with something like below:
WebUI.switchToWindowIndex("1")
WebUI.delay(2)
WebUI.waitForElementVisible(findTestObject(...), 10)
WebUI.verifyElementVisible(findTestObject(...), FailureHandling.STOP_ON_FAILURE)
or
WebUI.verifyElementPresent(findTestObject(...), 10, FailureHandling.STOP_ON_FAILURE)
or
WebUI.verifyElementText(findTestObject(...), "your text", FailureHandling.STOP_ON_FAILURE)
WebUI.closeWindowIndex("1")
Edit2: If you want another method to close your tab/window, then how about the below?
Close tab:
When you use “Robot”, you cannot be using your keyboard or mouse while the test runs, as you would be interfering with the Robot trying to do the below. Additionally, you have to be on the tab/window that you want closed.
import java.awt.Robot
import java.awt.event.KeyEvent
//WebUI.delay(0.5)
"close this tab"
Robot robot = new Robot()
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_SHIFT)
robot.keyPress(KeyEvent.VK_W)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_SHIFT)
robot.keyRelease(KeyEvent.VK_W)
// Switch back to main page
WebUI.switchToDefaultContent()
Check the number of open windows to ensure index 1 exists:
int windowCount = WebDriverFactory.getWebDriver().getWindowHandles().size()
println("Total open windows: $windowCount")
if (windowCount > 1) {
WebUI.closeWindowIndex(1) // Try 0 or other indices if needed
}
2. Switch to the Target Window First
Ensure focus is on the correct window/tab before closing:
WebUI.switchToWindowIndex(1)
WebUI.delay(2) // Let the window stabilize
WebUI.closeWindowIndex(1)
3. Use Browser-Specific Workarounds
For Safari: Enable automatic window management in Safari settings.
Go to Safari > Preferences > Tabs and disable Prevent Safari from closing windows with multiple tabs.
For Chrome/Edge: Use explicit window handles instead of indices:
It doesn’t matter if there is a count for how many windows are open or not, that is absolutely right. That was simply a suggestion from the Katalon studio assistant, so I left it like that.
Anyhow before was simply - WebUI.closeWindowIndex(1) and I never have any issue with that.
I also tried your suggestion, but it didn’t close it.
import java.awt.Robot
import java.awt.event.KeyEvent
//WebUI.delay(0.5)
"close this tab"
Robot robot = new Robot()
robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_W)
robot.keyRelease(KeyEvent.VK_CONTROL)
robot.keyRelease(KeyEvent.VK_W)
// Switch back to main page
WebUI.switchToDefaultContent()
On Mac, it is supposed to be CMD + W, to close a browser’s tab. I have amended my earlier Robot command.
Close tab:
import java.awt.Robot
import java.awt.event.KeyEvent
//WebUI.delay(0.5)
"close this tab"
Robot robot = new Robot()
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_W)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_W)
// Switch back to main page
WebUI.switchToDefaultContent()
Also, I wanted to get across with using the verify statements was to “prove” that you were actually on the second tab before you close it. Taking the picture means you have to review the picture after running your test that your test did what you wanted. The verify statements do that as well but with the failure added, you will know immediately when it did not.
As you said - CMD + SHIFT + W is for closing the browser, not the second tab(windows index 1) which is the goal.
I tried to check if this would work anyway, but nothing happened. I try also if only with COMMAND + W would work, but this also does not.
Is it possible that all of this is because the tab contains only a pdf?