Opening Chrome Browser with a predefined custom Chrome Profile which stores session info such as credentials and cookies

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:

  1. In Chrome UI, you can create a new Profile with name new_chrome_profile
  2. 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 the C:\users\osusername\AppData\Local\Google\Chrome\User Data\Profile 2 folder. The association of new_chrome_profile and Profile 2 was determined by Chrome when new_chrome_profile was added.
  3. 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

  1. Download the webdriverfactory4ks-all.jar from the GitHub project’s Releases page.
  2. Place the jar into the Drivers directory of you Katalon Studio project; just as documented in the doc External libraries
  3. Write your test case script which calls the com.kazurayam.webdriverfactory4ks.ChromeDriverFactory class.

Examples

The following examples will show you how to utilize this.

  1. Test Cases/main/example_openChromeDriverWithProfile — opening Chrome specifying a predefined Profile name
  2. Test Cases/main/example_openChromeDriverWithProfileDirectory — opening Chrome specifying a name of Profile directory, for example Default directory.
  3. Test Cases/main/example_startGmailWithoutLoginOperation — this demonstrates that I can navigate into Gmail without login operation if I open Chrome with the Default directory.
2 Likes

Hi,

Not able to use above solution to open chrome browser with custom or default profile and observed below error:
Reason:
org.openqa.selenium.WebDriverException: unknown error: failed to write prefs file
(Driver info: chromedriver=74.0.3729.6

Need your help to fix this issue ASAP. Appreciated your early response.

need help" code is failing: and Reason:
org.openqa.selenium.WebDriverException: unknown error: failed to write prefs file
(Driver info: chromedriver=74.0.3729.6

webdriverfactory4ks works for me.

My environment is

software version
Katalon Studio 6.1.4
chromedirver 74.0.3729.6
Chrome browser 74.0.3729.131
1 Like

com.kazurayam.webdriverfactory4ks.ChromeDriverFactory does NOT create a Chrome Profile for you.

You need to manually create a Chrome profile XXXXXX manually before calling cdFactory.openChromeDriverWithProfile('XXXXXX').

How? — See https://www.bettercloud.com/monitor/the-academy/how-to-create-switch-profiles-in-chrome/

Have you created a Chrome Profile with which you want to open?

1 Like

Some stack overflow posts discusses some possible reasons which may cause this problem. For example,

Exception "unknown error: failed to write prefs file" is thrown when same 
user-data-dir and profile is used by two chrome instances in parallel.

In this case, you should stop ALL Chrome browser, and try again.

1 Like

hi,

please fix the paths in a document & testcases
//import com.kazurayam.ks.thoughtful.ChromeDriverFactory
import com.kazurayam.webdriverfactory4ks.ChromeDriverFactory
//import com.kazurayam.ks.thoughtful.ChromeProfileFinder
import com.kazurayam.webdriverfactory4ks.ChromeProfileFinder

Hi @kazurayam, how were you able to handle this error

Close all of Chrome Browser’s Window on your PC before you execute the test.

Is there no other way for this to be handled? I’m not running my Automation on Virtual Machine or remotely

Vertual Machine? remotely? — I do not understand how these factors concern about “user data directory is already in use” problem.

Since you mentioned that other open browsers should be closed, i figured that in order for me to run this safely without disrupting my other tasks, it would really need me to use some virtual machine so I can isolate my automation runs. Anyways, your code helped me figure out how to bypass Active Directory. Thank you very much for this! :smiley:

The following post may help

1 Like

This was my primary motivation why I researched how to start Chrome Browser with predefined custom Chrome Profile.

To tell you the truth, I was not successful for my issue. I have never succeeded to make Chrome Browser started retaining “already logged in status” of Web applications. I could not find out the detail but Chrome Browser is very well-engineered in terms of “Security”. I could not cheat Chrome at all.

I have got tired trying to cheat Chrome, and I am negative I could achieve it in future.

@kazurayam
Man , you`ve solved one of my worst problems and i thank you so mush <3 .
But there is always the question, can we have the same thing for other browsers , Firefox , Opera,Safari maybe ?

No, I am not willing to. Looking at one browser is heavy enough. No more, thanks.

I have ever worked on Firefox:

but I forgot all about this.

Hello! Any chance to do the same in incognito mode?
Thanks for the aswer.

I don’t understand this. Chrome incognito mode and Chrome Profile — these 2 things seem not relevant at all to me.

1 Like

I’ve set up the profile, did all the steps, however, is it possible for that profile to open a chrome in incognito mode, always?