Closing windows index is failing on MacOS

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

1 Like

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()
1 Like

1. Verify Open Windows Before Closing

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:
def handles = WebDriverFactory.getWebDriver().getWindowHandles()
WebUI.switchToWindowUrl("https://your-target-url.com")
WebUI.closeWindowUrl("https://your-target-url.com")

4. Kill Zombie Browser Processes

macOS may leave orphaned browser instances. Add a teardown script to force-terminate processes:

@AfterTestCase
def closeBrowsers() {
    WebUI.closeBrowser()
    // Force-kill macOS processes (Chrome example)
    "pkill -f chromedriver".execute()
    "pkill -f Google\\ Chrome".execute()
}

5. Use closeWindowTitle or closeWindowUrl

Avoid indices altogether if unstable:

WebUI.closeWindowTitle("Your Window Title")
// OR
WebUI.closeWindowUrl("https://example.com")

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?

can you find open window count and close them one by one or close based on window title if its unique

I don’t understand what you mean by find open window.
If it can “switch to window index 1” there is an open window with index 1