Dynamically setting user agent string not working, what am I hosing up?

For some reason the following code isn’t working, and I can’t get the Katalon help site to pull up the full detailed article on RunConfiguration.setWebDriverPreferencesProperty , so asking here if anyone knows why the browser window that is opened using the below doesn’t include my custom user agent?

This is the only script requiring the use of a custom user agent, so not wanting to set a global user agent string unless I absolutely have to

//Select desired browser
RunConfiguration.setWebDriverPreferencesProperty('args', ["--incognito", "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36/KATALON"])
setupRun.select_browser_driver(local_browser_type, user_browser_path)

//Open maximized incognito browser session to indicated site homepage
KeywordUtil.markWarning ("Opening Initial Page")
WebUI.navigateToUrl(initial_URL)
WebUI.maximizeWindow()  
WebUI.waitForPageLoad(30)

Here’s the setup run function mentioned above:

public static select_browser_driver (String browser_type = "", String user_browser_path = "") {


		switch (browser_type.toLowerCase())	{

			//Standard Case - No Browser Identified
			case (""):
				KeywordUtil.logWarning ("*** No Browser Type Specified ***")

				break

			//Set Chrome Driver
			case ("chrome"):
				def chrome_driverpath = user_browser_path
				KeywordUtil.logInfo ("Browser Driver Path: " + chrome_driverpath)
				System.setProperty("webdriver.chrome.driver",chrome_driverpath)
				WebDriver chrome = new ChromeDriver()
				DriverFactory.changeWebDriver(chrome)
				break

			//Set Firefox Driver
			case ("firefox"):
				def firefox_driverpath = user_browser_path
				KeywordUtil.logInfo("Firefox Driverpath: " + firefox_driverpath)
				System.setProperty("webdriver.gecko.driver",firefox_driverpath)
				WebDriver firefox = new FirefoxDriver()
				DriverFactory.changeWebDriver(firefox)
				break

			//Set Edge Driver
			case ("edge"):
				def edge_driverpath = user_browser_path
				KeywordUtil.logInfo("Edge Driverpath: " + edge_driverpath)
				System.setProperty("webdriver.edge.driver",edge_driverpath)
				WebDriver edge = new EdgeDriver()
				DriverFactory.changeWebDriver(edge)
				break

			//If none of the above are found, give an error and stop the run
			default:
				KeywordUtil.markFailedAndStop ("*** Indicated Browser (" + browser_type + "not found, please check and try again", )

				break

		}
	}
1 Like

Solution: Configure Browser Options Directly

  1. Modify your select_browser_driver method to accept user agent parameter:

groovy

public static select_browser_driver(String browser_type = "", String user_browser_path = "", String userAgent = "") {
    switch(browser_type.toLowerCase()) {
        case("chrome"):
            ChromeOptions chromeOptions = new ChromeOptions()
            chromeOptions.addArguments("--incognito")
            if(userAgent) {
                chromeOptions.addArguments("--user-agent=$userAgent")
            }
            // Add other Chrome options as needed

            System.setProperty("webdriver.chrome.driver", user_browser_path)
            WebDriver chrome = new ChromeDriver(chromeOptions)
            DriverFactory.changeWebDriver(chrome)
            break

        case("firefox"):
            FirefoxOptions firefoxOptions = new FirefoxOptions()
            firefoxOptions.addArguments("-private")
            if(userAgent) {
                firefoxOptions.addPreference("general.useragent.override", userAgent)
            }
            // Add other Firefox options

            System.setProperty("webdriver.gecko.driver", user_browser_path)
            WebDriver firefox = new FirefoxDriver(firefoxOptions)
            DriverFactory.changeWebDriver(firefox)
            break

        case("edge"):
            EdgeOptions edgeOptions = new EdgeOptions()
            edgeOptions.addArguments("-inprivate")
            if(userAgent) {
                edgeOptions.addArguments("--user-agent=$userAgent")
            }
            // Add other Edge options

            System.setProperty("webdriver.edge.driver", user_browser_path)
            WebDriver edge = new EdgeDriver(edgeOptions)
            DriverFactory.changeWebDriver(edge)
            break

        default:
            KeywordUtil.markFailedAndStop("Unsupported browser: $browser_type")
            break
    }
}
  1. Update your test script to pass user agent:

groovy

String customUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36/KATALON"

// Call method with user agent parameter
setupRun.select_browser_driver(
    local_browser_type, 
    user_browser_path, 
    customUserAgent
)

// Continue with your test steps
WebUI.navigateToUrl(initial_URL)
WebUI.maximizeWindow()

Key Fixes:

  1. Direct Browser Options Configuration:
  • Create browser-specific options objects (ChromeOptions, FirefoxOptions, etc.)
  • Add arguments directly to these options instead of using RunConfiguration
  • For Firefox, use addPreference instead of arguments
  1. User Agent Injection:
  • Pass user agent string as a parameter to your driver setup method
  • Conditionally apply it only when provided
  1. Browser-Specific Incognito:
  • Chrome: --incognito
  • Firefox: -private
  • Edge: -inprivate

