Is it possible to change the user-agent of the browser on Katalon TestOps?

Hello!
My web app has some validations where it detects if it is a mobile browser that is being used to access the app, so it displays especial components accordingly. I’m able to achieve this by changing the user-agent of the browser (In the PC). Everything is working fine in the studio, I’m able to run the test cases in desktop browser and emulate the mobile browser by running a special keyword that changes the user-agent in the @Setup() of the test Suite.

Here’s my keyword:

@Keyword
	public static void setMobileUserAgent() {
		String browserName = DriverFactory.getExecutedBrowser().getName().toLowerCase();

		// Define user-agent strings for different browsers
		String chromeUserAgent = "Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36"
		String edgeUserAgent = "Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36 Edg/127.0.0.0"
		String firefoxUserAgent = "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263"

		// -- Chrome
		if (browserName.contains("chrome")) {
			WebUI.comment('\n\n\n<- Setting up mobile enviroment for Chrome ->')

			// Define the arguments
			def chromeOptions = [
				"--incognito",
				"--user-agent=" + chromeUserAgent,
				"--window-size=" + "360" + "," + "740"
			]

			RunConfiguration.setWebDriverPreferencesProperty("args", chromeOptions)

			// -- Firefox
		} else if (browserName.contains("firefox")) {
			WebUI.comment('\n\n\n<- Setting up mobile enviroment for FIREFOX ->')

			// Set Firefox options
			def firefoxOptions = [
				"args": [
					"--private",
					"--width=" + GlobalVariable.mobileViewportWidth,
					"--height=" + GlobalVariable.mobileViewportHeight
				],
				"prefs": [
					"general.useragent.override": firefoxUserAgent
				]
			]

			RunConfiguration.setWebDriverPreferencesProperty("moz:firefoxOptions", firefoxOptions)

			// -- Edge Chromium
		} else if (browserName.contains("edge")){
			WebUI.comment('\n\n\n<- Setting up mobile enviroment for EDGE ->')

			// Enable Edge
			RunConfiguration.setWebDriverPreferencesProperty("ms:edgeChromium", true)

			// Set Edge options correctly
			def edgeOptions = [
				"args": [
					"--inprivate",
					"--user-agent=" + edgeUserAgent,
					"--window-size=" + "360" + "," + "740"
				]
			]

			RunConfiguration.setWebDriverPreferencesProperty("ms:edgeOptions", edgeOptions)

			// -- Safari
		} else if (browserName.contains("safari")){
			WebUI.comment('\n\n\n<- Setting up mobile enviroment for SAFARI ->')

			// -- Not supported browser
		} else {
			WebUI.comment("\n\n\n<- Unsupported browser for setting user-agent: " + browserName)
		}
	}

As I said this works in the Studio and I’m able to run mobile version of the test cases by using this keyword. But in the Test Cloud I’m getting this error:

[TEST_CASE][ERROR] - Test Cases/_Mobile versions/Authentication/TC-01 Login, check username and logout: java.lang.NullPointerException: Cannot invoke method getName() on null object
	at web.BrowserUtils.setMobileUserAgent(BrowserUtils.groovy:47)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:55)
	at TS-002_Authentication_(Mobile).setupTestCase(TS-002%20Authentication%20(Mobile).groovy:50)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at com.kms.katalon.core.main.ScriptEngine.runScriptMethodAsRawText(ScriptEngine.java:133)
	at com.kms.katalon.core.main.TestSuiteExecutor.runMethod(TestSuiteExecutor.java:225)
	at com.kms.katalon.core.main.TestSuiteExecutor.***$0(TestSuiteExecutor.java:204)
	at com.kms.katalon.core.main.TestSuiteExecutor.invokeTestSuiteMethod(TestSuiteExecutor.java:203)
	at com.kms.katalon.core.main.TestSuiteExecutor.invokeEachTestCaseMethod(TestSuiteExecutor.java:187)
	at com.kms.katalon.core.main.TestCaseExecutor.invokeTestSuiteMethod(TestCaseExecutor.java:381)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:276)
	at com.kms.katalon.core.common.CommonExecutor.accessTestCaseMainPhase(CommonExecutor.java:65)
	at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:150)
	at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:106)
	at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:180)
	at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
	at TempTestSuite1725584295352.run(TempTestSuite1725584295352.groovy:35)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

