Follow up to - How to target robot at a specific window?

Well, Katalon cursed us by converting How to target robot at a specific window? into a blog post :slight_smile:

Essentially it appears that Javascript has built functionality that puts some significant constraints on the window.close() javascript function in that the targeted window now has to be explicitly called out, as noted in the following section from Window: close() method - Web APIs | MDN, which I had not encountered because the scripts I was previously using this function against only opened a single page and then closed it, thus putting them into the “top-level windows that have a single history entry” category

“This method can only be called on windows that were opened by a script using the Window.open() method, or on top-level windows that have a single history entry. If the window doesn’t match these requirements, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.”

For more complex scripts, I’m at a loss as to how to do this, as the method described in the linked article -

//Global variable to store a reference to the opened window
let openedWindow;

function openWindow() {
  openedWindow = window.open("moreinfo.htm");
}

function closeOpenedWindow() {
  openedWindow.close();
}

Only works for Javascript opened windows, which I am failing to get the Katalon webdriver to recognize. On the flip side I can’t figure out how to put the WebDriver object into a javascript-recognized object such as myWindow to use in the myWindow.close() javascript call.

Anyone have any suggestions on how I might get this to work in Katalon, as if I have to stick to the javascript opening method and am unable to use the Katalon web functions I might as well forget Katalon for this instance and just move to basic Selenuim?

1 Like

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

How about putting a “try / catch” block around your close event to try a second way, like:

function closeOpenedWindow() {
   try {
      openedWindow.close();
   catch (Exception) {
	   "try and 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)
   }
}

I’ll give that a try, the only issue with the robot method is that if multiple windows are running in parallel it will close whatever window happens to be active at the time, instead of the desired target window. That was the issue presented in the first linked chat.

Apparently the browser close command is supposed to be much improved in Selenium 4; however it does not appear that Katalon has fully supported Selenium 4 yet

1 Like

Then what about putting focus on your specific window and then sending it the way of the dodo.

2 Likes

I think we’re getting a bit off track here, the issue is how to populate the opened window variable using a window that has been created using the WebUI.openBrowser function, since Katalon doesn’t appear to be able to use WebUI functions on windows created with window.open(), how do we get that piece to work?

Clearly!

What exactly, in a very brief simple-English sentence, do you want to achieve?

  1. open a window?
  2. close a window?

Well, you said brief, but that’s not really workable in this situation I believe :slight_smile:

  1. Open a window using the Katalon WebUI command set
  2. Run my multi-page script, again using Standard Katalon WebUI commands
  3. At script end, execute a “clean close” using either targeted robot commands (that only close the window for the browser window the calling script is executing, not just a random open window that happens to be focused at that point in time, or with the Javascript close command.

I’d prefer a robot method because I know that closes the browser in such a way as to fire the backend console process that I need to test fires at browser close, whereas the JS method is currently unproven, but I’ll take and try anything I can get :slight_smile:

Since it appears that this topic may have Katalon’s eye, if anyone from that team would want to work with me to figure out why the Katalon WebUI browser close command doesn’t trigger the correct browser closing processes I’m game for that as well.

Please define “page script” then “multi-page script”. I have no idea what that means. I can guess, but it’s anything but clear.

There’s a reason I asked for “simple English” and “brief”. Now we’re getting caught up in techno-babble. AGAIN. And having to go back and fore again…

See?

Let me explain what I think…

  1. What you want to do, I believe, can be done.

  2. It’s impossible to be sure and guide you without understanding what it is you’re doing. Due to the convoluted nature of threads on the forum, it is really hard to figure out.

  3. If this was my day job I’d have time to gather together all the pieces and roll out an answer/solution. But it isn’t my day job. You have to help ME help you.

  4. You have definitely exposed a weakness in the API sets on offer – see my response on the old thread which I believe was closed far too early. And using it as a blog post is a joke, I’m sure.

At the very least, that, if it’s true, is a selenium bug which the Katalon APIs have inherited.

@duyluong @support.squad @mike.verinder

“Page Script” means a script that runs against a single page and then closes, “Multi-page” script means that there is more than one page involved in the script.

This is a critical call-out due to this statement on the window.close() item linked above - " or on top-level windows that have a single history entry". In other words, a single page script works with windows.close() [at least mine does]; however it fails once more than one page is involved which appears to fall into the page history failure trap.