How to pass keywordutil as a parameter

How to pass KeywordUtil as a parameter in method signature i.e

image

@Keyword
	public static void elementCount(def locator, String message, keywordUtil???){

		List<WebElement> count = WebUiCommonHelper.findWebElements(testObject(locator), 5);

		if(count.size() > 1 || count.size() == 0){

			'Take ScreenShot'
			util.ScreenShot.function()

			KeywordUtil.????("**** CUSTOM ERROR *** " + message + " is display: " + count.size() + " record(s)" )
		}
	}

I doubt you can pass a method as a parameter in whatever OOP language, without advanced voodoo (dynamic programming)
What are you trying to achieve?

Thanks for your prompt response. Based on the method signature input …
I should perform any of the below

KeywordUtil.logInfo
KeywordUtil.markError
KeywordUtil.markFailedAndStop
KeywordUtil.markFailed

I don’t see or understand what your problem is. Just use the class.method you need.

Don’t forget to…

import com.kms.katalon.core.util.KeywordUtil

There’s no way (normally, at least) to do this by replacing the method name dynamically, as you state. This is due to a fundamental principle in object-oriented programming called “separation of data and function”. What you’re suggesting would essentially treat function as data, which isn’t generally a good idea.

The only way to really do what you’re trying to do would be a conditional statement, either an if/else bock or a switch statement:

public void method(String something) {
    if(something.equals("a")) {
        KeywordUtil.logInfo(...)
    } else if (something.equals("b")) {
        KeywordUtil.markError(...)
    } // and so on...
}

or

public void method(String something) {
    switch (something) {
        case "a":
             KeywordUtil.logInfo(...)
        case "b":
             KeywordUtil.markError(...)
        // and so on...
    }
}

There actually is a way to break this “rule” using reflection, however, I don’t generally recommend going this route unless you are extremely familiar with how reflection works.

1 Like

Sorry if haven’t conveyed my query clearly.

Thanks @Brandon_Hein, @anon46315158 @Russ_Thomas

The below approach solved my issue. I took the inspiration from one of @kazurayam custom keyword.

@Keyword
	public static void elementCount(int locatorCount = 1, String locator, String locatorName , FailureHandling flowControl){

		List<WebElement> count = WebUiCommonHelper.findWebElements(testObject(locator), 5);

		if (flowControl == FailureHandling.OPTIONAL) {
			if(count.size() > locatorCount || count.size() == 0){
				'Take ScreenShot'
				util.ScreenShot.function()
								
				KeywordUtil.logInfo("**** CUSTOM FailureHandling.OPTIONAL *** " + locatorName + " : " + count.size() + " record(s) found.")
			}


		} else if (flowControl == FailureHandling.CONTINUE_ON_FAILURE) {
			if(count.size() > locatorCount || count.size() == 0){
				'Take ScreenShot'
				util.ScreenShot.function()
								
				KeywordUtil.markFailed("**** CUSTOM FailureHandling.CONTINUE_ON_FAILURE *** " + locatorName + " : " + count.size() + " record(s) found." )
			}

		} else if (flowControl == FailureHandling.STOP_ON_FAILURE) {
			if(count.size() > locatorCount || count.size() == 0){
				'Take ScreenShot'
				util.ScreenShot.function()
						
				KeywordUtil.markFailedAndStop("**** CUSTOM FailureHandling.STOP_ON_FAILURE *** " + locatorName + " : " + count.size() + " record(s) found." )
			}
		}


	}

In the above method I am constructing Katalon-compatible TestObject in memory.