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”)
Breaking Down the Example
Import KeywordLogger: You need to include KeywordLogger in your script.
Create a logger: KeywordLogger logger = new KeywordLogger()
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.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
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.
Source: Read MoreÂ