Method mark status not as expected in test case log/report

I don’t know if anyone ever experience this or not. It’s pretty weird for me.

So I created custom keyword to take screenshot when using the KeywordUtil mark (because they don’t doing screenshot).

Here is my code:

package commonExtend

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

public class statusUtils {

	@Keyword
	def static captureFailAndStop(errorMessage) {
		WebUI.takeScreenshot()
		KeywordUtil.markFailedAndStop(errorMessage)
	}

	@Keyword
	def static captureFailAndContinue(errorMessage) {
		WebUI.takeScreenshot()
		KeywordUtil.markFailed(errorMessage)
	}

	@Keyword
	def static captureErrorAndStop(errorMessage) {
		WebUI.takeScreenshot()
		KeywordUtil.markErrorAndStop(errorMessage)
	}

	@Keyword
	def static captureErrorAndContinue(errorMessage) {
		WebUI.takeScreenshot()
		KeywordUtil.markError(errorMessage)
	}
}

When running in script:

statusUtils.captureErrorAndStop('Line 1')
statusUtils.captureErrorAndContinue('Line 2')
statusUtils.captureFailAndContinue('Line 3')
statusUtils.captureFailAndStop('Line 4')

Only line 1 and 3 will mark in test case report/log those step are error/fail and show in log the error message inputted.

But in the line 2 and 4, test step marked passed and only show log of the WebUI.takeScreenshot(): Taking screenshot successfully.

Anyone have solution for this or replacement for KeywordUtil to handling error/fail for my method?

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,
Elly Tran

I do not see what you want to achieve.


Possibly you do not have any idea what KeywordUtil class does. That should be the first problem you need to tuckle. The best way for understanding the class is to read the source code. I can tell you how to find the source code of the class.

You can find the source code of com.kms.katalon.core.util.KeywordUtil in the jar file: <Your Katalon_Studio installation folder>\configuration\resources\source\com.kms.katalon.core\com.kms.katalon.core-sources.jar

I would quote the source of v10.3.1 as follows:

package com.kms.katalon.core.util;

import com.kms.katalon.core.exception.StepErrorException;
import com.kms.katalon.core.exception.StepFailedException;
import com.kms.katalon.core.logging.ErrorCollector;
import com.kms.katalon.core.logging.KeywordLogger;

public class KeywordUtil {
    
    private static final KeywordLogger logger = KeywordLogger.getInstance(KeywordUtil.class);
    
    /**
     * Mark a keyword to be failed and continue execution
     * 
     * @param message fail message
     */
    public static void markFailed(String message) {
        logger.logFailed(message);
        ErrorCollector.getCollector().addError(new StepFailedException(message));
        ErrorCollector.getCollector().setIsNextStepSkipped(true);
    }

    /**
     * Mark a keyword to be failed and stop execution
     * 
     * @param message fail message
     */
    public static void markFailedAndStop(String message) {
        ErrorCollector.getCollector().setStopLauncher(true);
        throw new StepFailedException(message);
    }

    /**
     * Log message as info
     * 
     * @param message log info message
     */
    public static void logInfo(String message) {
        logger.logInfo(message);
    }

    /**
     * Mark a keyword to be warning
     * 
     * @param message warning message
     */
    public static void markWarning(String message) {
        logger.logWarning(message);
    }

    /**
     * Mark a keyword to be passed
     * 
     * @param message passed message
     */
    public static void markPassed(String message) {
        logger.logPassed(message);
        ErrorCollector.getCollector().setKeywordPassed(true);
    }
    
    /**
     * Mark a keyword to be error
     * 
     * @param message error message
     */
    public static void markError(String message) {
        ErrorCollector.getCollector().addError(new StepErrorException(message));
        ErrorCollector.getCollector().setIsNextStepSkipped(true);
    }
    
    /**
     * Mark a keyword to be error and stop execution
     * 
     * @param message error message
     */
    public static void markErrorAndStop(String message) {
        ErrorCollector.getCollector().setStopLauncher(true);
        throw new StepErrorException(message);
    }
}

@qaat.hanh you wanna take screenshot whenever your test step fails?

Yeah, beacause KeywordUtil doesn’t take screenshoot if it fail or error. So i write a function to do it both in one step in test case.

But when running, 2 captureErrorAndStop and captureFailAndContinue will run to the KeywordUtil that send log to report. 2 functions captureErrorAndContine and captureFailAndStop only run and giving log at the takeScreenshot.

Look like it because of markError and markFailedAndStop doesn’t working properly. But what cause this?? :frowning:

What do you mean by “properly”? I don’t see it.

So I would guess … It seems that you want KeywordUtil.markFailedAndStop to display the message “Test fail and stop” in the Log Viewer tab, am I right?

I tried to reproduce what you saw. Yes, you are right. I saw the KeywordUtil.markFailedAndStop displayed no message in the Log Viewer tab. You don’t like it, do you?

However, I saw the KeywordUtil.markFailedAndStop displayed the message “Line 4” in the Console tab.

In a preceding post in this topic, I showed you the source of com.kms.katalon.core.util.KeywordUtil class. You can find the implementation of the markFailedAndStop method:

    public static void markFailedAndStop(String message) {
        ErrorCollector.getCollector().setStopLauncher(true);
        throw new StepFailedException(message);
    }

As far as I see, the markFailedAndStop works as coded; it will display no message in the Log Viewer tab, it will display the message Line 4 in the Stacktrace in the Console tab.

By the way, you can also see the source of markFailed method:

    public static void markFailed(String message) {
        logger.logFailed(message);
        ErrorCollector.getCollector().addError(new StepFailedException(message));
        ErrorCollector.getCollector().setIsNextStepSkipped(true);
    }

The markFailedAndStop and markFailed — these 2 methods have the similar names but are coded quite differently. I have no idea what is the design intention of KeywordUtil class. I don’t know the reason why these methods are implemented as such. Perhaps Katalon has some good reasons.

The KeywordUtil class is given to you. Why not you embrace the given KeywordUtil class as is? or should you invent your own alternative? It is up to you.

if inbuilt function doesn’t suit your use case, why not create custom function from scratch based on your needs

Thanks for helping everyone. I will make A custom keyword for my own use