closeWindowIndex Issue

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

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

1 Like

Instead of the above, how about try:

// 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.

WebUI.closeWindowIndex(1)

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

@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:

```

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.

@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

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


1 Like

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?

WebUI.switchToDefaultContent()

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

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

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

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

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

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

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:

WebUI.delay(0.5)
// back to main page
WebUI.switchToDefaultContent()
Example:
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()
}