[WebUI] How can I open a new Tab on Chrome?

So, I’ve been searching and all I found about this was the this <a href=“http://forum.katalon.com/questions/how-to-open-a-new-chromes-tab-a-detail-example-would-be-much-appreciated/”>page</a>, but even this one inserted completely into a test, word for word, did not work for me, so I would apretiate if someone could give me the help I need, and explain to me can I open a new Tab on Google Chrome.


Thank you very much for your help

There are a few downsides such as the clear of any forms inserted by the user when you open a new page.

Thanks, but I actually found a way around this, some hyperlinks automatically open in a new tab as such I used one of these links on the MediaWiki website and then I created this method:

WebUI.openBrowser('google.com')
openTab()

import com.kms.katalon.core.testobject.ConditionType as ConditionType
//import org.openqa.selenium.Keys as Keys
Void openTab() {

String currentPage = WebUI.getUrl()

int currentTab = WebUI.getWindowIndex()

WebUI.navigateToUrl('https://www.mediawiki.org/wiki/Special:Contributions/')

TestObject helpButton = new TestObject('Help')

helpButton.addProperty('class', ConditionType.EQUALS, 'mw-helplink', true)

helpButton.addProperty('text', ConditionType.EQUALS, 'Help', true)

helpButton.addProperty('tag', ConditionType.EQUALS, 'a', true)

WebUI.click(helpButton)

WebUI.switchToWindowIndex(currentTab+1)

WebUI.navigateToUrl(currentPage)

/*TestObject body = new TestObject('body')

body.addProperty('tag', ConditionType.EQUALS, 'body')

WebUI.sendKeys(body, Keys.chord(Keys.CONTROL + Keys.SHIFT + Keys.TAB))*/

WebUI.switchToWindowIndex(currentTab)

WebUI.navigateToUrl(currentPage)

WebUI.switchToWindowIndex(currentTab+1)
}

The only problem I have with it is that I can’t actually do the switching of the tabs as I intended, you can see the commented code in there that is my attempt at making the switch, it did not work, can you help me out with that?

1 Like

I think you can achieve this using
1. Open Chrome Browser
2. Send keys (CTRL, SHIFT, ‘N’)
3. Switch to window

I think this should help. Use script tab to code or manual mode with built keywords

Dear Hari
I had tried this option, but not able to open window or tab.
Did you try the steps you mentioned?

I think you can achieve this using

  1. Open Chrome Browser
  2. Send keys (CTRL, SHIFT, ‘N’)
  3. Switch to window

Did it work?

Dear Joel Rodrigues
Did you find any other solution for this?

Regards
Alex
Happy Automating :slight_smile:

You can use Javascript to do this. Below is the sample code hope it helps!!

import org.openqa.selenium.JavascriptExecutor
import com.kms.katalon.core.webui.driver.DriverFactory

WebUI.openBrowser('https://www.google.com/')

WebDriver driver = DriverFactory.getWebDriver()

JavascriptExecutor js = ((driver) as JavascriptExecutor)

js.executeScript("window.open();")
    
WebUI.closeBrowser()
2 Likes

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.:smile:

8 Likes

That’s amazing, thank you

Hi there,

Please try with below code:

import java.awt.Robot
import java.awt.event.KeyEvent

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser("www.google.com")
Robot robot = new Robot()
robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_T)
robot.keyRelease(KeyEvent.VK_CONTROL)
robot.keyRelease(KeyEvent.VK_T)

It uses RobotFramework to simulate pressing Ctrl + T hotkeys for opening a new tab in Chrome

6 Likes

you can find detailed info on this :

http://helping-eachothers.blogspot.in/2018/02/how-to-move-to-new-tab-in-chrome-using.html#more

1 Like

It is easy with WebUI.executeJavaScript()

WebUI.openBrowser('https://www.katalon.com/')
WebUI.executeJavaScript('window.open();', [])
currentWindow =  WebUI.getWindowIndex()
//Go in to new tab
WebUI.switchToWindowIndex(currentWindow + 1)
WebUI.navigateToUrl('https://www.google.lk')
//Comming back
WebUI.switchToWindowIndex(currentWindow)
9 Likes

Really easy with Javascript

WebUI.openBrowser(’’)

WebUI.executeJavaScript(‘window.open();’, [])

alok said:

you can find detailed info on this :

Alok Srivastav

It is very helpful. great work.

Can you open a new window in incognito mode with JS?

1 Like

@Rajitha_Kithuldeniya

What do I do if I want to switch contentiously from Tab1 to Tab 2 and Tab2 to Tab1 and so onn
Ex:
// TAB1
WebUI.openBrowser(‘https://www.katalon.com/’)
WebUI.executeJavaScript(‘window.open();’, [])
currentWindow = WebUI.getWindowIndex()
//Go in to new tab TAB2
WebUI.switchToWindowIndex(currentWindow + 1)
WebUI.navigateToUrl(‘https://www.google.lk’)

//Comming back TAB1 Does not work for me
WebUI.switchToWindowIndex(currentWindow)

Did anybody try to run the WebUI.switchToWindowIndex(1) on FireFox with the latest Katalon version?
It used to work okay, but now it doesn’t.

Try to do it via JavaScript instead of WebUI:
Your imports:

import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor
import org.openqa.selenium.WebDriver as WebDriver
Your code:
WebDriver driver = DriverFactory.getWebDriver()
JavascriptExecutor js = ((driver) as JavascriptExecutor)
js.executeScript(‘window.open();’)

Thank you, this worked for my needs

I have fixed my problem by changing the settings in search page.