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?

Integrating KeywordLogger with Test Listeners in Katalon Studio

Hoca

Administrator
Staff member
Joined
Mar 22, 2024
Messages
414
Reaction score
0
Points
16
Effective logging is crucial in test automation. It helps testers track test execution, identify issues, and understand skipped steps. Katalon Studio’s KeywordLogger provides a versatile logging mechanism, which can be enhanced further by integrating with Test Listeners. This blog will guide you through the integration process, offering practical examples to illustrate the concepts.



What is KeywordLogger?


KeywordLogger is a logging utility in Katalon Studio that allows you to log different types of messages, such as informational messages, errors, and skipped steps. These logs provide a detailed account of the test execution flow, aiding in debugging and reporting.

Setting Up Test Listeners


To set up Test Listeners in Katalon Studio, you need to create a new Groovy script in the Test Listeners folder of your Katalon project. This script will define methods annotated with listener annotations like @BeforeTestCase, @AfterTestCase, @BeforeTestSuite, and @AfterTestSuite.

Creating a Test Listener

  1. Create the Test Listeners folder in your Katalon project.
  2. Create a new Groovy script. For instance, name it LoggingListener.

Integrating KeywordLogger with Test Listeners


Let’s create a comprehensive Test Listener that logs messages at different stages of the test lifecycle.



Step-by-Step Guide

  1. Import necessary classes:​


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.annotation.BeforeTestSuite

import com.kms.katalon.core.annotation.AfterTestSuite

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

import com.kms.katalon.core.context.TestSuiteContext



  1. Initialize the KeywordLogger:

KeywordLogger logger = new KeywordLogger()



  1. Define the methods with appropriate annotations:

@BeforeTestCase

def beforeTestCase(TestCaseContext testCaseContext) {

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

}

@AfterTestCase

def afterTestCase(TestCaseContext testCaseContext) {

if (testCaseContext.getTestCaseStatus() == 'FAILED') {

logger.logError("Test case failed: " + testCaseContext.getTestCaseId())

} else {

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

}

}

@BeforeTestSuite

def beforeTestSuite(TestSuiteContext testSuiteContext) {

logger.logInfo("Starting test suite: " + testSuiteContext.getTestSuiteId())

}

@AfterTestSuite

def afterTestSuite(TestSuiteContext testSuiteContext) {

logger.logInfo("Completed test suite: " + testSuiteContext.getTestSuiteId())

}



Full Example

Here’s the complete code for a Test Listener that integrates KeywordLogger:

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.annotation.BeforeTestSuite

import com.kms.katalon.core.annotation.AfterTestSuite

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

import com.kms.katalon.core.context.TestSuiteContext

KeywordLogger logger = new KeywordLogger()

@BeforeTestCase

def beforeTestCase(TestCaseContext testCaseContext) {

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

}

@AfterTestCase

def afterTestCase(TestCaseContext testCaseContext) {

if (testCaseContext.getTestCaseStatus() == 'FAILED') {

logger.logError("Test case failed: " + testCaseContext.getTestCaseId())

} else {

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

}

}

@BeforeTestSuite

def beforeTestSuite(TestSuiteContext testSuiteContext) {

logger.logInfo("Starting test suite: " + testSuiteContext.getTestSuiteId())

}

@AfterTestSuite

def afterTestSuite(TestSuiteContext testSuiteContext) {

logger.logInfo("Completed test suite: " + testSuiteContext.getTestSuiteId())

}



Practical Examples


Let’s look at some practical examples to understand how this integration works in real-world scenarios.

Example 1: Logging Test Case Execution


Imagine you have a test case for verifying user login functionality. By using the integrated Test Listener, you automatically log the start and end of the test case execution.

Test Case Script:

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('')

WebUI.navigateToUrl('https://example.com/login')

WebUI.setText(findTestObject('Page_Login/txt_Username'), 'username')

WebUI.setText(findTestObject('Page_Login/txt_Password'), 'password')

WebUI.click(findTestObject('Page_Login/btn_Login'))

WebUI.verifyElementPresent(findTestObject('Page_Home/lbl_LoggedIn'), 10)

WebUI.closeBrowser()




Logged Output:​


INFO: Starting test case: Test Cases/Verify User Login

INFO: Completed test case: Test Cases/Verify User Login



If the login fails, the output will include an error log:

INFO: Starting test case: Test Cases/Verify User Login

ERROR: Test case failed: Test Cases/Verify User Login



Example 2: Logging Test Suite Execution


Consider a test suite that runs multiple test cases related to user registration. The integrated Test Listener logs the start and end of the test suite.

Test Suite Script:

  • Verify User Registration
  • Verify User Registration with Invalid Data
  • Verify User Registration without Mandatory Fields



Logged Output:

INFO: Starting test suite: Test Suites/User Registration Tests

INFO: Starting test case: Test Cases/Verify User Registration

INFO: Completed test case: Test Cases/Verify User Registration

INFO: Starting test case: Test Cases/Verify User Registration with Invalid Data

ERROR: Test case failed: Test Cases/Verify User Registration with Invalid Data

INFO: Starting test case: Test Cases/Verify User Registration without Mandatory Fields

INFO: Completed test case: Test Cases/Verify User Registration without Mandatory Fields

INFO: Completed test suite: Test Suites/User Registration Tests



Best Practices for Effective Logging

  • Combine Logs for Clarity

By combining logInfo, logError, and logSkipped messages, you create a detailed and clear log that covers all aspects of your test case execution. This practice ensures you have comprehensive logs for easy debugging and reporting.

  • Use Test Listeners for Consistency

Integrating KeywordLogger with Test Listeners ensures that logs are consistently recorded at key points in your test lifecycle without manual intervention. This approach helps maintain a standardized logging format across all test cases and suites.

  • Monitor and Review Logs Regularly

Regularly monitoring and reviewing your logs helps identify patterns, recurring issues, and opportunities for improving your test scripts and overall testing process. Consistent log review ensures that any issues are quickly identified and addressed, enhancing the reliability of your tests.

Conclusion


Integrating KeywordLogger with Test Listeners in Katalon Studio provides comprehensive and consistent logs, automatically capturing key events throughout your test lifecycle.
 
Top Bottom