Open Browser with Custom Profile


#1

This tutorial is inspired by this question:

Katalon Studio automatically prompts a new browser with no cached data for every test execution. However, sometimes you need to have some types of default state, for example, to verify that an action is taken when a user logs in for the first time ever. This test scenario requires that a new browser must be opened with some cached data. This tutorial is aimed to demonstrate how to do this scenario in Katalon Studio.

This tutorial will guide you through a simple test scenario that can be extended for your own purposes:

  • Open a browser.
  • Navigate to gmail.com
  • The browser will re-direct you to your inbox.

Chrome

The first step is to create a Chrome Profile that contains your gmail account’s information. Open your Chrome browser and navigate to chrome://settings/ . There clicking on Manage other people will open a panel. Click on Add person and choose the name for your Chrome Profile.

This will open a new Chrome instance with a new profile. Now proceed to log in your gmail account in this instance. After that, navigate to chrome://version/ . You should copy the path to the profile of this Chrome instance to your notepad or a text editor for latter use.

profile_path

Now open Katalon Studio and create a new test case. Open the script mode and enter the following code:

import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions

// Copy the path to chromedriver.exe
String pathToChromeDriver = "{katalon_installed_folder}\configuration\resources\drivers\chromedriver_win32\chromedriver.exe"
System.setProperty("webdriver.chrome.driver", pathToChromeDriver)

// It is important that this chromeProfilePath ends with User Data and not with the profile folder (Profile 2)
String chromeProfilePath = "C:\\Users\\thanhto\\AppData\\Local\\Google\\Chrome\\User Data\\";
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
// Here you specify the actual profile folder (Profile 2)
chromeProfile.addArguments("profile-directory=Profile 2");

WebDriver driver = new ChromeDriver(chromeProfile);
driver.get("https://gmail.com/");
DriverFactory.changeWebDriver(driver)

// Here you can continue your test case

Run the test case and the browser will navigate to gmail.com and re-direct you to your own inbox.

Note: All Chrome browsers should be closed before running this test case


Add extention to browser
Launch Browser as Different User
WebDriver Factory for Groovy Scripting in Katalon Studio
Run Chrome with a different user than the one who started Katalon
How to open chrom with profile2
Please remove unnecessary --disable-extensions --disable-extensions-except switches for chrome.exe
Run Automation in the Active browser
#2

Hello Thanh,

Thanks for your solution. Although it is tedious, it worked.

It serves the purpose.

But do let me know if this can be simplified with any inbuilt keyword in future. We want to link up our automation scripts with CI tools in future.

Thanks & Regards,
Aravind


#3

Hi @aguggella

I will discuss with the team about this possibility and will let you know.

Regards !


#4

I am seriously interested in this topic.

I tried the Thanh To’s approach, and found a difficulty.

In my Chrome, I created a User named Katalon with an avatar of white flower. I switched to the Katalon user, and retrieved chrome://version page. Please find the following screenshot.

I expected that the Profile path should ends with a directory named equal to the User name: Katalon. But actually it doesn’t. The actual profile path ends with a directory named Profile 2. The actual profile path seems automatically resolved by the Chrome browser, hence it can be environment dependent.

This runtime environment dependency causes a problem for writing test scripts. Say, I have 2 PCs. I want to run just the same Katalon project. I can code the Chrome profile name Katalon.

How can a test case code can not resolve a User Katalon to some Profile name: Profile 1 or Profile 2 or Profile 3?

Does anybody know how to resolve this relation by scripting? Or do I have to check chrome://version page everytime manually to find out the profile path of the Katalon user?

Or is it possible for me to force chromedriver to tell chrome browser to make a Profile ending with the user name I wanted Katalon.


#5

One idea has come up to me.

How about using the Default profile directory always? I can expect the Default profile is there always. I would not add new User at all. I would use an existing Chrome user (no matter how it is named I should not mind it) which is resolved to the Default profile directory.


#6

@kazurayam

I am not following. You need another Chrome profile if you want to test on different profiles right ?


#7

I want to execute my test on AWS EC2 with Jenkins. I want to terminate the EC2 instance once the test finished, and create another one next time. Therefore the Chrome profile directories will be empty at startup. My test suites will requires some cached resources (cookies especially). For that, I would prepare some cached resources and store it somewhere, and restore it into the EC2 instance by shell or Gradle script.

The problem is, the name of Chrome User (Katalon) and the name of Profile directory (‘Profile x’) is varying; I want to find how a method to find the Profile directory with Chrome User name as key by script, without human intervention. — I can not find the method yet.

So, a possible opposite approach is to always use the Chrome Profile directory Default on EC2 instance and dont mind User name. — But another problem occurs. if I run this test suite on my daly-use PC, the Default profile directory will be overwritten by test fixture; this causes my daily-use Chrome be uncomforable.


#8

The new post by @ Brandon_Hein gave me a clue.

