# closeWindowIndex Issue

**URL:** <https://forum.katalon.com/t/closewindowindex-issue/109433>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [December 4, 2023, 9:44am UTC](https://forum.katalon.com/t/closewindowindex-issue/109433 "2023-12-04T09:44:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sdhongadi](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sdhongadi/32/16104_2.png) [@Sdhongadi](https://forum.katalon.com/u/Sdhongadi)\
**Post date:** [December 4, 2023, 9:44am UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/1 "2023-12-04T09:44:44Z")

</div>

Hi Team,

In one of my Test case I am opening a hyperlink and then switching to a new window for validations. Once done am switching back to Main window and closing the newy open tab using following code.

"WebUI.click(findTestObject(‘Reports/ReportsLink’), FailureHandling.OPTIONAL)

WebUI.switchToWindowIndex(1, FailureHandling.OPTIONAL)

WebUI.delay(2, FailureHandling.OPTIONAL)

if()  
{  
}

WebUI.switchToWindowIndex(0, FailureHandling.OPTIONAL)  
WebUI.delay(2)

WebUI.closeWindowIndex(1,FailureHandling.OPTIONAL)  
"  
After execution the newly opened tab is getting closed and switched to main window and the Window is also present However am not able to continue with other Test cases on same window. They are failing since there is no opened window

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/5/8/58461bf8af149dce782dcdce77e9f18c10a84a8a.png)

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/2/8/289fbc43e5a2f41676fcf585b2d6c06e87aa1c6e.png)

Let me know if you need more info.  
Tried many ways by swapping the rows, using windows handles nothing worked. Appreciate your inputs

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [December 4, 2023, 3:32pm UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/2 "2023-12-04T15:32:35Z")

</div>

> [@Sdhongadi](#):
>
> WebUI.switchToWindowIndex(0, FailureHandling.OPTIONAL)

Instead of the above, how about try:

```auto
// Switch back to main page
WebUI.switchToDefaultContent()

```

Edit:  
I’m also going to suggest you to close your window1 **before** you switch back to the default window so the **focus** will remain on the default window. Also, since the FailureHandling.OPTIONAL is the **default** setting for KS, you don’t really need it.

```auto
WebUI.closeWindowIndex(1)

// Switch back to main page
WebUI.switchToDefaultContent()

```

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [December 4, 2023, 3:36pm UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/3 "2023-12-04T15:36:18Z")

</div>

@Sdhongadi I can assure you, the `WebUI.switchToWindow*` APIs work. Are you certain you haven’t screwed up your if-else-logic somewhere?

And by the way, learn to post code so that it’s easier for other people to read:

`````

```plaintext
your code here

```

`````

That’s THREE “backticks”. The backtick key is usually found to the left of the 1 key.

You can also markup code inline with single backticks surrounding it `like this`.

---

<div class="post-metadata">

**Author:** ![Sdhongadi](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sdhongadi/32/16104_2.png) [@Sdhongadi](https://forum.katalon.com/u/Sdhongadi)\
**Post date:** [December 5, 2023, 6:27am UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/4 "2023-12-05T06:27:39Z")

</div>

@grylion54 @Russ_Thomas Thanks for the reply.  
I have tried many different ways like just executing just one line to close the window one  
`WebUI.closeWindowIndex(1)`  
Also by removing and adding failure handling, Adding and removing the if lese logic still no luck  
Also tried using katalon version 8.6.6 and 8.6.8  
As per your suggestion re-ran the test case. still the same issue. Pasting the original code and the results here

```auto
ArrayList<String> ValidationsPassed = new ArrayList<String>()

ArrayList<String> ValidationsFailed = new ArrayList<String>()

WebUI.click(findTestObject('Reports/ReportsLink'), FailureHandling.OPTIONAL)

WebUI.switchToWindowIndex(1)

WebUI.delay(2)

if (WebUI.waitForElementPresent(findTestObject('Reports/UM first link'), 30)) {
    WebUI.click(findTestObject('Reports/UM first link'), FailureHandling.OPTIONAL)

    WebUI.switchToWindowIndex(2, FailureHandling.OPTIONAL)

    WebUI.waitForPageLoad(10, FailureHandling.OPTIONAL)

    String Pagetext = WebUI.getText(findTestObject('Object Repository/Reports/FullPage'), FailureHandling.OPTIONAL)

    if (Pagetext.contains('Client LoB')) {
        ValidationsPassed.add('\n Reports page getting displayed successfully ' //KeywordUtil.markFailed('\n Below validations have been failed \n')
            )
    } else {
        ValidationsFailed.add('\n Reports Page displayed had error ')
    }
    
    WebUI.closeWindowIndex(2)
} else {
    ValidationsFailed.add('Reports page has not been loaded/contains error')
}

WebUI.closeWindowIndex(1)

WebUI.delay(2)

WebUI.switchToDefaultContent()

```

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/6/d/6d2b422b8ec5eceab7f06e9e72d277695e9d1112.png)

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/2/8/2881b5238b6fbf909d5087d19b8b9d3dc496ea20.png)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [December 5, 2023, 5:33pm UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/5 "2023-12-05T17:33:05Z")

