Cannot manage Mobile app testing creating driver with startMobileDriver from MobileDriverFactory

Greetings. I am trying make my test by my own using the Appium driver instead of Mobile, to create my custom keywords and classes.

I am using MobileDriverFactory to create the driver and set it globally with startMobileDriver method using the app Id. This method is called in @BeforeTestCase. But at moment to set the driver and use it:

  1. The app does not start.
  2. At moment to use Mobile startExistentApp method it creates a new driver and then start again.
  3. Sometimes the app it just starts right after @AfterTestCase annotated method.

Evidence:
Log from test case:

Method present in image: createDriverFromRemote
def appiumDriver = MobileDriverFactory.startMobileDriver(appId)
MobileDriverFactory.setDriver(appiumDriver)

1 Like

Hi there, and thanks for posting in the Katalon community! :hugs:

To help you faster, please review our guide on Custom Keyword here: Introduction to custom keywords in Katalon Studio | Katalon Docs. Double-checking the steps and configurations might resolve the issue.

If the doc doesn’t help, feel free to provide more details, and a community member will assist you soon. Thanks for being a part of our community!

Best,
Albert Le

The issue occurs due to one of the below:

  1. The driver is not initialized or closed properly.
  2. Conflicts arise when mixing Katalon’s built-in Mobile keywords with custom driver creation.
  3. The app might not start due to incorrect driver setup or appId configuration.

Step-by-Step Fix

1. Initialize the Driver Correctly

Avoid using MobileDriverFactory directly. Instead, use Katalon’s startApplication or configure a custom driver with Appium desired capabilities.

// Use this in your @BeforeTestCase method
@BeforeTestCase
def setupDriver() {
  // Define Appium capabilities
  AppiumDriverProperties capabilities = new AppiumDriverProperties()
  capabilities.setPlatformName("Android") // or "iOS"
  capabilities.setDeviceId("your_device_id")
  capabilities.setApp("path/to/app.apk") // or appId if pre-installed
  
  // Start the driver with capabilities
  Mobile.startApplication(capabilities, false) // false = new driver session
}

2. Avoid Mixing Built-in Keywords with Custom Drivers

If you use startApplication, do not call startExistentApp later. Instead, reuse the existing driver:

// Instead of Mobile.startExistentApp(...), use:
Mobile.switchToExistingApp("your_app_id")

3. Close the Driver Properly in @AfterTestCase

Ensure the driver is terminated after each test to prevent conflicts:

@AfterTestCase
def tearDownDriver() {
  Mobile.closeApplication()
}

4. Verify Configuration in execution.properties

In your test case’s Profile (execution.properties), ensure these settings are defined:

platformName = Android
deviceId = emulator-5554
app = https://your-app-url/app.apk

5. Use Custom Appium Driver (Advanced)

If you need full control over the driver, use AppiumDriver instead of Katalon’s wrapper classes:

import io.appium.java_client.AppiumDriver

@BeforeTestCase
def createCustomDriver() {
  // Define Appium server URL and capabilities
  String appiumServerUrl = "http://localhost:4723/wd/hub"
  DesiredCapabilities caps = new DesiredCapabilities()
  caps.setCapability("platformName", "Android")
  caps.setCapability("deviceName", "Pixel_4")
  caps.setCapability("app", "path/to/app.apk")

  // Create the driver and set it globally
  AppiumDriver driver = new AppiumDriver(new URL(appiumServerUrl), caps)
  MobileDriverFactory.setDriver(driver)
}

Common Pitfalls & Fixes

Symptom Cause Solution
App doesn’t start Incorrect appId or capabilities Double-check appId/appPath and device ID.
Driver restarts unexpectedly Conflict between Mobile keywords and custom driver Stick to one approach (either use Katalon’s built-in keywords or custom Appium code).
App launches after @AfterTestCase Driver closed too early Ensure Mobile.closeApplication() is called only in @AfterTestCase.

Debugging Tips

  1. Check Appium Logs:
    Run Appium server with --log-level debug to see detailed logs.
appium --log-level debug
  1. Enable Katalon Logging:
    Add this to your test script:
import com.kms.katalon.core.configuration.RunConfiguration
RunConfiguration.setLogLevel(RunConfiguration.LOGLEVEL_DEBUG)

Example Project Structure

your-project/
├── Keywords/
│   └── CustomMobileKeywords.groovy
├── Test Cases/
│   └── YourMobileTest.testcase
└── Profiles/
    └── default.properties

By following these steps, you should resolve driver initialization issues and avoid conflicts between Katalon’s built-in keywords and custom Appium code

Hi @dineshh.

Thanks for the feedback. And responding and giving you more details I did not provide:

I will use custom drivers, actually the method createrDriverFromRemote I use is a custom method that create and use MobileDriverFactory.setDriver to use my provided driver. I have two methods, one use MobileDriverFactory.startMobileDriver(appId)
and other use AndroidDriver and IOSDriver, similar to point 5 in your response.

I closed the application in the AfterTestCase method.

About the capabilities I assign it in the Remote options under the project settings.

Additionally:

  1. Is there any documentation about the multiple capabilities handling on testing?
  2. I could not find anything about execution.properties file o section in the project, you mean the profiles instead?