In my Chrome, I made a new proflile named Katalon. My problem was finding out which directory under the C:\Users\<MyOsUserName>\AppData\Local\Google\Chrome\User Data is linked to the new Katalon profile.

In the directory, I found 3 profile directores which contains Preferences file.

  • Default\Preferences
  • Profile 1\Preferences
  • Profile 2\Preferences

I look into all Preferences files, and found the Profile 2/Preferences file contains the following code:

{
    ...
    "profile": {
        ...
        "name": "Katalon",
        ...
    }
    ...
}

OK。Now I got it。All I need to do is as follows:

  1. locate the Chrome’s User Data directory: C:\Users\<MyOsUserName>\AppData\Local\Google\Chrome\User Data
  2. identify the name of Profile in question: Katalon
  3. search the User Data directory to look up subdirectories which contain a child Preferences file
  4. parse the Preferences file, select the value of /profile/name node in the json. If the value of /profile/name is equal to the Profile name in question (Katalon), then the directory is the one I want
  5. now I know the Profile directory I want is C:\Users\<MyOsUserName>\AppData\Local\Google\Chrome\User Data\<X>

This algorithm resolves the Profile name (Katalon) -> the Directory name (X) deterministically.

I would implement this as a Custom Keyword.


Please remove unnecessary --disable-extensions --disable-extensions-except switches for chrome.exe
Desired Capabilities - The Missing Manual
#9

Hi Thanh,

Thanks for this - it’s a big help. I’m able to launch chrome with the correct profile, but I’m running into an error caused by this line:
WebDriver driver = new ChromeDriver(chromeProfile);

Error:

Reason:
org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /Applications/Google Chrome.app/Contents/MacOS/Google Chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

Any insight into what might be causing this? Is it a versioning issue? The chrome profile I'm using is up to date (72.0.3626.121). I have noticed that I don't get this error if I kill all chrome processes before running (but of course that's not optimal). Any help would be greatly appreciated. 

Thanks, 

Will

#10

Hi @wil.fifer

It seems to be an issue with Selenium Web Driver itself according to this opened issue:

If you really want to get around it, then please try one of the suggestions there.

Regards !

Regards !


#11

As suggested by PascalGit1, the following approach worked for me.

ChromeOptions ChromeOptions = new ChromeOptions();
ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox");
driver = new ChromeDriver(ChromeOptions);

It seems that the option --headless is significant. When i removed --headless, the problem comes up.

Specifying --healeass makes my Chrome headless; I can not view the Chrome window any longer. This is not very much satisfactory. Still I want to find out a way to work around the “DevToolsActivePort file is missing” problem while keeping the Chrome window to be visible. I will continue researching a bit more.


#12

As for

this problem, in the https://stackoverflow.com/questions/50642308/org-openqa-selenium-webdriverexception-unknown-error-devtoolsactiveport-file-d discussion, the following was mentioned.

this can be caused by using a Chromedriver which does not support the version of the installed Chrome. This can happen, for example, if chrome is upgraded without upgrading Chromedriver. – expz Nov 29 '18 at 21:12

It implies, that the chromedirver bundled in the Katalon Studio might be too old to work with the current version of Google Chrome.

OK, let me check it.


My Environment

OS: Windows 7
Katalon Studio version : 6.0.6 beta
chromedriver version: 2.43.600210
Chrome browser version: 72.0.3626.121

Required

Chrome Driver’s documentation http://chromedriver.chromium.org/downloads says

If you are using Chrome version 72, please download ChromeDriver 2.46 or ChromeDriver 72.0.3626.69"

Experiment

I downloaded the Chrome Driver 2.46 and installed into the Katalon Studio’s directory. I tried my test and found that the “DevToolsActivePort file doesn’t exist” problem disappeared.

Conclustion

The chromedrivers bundled in Katalon Studio 6.0.6 should be updated to the current releases.


[Updated - Mar 8th] Beta release 6.0.6
#13

I could successfully developed a set of Groovy classes named webdriverfactory4ks. This library enables me to launch Groogle Chrome browser specifying a Custom Profile name. You can use it as follows:

import com.kazurayam.ksbackyard.webdriver.ChromeDriverFactory
...
	@Test
	void test_openChromeDriver() {
		ChromeDriverFactory cdFactory = new ChromeDriverFactory()
		WebDriver driver = cdFactory.openChromeDriverWithProfile('Katalon')   // Katalon is a Profile name
		assertThat(driver, is(notNullValue()))
		DriverFactory.changeWebDriver(driver)
		WebUI.navigateToUrl('http://demoaut.katalon.com/')
		WebUI.delay(3)
		WebUI.closeBrowser()
	}

This code set worked OK.

I would share the code later.

This code set involves a few Groovy classes, so it should be archived into a *.jar for sharing class files. The Katalon Plugin mechanism is still being developed, and would requires a few weeks to come.


edited 12/March/2019

I have published the webdriverfactory4ks-all.jar on GitHub.


Run Chrome with a different user than the one who started Katalon