Skip to main content

Quality Assurance

Guide to Using KeywordLogger in Katalon Studio

Mobile Apps Developers At The Office.

Logging is an essential part of test automation. It helps you keep track of what your tests are doing, spot errors, and understand why certain steps were skipped. In Katalon Studio, KeywordLogger is a handy tool that makes logging easy and effective. This guide will show you how to use KeywordLogger with simple examples.

What is KeywordLogger?

KeywordLogger is a feature in Katalon Studio that lets you log different types of messages. These messages can be:

  • Informational: To tell you what’s happening in your test.
  • Errors: To alert you when something goes wrong.
  • Skipped: To indicate when a step is skipped.

 

Basic Usage

First, you need to create a logger object and then use its methods to log messages. Here’s a simple example:

KeywordLogger logger = new KeywordLogger()

logger.logInfo("This is an info message")

logger.logError("This is an error message")

logger.logSkipped("This is a skipped message")
Console Output

Console Output

Log Viewer

Breaking Down the Example

  1. Import KeywordLogger: You need to include KeywordLogger in your script.
  2. Create a logger: KeywordLogger logger = new KeywordLogger()
  3. Log messages: Use logInfo, logError, and logSkipped methods.

 

Practical Examples

  • Logging Informational Messages

Use logInfo to track the flow of your test.

Example: Start and End of a Test Case

import com.kms.katalon.core.logging.KeywordLogger

KeywordLogger logger = new KeywordLogger()

logger.logInfo("Starting the test case: Verify Login")

// Simulate login test steps

try {

    performLogin("username", "password")

    logger.logInfo("Login successful")

} catch (Exception e) {

    logger.logError("Login failed: " + e.message)

}

logger.logInfo("Ending the test case: Verify Login")

 

  • Logging Errors

Use logError to capture any issues that occur.

 

Example: Form Submission Error

import com.kms.katalon.core.logging.KeywordLogger

KeywordLogger logger = new KeywordLogger()

try {

    submitForm("formData")

    logger.logInfo("Form submitted successfully")

} catch (Exception e) {

    logger.logError("Form submission failed: " + e.message)

}

 

  • Logging Skipped Steps

Use logSkipped to note when a test step is conditionally skipped.

 

Example: Skipping Based on a Condition

import com.kms.katalon.core.logging.KeywordLogger

KeywordLogger logger = new KeywordLogger()

boolean isFeatureEnabled = checkFeatureToggle("newFeature")

if (isFeatureEnabled) {

    try {

        testNewFeature()

        logger.logInfo("New feature test executed")

    } catch (Exception e) {

        logger.logError("New feature test failed: " + e.message)

    }

} else {

    logger.logSkipped("New feature is disabled, test skipped")

}

 

Advanced Usage: Test Listeners

Test Listeners in Katalon Studio allow you to log messages automatically at different stages of your test lifecycle, such as before a test case starts and after it ends.

Example: Using Test Listeners


import com.kms.katalon.core.logging.KeywordLogger 

import com.kms.katalon.core.annotation.BeforeTestCase 

import com.kms.katalon.core.annotation.AfterTestCase 

import com.kms.katalon.core.context.TestCaseContext

@BeforeTestCase

    def beforeTestCase(TestCaseContext testCaseContext) {

       KeywordLogger logger = new KeywordLogger()

       logger.logInfo("Starting test case: " + testCaseContext.getTestCaseId())

    }

@AfterTestCase

 def afterTestCase(TestCaseContext testCaseContext) {

    KeywordLogger logger = new KeywordLogger()

    logger.logInfo("Completed test case: " + testCaseContext.getTestCaseId())

 }

 

Output

2024 06 05 17 56 24 Katalon Studio 8.6.8 C690de7c Salesforceqa Location C Users Sanket.dudh

2024 06 05 17 51 54 Katalon Studio 8.6.8 C690de7c Salesforceqa Location C Users Sanket.dudh

Conclusion

Using KeywordLogger in Katalon Studio is straightforward and highly beneficial for tracking your tests, spotting errors, and understanding skipped steps. By following the examples in this guide, you can easily incorporate logging into your test scripts, making them more robust and easier to debug.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sanket Dudhe

Sanket Dudhe is a Technical Consultant at Perficient. He has an experience of 4+ years as SDET. He loves technology and hence is curious to learn about new emerging technologies #lovefortechnology.

More from this Author

Categories
Follow Us