I have made a GitHub project to develop and distribute the webdriverfactory-all.jar
Problem to solve
Original question
A post in the Katalon Forum Run Automation in the Active browser raised an interesting question. The originator aguggella gave us a good explanation what his/her problem is. I would quote the paragraphs with a bit of rewording:
I have Salesforce application which will send OTP (one time password) to the client’s browser on the following occasions: (1) first time of logging-in, (2) at the second logging-in and later when the browser does not present credential information associated with the original OTP because the browser’s cache is cleared or empty
I need to open browser manually and login to the Salesforce application once. A new OTP will be issued and processed. I will do that anyway. As long as I use the same Profile of Browser without clearing cached data, the original OTP is retained in the cache. I will not be asked to repeat login operation.
Problem arises here: I want to automate testing the Salesforce application using Katalon Studio, but I can not. Why? Whenever I start test script, Katalon opens a new browser with no cached data. Therefore Salesforce application sends a new OTP. New OTP requires my manual intervention. Automation broken.
Let me assume, I opened Chrome with a Profile ‘Katalon user’ and logged-in Salesforce applition. If I could somehow let Katalon Studio to start Chrome with the same Profile ‘Katalon user’, then the test will run without requiring manual intervention. But how can I achieve it?
How to open Chrome with predefined profile
Thanh To , a Katalon Developer, replied to aguggella in another post Open Browser with Custom Profile. He described the following points:
- In Chrome UI, you can create a new Profile with name
new_chrome_profile
- In Chrome UI, you can identify the Profile path, a folder, where all of cached data is stored. For example, the Profile
new_chrome_profile
is found associated with theC:\users\osusername\AppData\Local\Google\Chrome\User Data\Profile 2
folder. The association ofnew_chrome_profile
andProfile 2
was determined by Chrome whennew_chrome_profile
was added. - In Katalon Studio, in test case script, you can write Groovy code to open Chrome browser as this:
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("user-data-dir=" + "C:\\Users\\thanhto\\AppData\\Local\\Google\\Chrome\\User Data\\");
chromeProfile.addArguments("profile-directory=Profile 2");
It’s great to know that I can write test code that can open Chrome with predefined Profile. But I find a itchy problem still remains.
I created a new Profile with name new_chrome_profile
. It was associated with a new folder named Profile 2
. This association is determined by Chrome. It’s OK for me to write the profile name new_chrome_profile
as a constant string in test cases. But I do not like writing the folder name Profile 2
as a constant string in my test case code. As Thanh To described, I can find it by checking the chrome://version
page. But this manual preparation looks less stylish with respect to complete automation.
What my ChromeDriverFactory class does?
I have developed a custom groovy class com.kazurayam.webdriverfactory4ks.ChromeDriverFactory
. Let me show you a sample use case of it. The following script assumes you have Chrome browser installed in your PC, and in it you have pre-defined a Profile with name Katalaon
. Before executing the script, you want open Chrome with Katalon
profile and visit whatever URL. Then you want to execute this script. You will find the Chrome browser launched by the test script is using the Katalon
profile.
Test Cases/main/example_openChromeDriverWithProfile
import org.openqa.selenium.WebDriver
import com.kazurayam.webdriverfactory4ks.ChromeDriverFactory
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
ChromeDriverFactory cdFactory = new ChromeDriverFactory()
WebDriver driver = cdFactory.openChromeDriverWithProfile('Katalon') // THIS IS THE MAGIC!
assert driver != null
DriverFactory.changeWebDriver(driver)
WebUI.navigateToUrl('https://demoauto.katalon.com/')
WebUI.delay(3)
WebUI.closeBrowser()
How to use webdriverfactory4ks in your Katalon project
- Download the
webdriverfactory4ks-all.jar
from the GitHub project’s Releases page. - Place the jar into the
Drivers
directory of you Katalon Studio project; just as documented in the doc External libraries - Write your test case script which calls the
com.kazurayam.webdriverfactory4ks.ChromeDriverFactory
class.
Examples
The following examples will show you how to utilize this.
- Test Cases/main/example_openChromeDriverWithProfile — opening Chrome specifying a predefined Profile name
- Test Cases/main/example_openChromeDriverWithProfileDirectory — opening Chrome specifying a name of Profile directory, for example
Default
directory. - Test Cases/main/example_startGmailWithoutLoginOperation — this demonstrates that I can navigate into Gmail without login operation if I open Chrome with the
Default
directory.