[Sharing is learning] [Get rewards] Join Us Now to Become Katalon Champions. Learn more
Joel Rodrigues
Katalon Apprentice
05/23/2017

[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="https://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

Upvote
Quote

Comments

  • Hari Charan
    Katalon Apprentice
    05/23/2017
    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
    Upvote
    Quote
  • Vinh Nguyen
    Katalon Moderator
    05/23/2017
    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
    Upvote
    Quote
  • Joel Rodrigues
    Katalon Apprentice
    05/24/2017
    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?
    Upvote
    Quote
  • Joel Rodrigues
    Katalon Apprentice
    05/24/2017
    There are a few downsides such as the clear of any forms inserted by the user when you open a new page.
    Upvote
    Quote
  • Joel Rodrigues
    Katalon Apprentice
    05/24/2017
    That's amazing, thank you
    Upvote
    Quote
  • alex
    Katalon Apprentice
    10/04/2017

    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 :)

    Upvote
    Quote
  • Deepthi
    Katalon Apprentice
    10/04/2017
    edited October 2017

    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()
    
    Upvote
    Quote
  • Dhaval Kriplani
    Katalon Apprentice
    10/16/2017

    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:

    Upvote
    Quote
  • alok
    Katalon Apprentice
    02/12/2018
  • Rajitha Kithuldeniya
    Katalon Apprentice
    03/13/2018
    edited March 13
    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)
    Upvote
    Quote
  • bhupendra Patidar
    Katalon Apprentice
    03/28/2018
    Really easy with Javascript

    WebUI.openBrowser('')
    WebUI.executeJavaScript('window.open();', [])
    Upvote
    Quote
  • Jambunath
    Katalon Apprentice
    06/12/2018
    alok said:
    It is very helpful. great work.
    Upvote
    Quote
  • Magyar Zoltán
    Katalon Apprentice
    06/13/2018
    Can you open a new window in incognito mode with JS?
    Upvote
    Quote
Sign In or Register to comment.