How can I switch to a different window in scripting mode?

I am new to Katalon and i have a task, the task is to open a window then switch to it, maximize it, then close it. in the task there are two windows. The first one is working correctly, the second one is not.
I will provide you with the JS which controls the window:

WebUI.openBrowser('')
WebUI.navigateToUrl('https://www.w3schools.com/jsref/met_win_open.asp')
WebUI.click(findTestObject('Object Repository/w3/Page_Window open() Method/a_Try it Yourself'))
WebUI.switchToWindowTitle('W3Schools Tryit Editor')
WebUI.click(findTestObject('Object Repository/w3/Page_W3Schools Tryit Editor/button_Try it'))
sleep(4000)
WebUI.switchToWindowTitle('')  // First one working good 
WebUI.maximizeWindow()
sleep(4000)
WebUI.closeWindowTitle('')
WebUI.switchToWindowTitle('W3Schools Tryit Editor')
WebUI.delay(3)
WebUI.closeWindowTitle('W3Schools Tryit Editor')
WebUI.switchToWindowTitle('Window open() Method')
WebUI.click(findTestObject('Object Repository/w3/Page_Window open() Method/a_Try it Yourself_1'))
WebUI.switchToWindowTitle('W3Schools Tryit Editor')
WebUI.delay(3) 
WebUI.click(findTestObject('Object Repository/w3/Page_W3Schools Tryit Editor/button_Try it'))
sleep(4000)
WebUI.switchToWindowTitle('MsgWindow')  // This is not working
WebUI.maximizeWindow()
sleep(4000)
WebUI.closeWindowTitle('MsgWindow')
WebUI.switchToWindowTitle('W3Schools Tryit Editor')
WebUI.delay(3)
WebUI.closeWindowTitle('W3Schools Tryit Editor')

JS :

 <script>
function myFunction() {
  var myWindow = window.open("", "MsgWindow", "width=200,height=100");
  myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
}
</script>
1 Like

Hi there @adelrahman.qarout, :wave:

Welcome to Katalon Community!

I found some topics which raised similar issues regarding switching between windows below. Perhaps you could check them out? :point_down:

Let us know if this helps. Thanks :+1:

1 Like

the best way might be to just use the “Recorder” option which will give you the appropriate code when you switch to that window. Can you capture objects on that window? is it an iframe window? have you tried using Index instead of window title? If it’s another tab… and it’s the 3rd one… WebUI.switchToWindowIndex(2)