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.