How to target robot at a specific window?

I’m using the below Robot code to “clean” close the browser because the WebUI.close function doesn’t close browsers in such a way as to trigger certain web events on our site that fire as the user is closing the browser:

			KeywordUtil.markWarning ("Closing Browser")
			Robot robot = new Robot();
			WebUI.delay (2)
			
			// Press keys Ctrl + W
			robot.keyPress(KeyEvent.VK_CONTROL);
			robot.keyPress(KeyEvent.VK_SHIFT);
			robot.keyPress(KeyEvent.VK_W);
			WebUI.delay (2)
			
			// Release keys Ctrl + W
			robot.keyRelease(KeyEvent.VK_W);
			robot.keyPress(KeyEvent.VK_SHIFT);
			robot.keyRelease(KeyEvent.VK_CONTROL);
			WebUI.delay (2)

The problem is that since it’s just keypresses, if I have multiple windows open then it triggers those keypresses on whatever window I happen to be using at the time, is there a way to “point” the keypresses at the specific window that a given script is executing in?

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!

Is the problem that you are multitasking ie running automation code alongside your other activities? If this is the case, I don’t see a Robot solution that wouldn’t interrupt you. Perhaps if you instead used Javascript to close the current tab? Eg

WebUI.executeJavaScript("window.close()

This may only work on an additional tab you opened though but please experiment

3 Likes

try to find the correct window first and use robot for key presses

That’s what I’m curious about, is there an identifier that I can capture when I open the browser instance that I can use to “jump” back to that instance immediately before closing?

Katalon is able to track instances in order to perform the “Terminate running webdrivers” action from the Katalon Studio menu, wondering if there are some accessible trackers Katalon is using that I can tap into

1 Like

There are a family of APIs like switchToWindowIndex() but I’m in favor of @Dan_Bown’s approach: use JavaScript. To me, that seems more direct than using any type of window handle. Hopefully that will work.

1 Like

I’ll give that a try (hopefully later today), thanks!

1 Like

Question - is there a way to detect how many tabs are currently open in a given window so I can loop the JS tab close fxn until they are all gone?

This is from Google:

So, you would have to go and get the count, and then use that in your loop.

Definitely working now, thanks everyone!!!

2 Likes

Please post back with the method you chose and how you got it working.

Thanks.

Hi there Kevin, :wave:

It appears that Dan’s comment has been able to help resolve your issue. Hence, we will close this thread up soon. Should you encounter any other issues, please ask the community by creating a thread on our forum :+1:

Thanks,
Albert

1 Like

This topic was automatically closed after 15 hours. New replies are no longer allowed.

Hi everyone @here,

We would like to share with you that this thread has been chosen by our internal teams to be turned into a blog post on The Katalon Blog. We will share the link to the blog with you shortly.

We have condensed the thread into succinct and informative chunks of information that go over the issues faced by the OP (original poster) and many of the proposed workarounds from other members.

This way, it can help those who are considering using Katalon, or have never been on the forum before, to discover potential solutions from other Katalon users who may be having the same questions/issues as they are.

:pushpin: Note: These blog posts are not intended to offer readers a one-size-fits-all solution to their questions, as we are aware that different machine settings and configurations may lead to different results, but rather to foster further peer-to-peer discussions and collaboration.


Mistakes and inaccuracies are bound to happen with these blog posts. Hence, we encourage you to share your thoughts and feedack in Site Feedback.

Thanks & happy testing! :sunglasses:

2 Likes

Hi all,

Please find the link to the blog post below…

Mistakes and inaccuracies are bound to happen with these blog posts. Hence, we encourage you to share your thoughts and feedack in Site Feedback

1 Like

This is anything but a comprehensive guide. Unless I’m mistaken, @kevin.jackey has identified an (as yet) unbridgeable gap between JavaScript, Robot and Selenium based windows. See Follow up to - How to target robot at a specific window?