What's new

Welcome to lfkuu | Welcome My Forum

Join us now to get access to all our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, and so, so much more. It's also quick and totally free, so what are you waiting for?

Guide to Using KeywordLogger in Katalon Studio

Hoca

Administrator
Staff member
Joined
Mar 22, 2024
Messages
261
Reaction score
0
Points
16
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.
 
Top Bottom