Here I’m a little lost, I don’t know if it’s possible to do this in the cloud.

I’m considering creating a new project just for mobile testing using Appium and a mobile emulator maybe, but I’m kinda new to katalon and that topic I thought it would be easier to change the user-agent since the app actually shows what it needs to just by changing that.

1 Like

Hi there, :wave:

Thank you very much for your topic! It may take a little while before Katalon team member or others forum members respond to you.

In the meantime, you can double-check your post to see if you can add any extra information i.e. error logs, HTML codes, screenshots, etc. Check out this posting guide to help us help you better!

Thanks! :sunglasses:
Katalon Community team

hope you are invoking remote web driver to trigger the test in test cloud?

I think the error might be because of that I’m using the local webDriver. I’ll try to do that thanks

1 Like

kindly mark as solution if it helped to resolve query Show your appreciation through LIKES and SOLUTION MARKS - Everything you need to know!

I’m sorry but I was unable to make it work.

I changed the RunConfiguration as described in the documentation: Pass deisred capabilities at runtime

So my code ended up a little like:

// Define the arguments
def chromeOptions = [
	"--incognito",
	"--user-agent=" + chromeUserAgent,
	"--window-size=" + "360" + "," + "740"
]

// This works as expected locally, I can change the user-agent of the browser
// and emulate a mobile browser
// RunConfiguration.setWebDriverPreferencesProperty("args", chromeOptions)

// This sets up the desired capabilities for testCloud
RunConfiguration.setDriverPreferencesProperty('Remote', 'args', chromeOptions)

For the remote configuration, it gets logged in the console:

2024-09-08 21:26:38.143 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/_Mobile versions/Authentication/TC-01 Login, check username and logout
2024-09-08 21:26:38.566 INFO  c.k.k.c.w.util.WebDriverPropertyUtil     - User set preference: ['args', '[--incognito, --user-agent=Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36, --window-size=360,740]']
2024-09-08 21:26:38.567 INFO  c.k.k.c.w.util.WebDriverPropertyUtil     - User set preference: ['browserName', 'chrome']
2024-09-08 21:26:38.567 INFO  c.k.k.c.w.util.WebDriverPropertyUtil     - User set preference: ['katalon:option', '{organizationId=1357144, usingTunnel=true}']
2024-09-08 21:26:38.568 INFO  c.k.k.c.w.util.WebDriverPropertyUtil     - User set preference: ['os', 'windows']
2024-09-08 21:26:38.568 INFO  c.k.k.c.w.util.WebDriverPropertyUtil     - User set preference: ['browserVersion', '128']
2024-09-08 21:26:38.574 INFO  c.k.k.core.webui.driver.DriverFactory    - Connecting to remote web server 'testcloud.katalon.com/hub' with type 'Selenium'
2024-09-08 21:26:38.699 INFO  c.k.k.core.webui.driver.DriverFactory    - Action delay is set to 1,000 milliseconds

But it doesn’t seem to have any effect, the test case is failing because it can’t find the right elements (the ones that are supposed to show on mobile version). I tried as well changing the settings directly on the studio:

Same issue, I can see that logged on the console but is not taking effect.

As another alternative, I saw that Test Cloud provides “Mobile Browsers” I tried to run directly that, without running any configuration and it seems to work at least one step more. It shows the mobile elements but the TC is having problems in other steps, I can’t actually debug that properly since is the cloud and the video gets bug as well.

I’m kinda running out of Ideas here.

mobile version is not working local driver or remote driver?

refer this Mobile testing on a remote device | Katalon Docs

Run tests on mobile browsers in TestOps

You have an active TestCloud subscription or a trial.

@csalazar hope you have active TestCloud subscription or a trial license for test cloud