</div>

Just to be sure, you are good with opening and closing “window2”, and opening and closing “window1” but when you get back to “window0” (or the default), you can not continue your scripts. Is that corrrect? I did have that happen and I will look to see what I did…

How about just pick an object on the default page and try to bring **focus** to it?

```auto
WebUI.switchToDefaultContent()

WebUI.waitForElementVisible(findTestObject('...'), 10, FailureHandling.OPTIONAL)
WebUI.waitForElementClickable(findTestObject('...'), 10, FailureHandling.OPTIONAL)
WebUI.focus(findTestObject('...'))

```

---

<div class="post-metadata">

**Author:** ![Sdhongadi](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sdhongadi/32/16104_2.png) [@Sdhongadi](https://forum.katalon.com/u/Sdhongadi)\
**Post date:** [December 6, 2023, 7:13am UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/6 "2023-12-06T07:13:44Z")

</div>

Correct! I am good with opening and closing of windows 1 and 2. Also the main window stays open.

I did try the code you suggested. It is successfully run(The object is focused on the main page). It seems to be working now. I can continue to run on the same page

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/1/9/19843ea66f899faa53d1086d1f9edbf10777024d.png)

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/1/2/120a845b1c779afbad0b82412e010a8784d9470c.png)

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/d/e/de483b727c90408db4a9f4ecb91b61c04a85b35b.png)

---

<div class="post-metadata">

**Author:** ![Sdhongadi](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sdhongadi/32/16104_2.png) [@Sdhongadi](https://forum.katalon.com/u/Sdhongadi)\
**Post date:** [December 6, 2023, 11:08am UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/7 "2023-12-06T11:08:51Z")

</div>

Some weird behavior observed.  
When I ran the same TC in test suite the close window step is not executed at all. It behaved like the steps does not exist

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/3/4/34392708dfd12ac4f46a3c2017ec7a6d2474111a.png)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [December 6, 2023, 7:28pm UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/8 "2023-12-06T19:28:10Z")

</div>

Here are two other ways to close a window that you are on;

```auto
import java.awt.Robot
import java.awt.Toolkit
import java.awt.event.KeyEvent

// method 1
WebUI.executeJavaScript('window.close();', [])

// or method 2
Robot robot = new Robot()
robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_W)
robot.keyRelease(KeyEvent.VK_CONTROL)
robot.keyRelease(KeyEvent.VK_W)

WebUI.delay(0.5)
// back to main page
WebUI.switchToDefaultContent()

```

---

<div class="post-metadata">

**Author:** ![Sdhongadi](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sdhongadi/32/16104_2.png) [@Sdhongadi](https://forum.katalon.com/u/Sdhongadi)\
**Post date:** [December 11, 2023, 1:38pm UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/9 "2023-12-11T13:38:00Z")

</div>

> [@grylion54](#):
>
> ```auto
> ss(KeyEvent.VK_CONTROL)
> robot.keyPress(KeyEvent.VK_W)
> robot.keyRelease(KeyEvent.VK_CONTROL)
> robot.keyRelease(KeyEvent.VK_W)
> 
> ```

The methods you mentioned is for closing the window i guess not the windowIndex(tab)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [December 11, 2023, 3:44pm UTC](https://forum.katalon.com/t/closewindowindex-issue/109433/10 "2023-12-11T15:44:34Z")

</div>

Either of the two methods will close a **window tab** ; you would have to be **on the tab** and then give either of these commands and poof, the window tab is gone and then you move back to the main default page:

```auto
WebUI.delay(0.5)
// back to main page
WebUI.switchToDefaultContent()

```

> **Example:**
>
> ```auto
> WebUI.click(findTestObject('myPage/button_Search'))
> 
> WebUI.delay(2)
> 
> WebUI.switchToWindowIndex(1)
> 
> "close this tab"
> if (WebUI.verifyTextPresent("Gmail", false, FailureHandling.OPTIONAL) || WebUI.verifyTextPresent("Google Search", false, FailureHandling.OPTIONAL)) {
> Robot robot = new Robot()
> robot.keyPress(KeyEvent.VK_CONTROL)
> robot.keyPress(KeyEvent.VK_W)
> robot.keyRelease(KeyEvent.VK_CONTROL)
> robot.keyRelease(KeyEvent.VK_W)
> WebUI.delay(0.5)
> WebUI.switchToDefaultContent()
> }
> 
> ```
