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

Hello Katalon Team, i am currently doing a test scenarios for a proof of concept and however being blocked. Below is my scenario:

The typical scenario requires the tester (using always the same identity) to handle THREE browsers simultaneously:

  1. access an App in Browser1 , get redirected to an app for authentication, get a session, get access
  2. access an App in Browser2 , get redirected to an app for authentication, get a session, get access
  3. in Browser1 , observe that access to another app is transparent, no re-authentication is required
  4. access an app in Browser3 , get redirected to an application for authentication, get a session, get access
  5. in Browser2 , observe that access to another app is transparent, no re-authentication is required
  6. in Browser1 , observe that the session is gone, i.e., that the identity is redirected for re-authentication.

@kazurayam, could you please help how to overcome that issue? I checked on the forum but could not get a solution for the above typical scenario.

Thanks in advance.

I am sorry, I do not understand your description above. You described something, but have not made any question.

What is “that issue”?

Have you been successful for the step#1?
Have you been successful for the step#2?
Have you been successful for the step#3?
Have you been successful for the step#4?
Have you been successful for the step#5?
Have you been successful for the step#6?
…
I don’t see.

The issue is that, after opening browser 1 and browser 2, im not able to switch to browser 1 to perform a test. Then switch to browser 2 to perform another test. That switching is my issue :frowning:

Please show your test case script.

here is a screenshot:

Every detail is hidden in your custom keywords. I can not help you debugging your custom keywords.

1 Like
  1. open browsers:

  2. repeat this keyword twice

  3. after opening the 2 browsers, i want to switch back to the first browser

  4. Perform some test in browser 1, then switch to browser 2, in the same flow…

Is it a it clear?

Essentially you are doing this:

WebUI.openBrowser(null)   // browser1
WebUI.openBrowser(null)   // browser2

Yes, you would see 2 browser windows opened.

But, you will loose the 1st session from Katalon to the browser1 as soon as the 2nd session started.

is there a solution, to switch back to the first session?

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

Hello @kazurayam, thank you. Have a nice weekend & stay safe.

Hi @kazurayam, I want to ask on this following question. I am using Katalon recorder in which there is no codes that you have mentioned. I want to switch from one browser to another browser using recorded clicks.

Katalon Recorder and Katalon Studio — these are 2 different products, you know?

Katalon Recorder is a browser plug-in.
Katalon Recorder lives and works inside a running browser.
Katalon Recorder can not get out of its containing browser window.
Katalon Recorder would never be able to switch from one browser to another.

Katalon Studio is a stand-alone application.
It can start/stop a browser.

I use Katalon Studio.
If you want to use the code I presented, you need to use Katalon Studio.

Hi @kazurayam,

Thanks for the prompt reply.

But I have a complete different scenario.

Let’s say I have 3 screens interconnected with each other. I want to click and scroll some things on a particular website in one browser, and the clicks and scroll should get replicated on other browser exactly the way they are in chrome browser. I just want to ensure that my application is running smooth across all browsers, so is there a way I can test it manually across all browsers at same time with synchronization.

I have no idea.