How to configure the browser driver as a mobile device [Mobile Emulation]

Hi team, I am testing a web product and want to configure the browser driver as a mobile device, similar to the screenshot, I haven’t found the right setting in the document (https://docs.katalon.com), maybe I haven’t found it, could someone please help me :rofl:

I found the same question in forum before~

@michael.hollander

Launching Chrome with device emulation enabled

@Aliasger_Kiranawala

How to set user-agent of a browser for execution as Mobile web?

@Gordon Its possible to set the size resolution but user agent we can set as chrome browser capability. Issue is I was not able to verify if the user agent is set or not, although browser was opening with set resolution.

Hi @Gordon,

In Chrome, it’s called Mobile Emulation

How it works in Katalon:

  • Step 1: Open `Project Setting/Desired Capabilities/Web UI/Chrome’
  • Step 2: Add this property on the table of properties:
mobileEmulation: {deviceName=Nexus 5}

Note that the Nexus 5 is an example.

thanks a lot, it works well~
when the browser driver is started, it will get the resolution of the device which having set

This is very helpful. Thank you very much!

One last question, though: Is it possible to pragmatically set the mobileEmulation value during a test case? If possible, I’d like to test a few models of phones by setting the value and looping the test case, rather than have a separate project for each model I intend to test.

Hi @duyluong,

Can you please explain more how to add that values.

i have followed your steps but didn’t work for me

Someone tried to add the orientation to LANDSCAPE?
I tried but did not work or I made something wrong.

I resolve that problem.
Value:
Driver Name: Chrome

Preferences:
Name: mobileEmulation
Type: Dictionary

Value:
Name: deviceName
Type: String
Value: Nexus 6

Name: orientation
Type: String
Value: LANDSCAPE

Name: platformName
Type: String
Value: Android

Name: platformVersion
Type: String
Value: 9.0

Hi, Can you share the Step by Step procedure. I’m just new in mobile testing.

Where to put this and is this needed?

Map<String, String> mobileEmulation = new HashMap<>();

mobileEmulation.put(“deviceName”, “Nexus 5”);

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption(“mobileEmulation”, mobileEmulation);

WebDriver driver = new ChromeDriver(chromeOptions);

or this is enough following the steps below?

How it works in Katalon:
•Step 1: Open `Project Setting/Desired Capabilities/Web UI/Chrome’
•Step 2: Add this property on the table of properties:
mobileEmulation: {deviceName=Nexus 5}

HI,
I have same problem.
I try this:

**1st. **
•Step 1: Open `Project Setting/Desired Capabilities/Web UI/Chrome’
•Step 2: Add this property on the table of properties:
mobileEmulation: {deviceName=Galaxy S5}

2.nd
use code
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put(“deviceName”, “Galaxy S5”);

Thanks, I have successfully run it.

Hi, Can you share the Step,
Where to put this code ? for each case?
2.nd
use code
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put(“deviceName”, “Galaxy S5”);

Hi @duyluong ,

is there a way to do this exact same thing but on Mozilla Firefox ?

Thanks!

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

Do you have any idea on how to set this up for the testCloud enviroment?

I’ve made this Keyword that I use on the Test Suite @Setup() so it changes the user-agent of the Browser for mobile testing.

@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)
		}
	}

I just run this keyword before opening the browser.

can you refer Mobile testing on a remote device | Katalon Docs