Help on how to code a Test Listener for both Local Test and Cloud Test (TestCloud)

Hi Community,

Currently my team have a predicament, we want to test using TestCloud as we already bought the Enterprise ver, but we notice that to start testing using Cloud, you need to change how the application start. We should use startApplication() and not startExistingApplication.

Now we have a problem to change the Test Listener code. When running locally, we use startExistingApplication as the application is already install in our emulator/real device but for cloud device is different.

For our code currently we set the BeforeTestSuite to startApplication() to run for cloud testing but when we want to run locally, we need to change it to startExistingApp()

Can anybody have a suggestion or experience on Cloud Testing that can assist us on this matter ? Thank you

Below is our current Test Listener.


public class CaseListener {
 String title_permission = "Please allow Maybank Trade to send you notifications"
 String packageName = "com.mbb.titan.uat.sg"
 
 @BeforeTestCase
 public void beforeTestCase(TestCaseContext testCaseContext) {
 Mobile.startExistingApplication(packageName)
  MainCase.allowNotification() // This method is to check the Allow Notifications when   starting the app

 }

 @AfterTestCase
 public void afterTestCase(TestCaseContext testCaseContext) {
  if (testCaseContext.getTestCaseStatus() == "FAILED") {
   println "Test case failed. Closing and restarting the app."
   try {
  Mobile.startExistingApplication(packageName)
   } catch (Exception e) {
    println "Error while restarting the app: ${e.message}"
   }
  } else if (testCaseContext.getTestCaseStatus() == "PASSED") {
   try {
	  Mobile.closeApplication()
   } catch (Exception e) {
    println "Error while closing the app: ${e.message}"
   }
  }
 }

 @BeforeTestSuite
 public void beforeTestSuite(TestSuiteContext testSuiteContext) {
  Mobile.startApplication(packageName, false, FailureHandling.CONTINUE_ON_FAILURE)
	  if (Mobile.verifyElementExist(ObjectModel.actionFindTheObject(FIND_BY_TEXTVIEW, title_permission), 0, FailureHandling.OPTIONAL)) {
   // If the prompt appears, tap "Not now"
   Mobile.tap(ObjectModel.actionFindTheObject(FIND_BY_ACCESS_ID, "Not now"), 0)
  } else {
   // Otherwise, print "Allowed"
   System.out.println("Permission prompt not found, proceeding...");
  }
 }

 @AfterTestSuite
 public void afterTestSuite(TestSuiteContext testSuiteContext) {
  Mobile.takeScreenshot()
  if (testSuiteContext.getStatus() == "FAILED") {
   println "Test case failed. Closing and restarting the app."
   try {
 Mobile.startExistingApplication(packageName)
   } catch (Exception e) {
    println "Error while restarting the app: ${e.message}"
   }
  } else if (testSuiteContext.getStatus() == "PASSED") {
   Mobile.closeApplication()
  }
 }
}
1 Like

To handle both local and cloud tests in a single Test Listener, you can use Katalon’s execution profiles to dynamically determine the environment and choose the appropriate method to start the application. Here’s a structured solution:

1. Set Up Execution Profiles

Create two execution profiles in Katalon Studio:

  • Local: Uses startExistingApplication().
  • Cloud: Uses startApplication(), configured for TestCloud.

Steps:

  1. In Katalon, go to Profiles under your project.
  2. Create a new profile (e.g., “TestCloud”) and add a custom property isCloud = true.
  3. Keep the default profile (or create a “Local” profile) with isCloud = false.

2. Modify Test Listener

Update your Test Listener to check the isCloud property and conditionally start the app:

groovy

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testcase.TestCaseContext
import com.kms.katalon.core.testcase.TestSuiteContext
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.util.internal.ExceptionsUtil

public class CaseListener {
    String title_permission = "Please allow Maybank Trade to send you notifications"
    String packageName = "com.mbb.titan.uat.sg"
    private boolean isCloud = RunConfiguration.getExecutionProperties().get('isCloud') ?: false

    @BeforeTestCase
    public void beforeTestCase(TestCaseContext testCaseContext) {
        startApp()
        MainCase.allowNotification() // Handle notifications as before
    }

    @AfterTestCase
    public void afterTestCase(TestCaseContext testCaseContext) {
        handlePostTest(testCaseContext.getTestCaseStatus())
    }

    @BeforeTestSuite
    public void beforeTestSuite(TestSuiteContext testSuiteContext) {
        startApp()
        handlePermissionPrompt()
    }

    @AfterTestSuite
    public void afterTestSuite(TestSuiteContext testSuiteContext) {
        Mobile.takeScreenshot()
        handlePostTest(testSuiteContext.getStatus())
    }

    private void startApp() {
        try {
            if (isCloud) {
                Mobile.startApplication(packageName, false, FailureHandling.CONTINUE_ON_FAILURE)
            } else {
                Mobile.startExistingApplication(packageName)
            }
        } catch (Exception e) {
            ExceptionsUtil.printStackTrace(e)
            throw e
        }
    }

    private void handlePermissionPrompt() {
        if (Mobile.verifyElementExist(ObjectModel.actionFindTheObject(FIND_BY_TEXTVIEW, title_permission), 0, FailureHandling.OPTIONAL)) {
            Mobile.tap(ObjectModel.actionFindTheObject(FIND_BY_ACCESS_ID, "Not now"), 0)
        } else {
            println("Permission prompt not found, proceeding...")
        }
    }

    private void handlePostTest(String status) {
        if (status == "FAILED") {
            println("Test failed. Restarting app...")
            try {
                startApp()
            } catch (Exception e) {
                println("Error restarting: ${e.message}")
            }
        } else if (status == "PASSED") {
            Mobile.closeApplication()
        }
    }
}

3. Explanation

  • Dynamic Environment Check: The isCloud flag is retrieved from execution profiles using RunConfiguration.
  • Unified App Handling: The startApp() method decides between startApplication() and startExistingApplication() based on the profile.
  • Consolidated Post-Test Logic: The handlePostTest() method manages app closure/restart consistently across both environments.

4. Run Tests

  • Locally: Use the “Local” profile (no need to install the app each time).
  • On TestCloud: Use the “TestCloud” profile (app will be installed automatically).

This approach eliminates manual code changes and ensures compatibility with both environments.

Hi @alhafiy.mahdialhilmy,

Thank you for sharing your issue. I will check with my team and back to you soon.

@alhafiy.mahdialhilmy We previously addressed this issue in support case 00032760, opened by your colleague last month. We recommend checking with them for the resolution details. You may also find @dineshh’s suggestion helpful. Please don’t hesitate to reach out if you have any concerns. Thank you.

Hi,
Thank you for the reply. We have solve the issue. Thank you.

How did you do it?

Would you mind sharing your solution to others?