How to switch from one browser to another, then return to the initial browser and continue further test

You can try calling WebUI.openBrowser() twice. What will happen? When your test case calls WebUI.openBrowser() for the 2nd time, it will close the previous browser window, and then opens a new browser window. Effectively it maintains only 1 session from Katalon process to browser.

How to open 2 windows of browser at a time?

It is possible. You should not use WebUI.openBrowser() at all. You should create multiple WebDriver instances (ChromeDrvier, FirefoxDriver) yourself. You want to create 2 instances and keep them alive. Use DriverFactoy.changeWebDriver() method to switch the target of WebUI.* keywords to the browser1, to the browser2, and back to the browser1, …

The following code works for me.

import org.openqa.selenium.WebDriver

import com.kazurayam.ks.webdriverfactory.WebDriverFactory
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// open browser1, keep the session to the browser1 alive
//WebDriver browser1 = WebDriverFactory.newWebDriver(DriverFactory.getExecutedBrowser(), 'Katalon') // open browser with profile 'Katalon'
WebDriver browser1 = WebDriverFactory.newWebDriver(DriverFactory.getExecutedBrowser())  
assert browser1 != null

// open browser2, keep the session to the browser2 alive
WebDriver browser2 = WebDriverFactory.newWebDriver(DriverFactory.getExecutedBrowser())
assert browser2 != null

// in browser1, navigate to a URL
DriverFactory.changeWebDriver(browser1)
WebUI.navigateToUrl('http://demoaut.katalon.com/')

// in browser2, navigate to a URL
DriverFactory.changeWebDriver(browser2)
WebUI.navigateToUrl('https://duckduckgo.com/about')

// switch to browser1 and do something
DriverFactory.changeWebDriver(browser1)
String title1 = WebUI.getWindowTitle()
WebUI.comment("title1: ${title1}")

// switch to browser2 and do something
DriverFactory.changeWebDriver(browser2)
String title2 = WebUI.getWindowTitle()
WebUI.comment("title2: ${title2}")

WebUI.delay(3)

browser2.quit()
browser1.quit()

You can create ChromeDriver yourself using org.openqa.selenium.webdriver.chrome.* API. This API is bundled in Katalon Studio and available for test cases. See ChromeDriver - WebDriver for Chrome - Getting started

In the above sample, I use com.kazurayam.ks.webdriverfactory.WebDriverFactory. It is my custom Groovy class. Have a look at its README. This enables me creating WebDriver instance with custom Chrome Profile (e.g, Katalon). The jar (ver 0.3.0) of this class is available at Releases · kazurayam/WebDriverFactory4ks · GitHub. You want to download the jar and locate it into the Drivers folder of your katalon project. To be honest, the doc of webdriverfactory project is not well written; I know the doc has some mistakes. I have already lost my interest in it.

4 Likes