Inspired from both Joel’s and Deepthi’s answer, I’ve created a test case where it opens a new tab and navigates to link provided from the current browser tab.
According to me, you should create a new test case where, whenever you want to open a url in another tab, you just have to call the test case.
Call the test case as:
WebUI.callTestCase(findTestCase('Utils/Open_New_Tab'), [path:Link])
where Link is a url stored as a string.
Here, “path” is the url you want to visit. Create a variable (in ‘Utils/Open_New_Tab’ test case) named “path” (in the variables Tab in Katalon) as a String and keep it null. This test case will open the link in new tab and bring focus to the original tab.
Here’s the test case I created,
import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor
import org.openqa.selenium.WebDriver as WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
String currentPage = WebUI.getUrl()
int currentTab = WebUI.getWindowIndex()
WebDriver driver = DriverFactory.getWebDriver()
JavascriptExecutor js = ((driver) as JavascriptExecutor)
js.executeScript('window.open();')
WebUI.switchToWindowIndex(currentTab + 1)
WebUI.navigateToUrl(currentPage)
WebUI.navigateToUrl(path)
WebUI.switchToWindowIndex(currentTab)
WebUI.navigateToUrl(currentPage)
PS: If you only want to open a new tab, remove
WebUI.navigateToUrl(path)
from the above code.
Hope it helps.