Javascript error while opening a new tab

I am trying to open a new tab and hitting a URL during my test executions. I know Katalon has these three commands to deal with window switching.

[WebUI] Switch To Window Index

[WebUI] Switch To Window Title

[WebUI] Switch To Window Url

But all of these involve the scenario where a new tab is opened on clicking some link. But in my scenario, I first need to open a new tab, switch to it and then hit a URL by WebUI.navigateToUrl()

I have been using the following for a few weeks:

WebUI.executeJavaScript('window.open();', [])
currentWindow = WebUI.getWindowIndex()
WebUI.switchToWindowIndex(currentWindow + 1)
WebUI.navigateToUrl(myURL)
WebUI.switchToWindowIndex(currentWindow)

This has been working perfectly fine for me, but recently my tests started throwing the following error on the first line of the above code

Caused by: org.openqa.selenium.JavascriptException: javascript error: Cannot read properties of undefined (reading 'match')

Does someone know why the above code would stop working and whether there’s a better way to open a new tab?

Has the AUT changed recently?

Taken at face value, I don’t see anything wrong with your code – but I am wondering about the wording and “match”.

Try this…

WebUI.executeJavaScript('window.open();', null)

The only other thing that springs to mind is…

WebUI.navigateToUrl(myURL)

How do you know the new browser window is ready? Why not pass the URL in to the window.open() call?

@farooqi.uzair1990 this works for me (I never see any errors):

//Urls to use:
def mainUrl = 'https://en.wikipedia.org/wiki/Main_Page'
def targetUrl = 'https://www.google.ca'

WebUI.openBrowser(mainUrl)
WebUI.waitForPageLoad(10)
WebUI.delay(2) //only used so tester can see page 
WebUI.executeJavaScript('window.open();', null)
WebUI.waitForPageLoad(10)
currentWindow = WebUI.getWindowIndex()

//Switches to 2nd tab
WebUI.switchToWindowIndex(currentWindow + 1)
WebUI.navigateToUrl(targetUrl)
WebUI.waitForPageLoad(10)
WebUI.delay(2) //only used so tester can see page

//Switches to 1st tab
WebUI.switchToWindowIndex(currentWindow)
WebUI.delay(2) //only used so tester can see page
WebUI.closeBrowser()

1 Like