Verification Steps:

  1. Add debug check after browser initialization:

groovy

String actualUserAgent = WebUI.executeJavaScript("return navigator.userAgent;", null)
KeywordUtil.logInfo("ACTUAL USER AGENT: " + actualUserAgent)
  1. Check browser console:

javascript

console.log(navigator.userAgent)

Why Your Previous Approach Failed:

  • RunConfiguration.setWebDriverPreferencesProperty() is designed for Katalon-managed drivers
  • When creating drivers manually with new ChromeDriver(), you must configure options directly
  • Preferences set via RunConfiguration are ignored when drivers are manually instantiated

Additional Recommendations:

  1. Centralize Browser Configuration:

groovy

ChromeOptions createChromeOptions(String userAgent = "") {
    ChromeOptions options = new ChromeOptions()
    options.addArguments("--incognito", "--disable-gpu")
    if(userAgent) options.addArguments("--user-agent=$userAgent")
    return options
}

// Then in driver setup:
WebDriver chrome = new ChromeDriver(createChromeOptions(customUserAgent))
  1. Use Katalon’s Built-in Browser Management (Alternative):

groovy

ChromeOptions options = new ChromeOptions()
options.addArguments("--user-agent=$customUserAgent")

WebUI.openBrowser("", options)
WebUI.navigateToUrl(initial_URL)

does it work?

Unfortunately it does not, here’s my updated code:

method

switch (browser_type.toLowerCase())	{

			//Standard Case - No Browser Identified
			case (""):
				KeywordUtil.logWarning ("*** No Browser Type Specified ***")

				break

			//Set Chrome Driver
			case ("chrome"): 	
				ChromeOptions chromeOptions = new ChromeOptions()
				chromeOptions.addArguments("--incognito")
				if(user_agent_string > "") {
					KeywordUtil.logInfo ("Adding Chrome user_agent_string: $user_agent_string")
					chromeOptions.addArguments("--user-agent=$user_agent_string")
				}
				def chrome_driverpath = user_browser_path
				KeywordUtil.logInfo ("Browser Driver Path: " + chrome_driverpath)
				System.setProperty("webdriver.chrome.driver",chrome_driverpath)
				WebDriver chrome = new ChromeDriver()
				DriverFactory.changeWebDriver(chrome)
				break

			//Set Firefox Driver
			case ("firefox"):
				FirefoxOptions firefoxOptions = new FirefoxOptions()
				firefoxOptions.addArguments("-private")
				if(user_agent_string > "") {
					KeywordUtil.logInfo ("Adding Firefox user_agent_string: $user_agent_string")
					firefoxOptions.addPreference("general.useragent.override", user_agent_string)
				}
				RunConfiguration.setWebDriverPreferencesProperty("firefoxOptions", firefoxOptions)
			
				def firefox_driverpath = user_browser_path
				KeywordUtil.logInfo("Firefox Driverpath: " + firefox_driverpath)
				System.setProperty("webdriver.gecko.driver",firefox_driverpath)
				WebDriver firefox = new FirefoxDriver()
				DriverFactory.changeWebDriver(firefox)
				break

			//Set Edge Driver
			case ("edge"):
				EdgeOptions edgeOptions = new EdgeOptions()
				edgeOptions.addArguments("-inprivate")
				if(user_agent_string > "") {
					KeywordUtil.logInfo ("Adding Edge user_agent_string: $user_agent_string")
					edgeOptions.addArguments("--user-agent=$user_agent_string")
				}
				RunConfiguration.setWebDriverPreferencesProperty("edgeOptions", edgeOptions)
			    
				def edge_driverpath = user_browser_path
				KeywordUtil.logInfo("Edge Driverpath: " + edge_driverpath)
				System.setProperty("webdriver.edge.driver",edge_driverpath)
				WebDriver edge = new EdgeDriver()
				DriverFactory.changeWebDriver(edge)
				break

			//If none of the above are found, give an error and stop the run
			default:
				KeywordUtil.markFailedAndStop ("*** Indicated Browser (" + browser_type + "not found, please check and try again", )
				break

		}
		
		//Display actual user agent string utilized
		String actualUserAgent = WebUI.executeJavaScript("return navigator.userAgent;", null)
		KeywordUtil.logInfo("ACTUAL USER AGENT: " + actualUserAgent)
	}

Test Script

//Select desired browser
//RunConfiguration.setWebDriverPreferencesProperty('args', ["--incognito"])
setupRun.select_browser_driver(local_browser_type, user_browser_path, user_agent_string)

//Open maximized incognito browser session to indicated site homepage
KeywordUtil.markWarning ("Opening Initial Page")
WebUI.navigateToUrl(initial_URL)
WebUI.maximizeWindow()  
WebUI.waitForPageLoad(30)
WebUI.delay(converted_page_draw_delay)
page_count++

Log Output from run:

Figured out what I was doing wrong in the deleted comments, this is working now, thanks!