Pages

01 December 2011

Defining Custom Loggers in SoapUI Groovy Script Assertions

Default logger in groovy script assertions of SoapUI, allows you to log into "script log" tab at the bottom of the page. Logs in this tab is volatile, read only and not easy for following errors and information. Thus, using such a logger is not gathering more benefits than printing information to system out.

I defined a custom file logger configuration (ASSERTION.SCRIPT.LOGGER) in soapui-log4j.xml that is located in the soapUI installation directory under bin folder. The configuration is a standard log4j configuration. Then add a script assertion to my test request. In groovy script, I create a logger instance with using logger name in configuration file as in the example below. The example groovy assertion script takes the current response time duration. After that checks if it is greater than 10 seconds; log the message in to file(ASSERTION.log).

Log4J Custom Configuration

<!-- custom logger for assertion scripts -->
<logger name="ASSERTION.SCRIPT.LOGGER">
  <level value="INFO"/>
  <appender-ref ref="ASSERTION"/>
</logger>

<appender name="ASSERTION" class="org.apache.log4j.RollingFileAppender">
 <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
 <param name="File" value="ASSERTION.log"/>
 <param name="Threshold" value="INFO"/>
 <param name="Append" value="false"/>
 <param name="MaxFileSize" value="10000KB"/>
 <param name="MaxBackupIndex" value="50"/>
 <layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
 </layout>     
</appender>

Groovy Assertion Script for Logging to File

import org.apache.log4j.Logger 
// ASSERTION.SCRIPT.LOGGER is defined in soapui-log4j.xml as below
def fileLogger = Logger.getLogger("ASSERTION.SCRIPT.LOGGER");
def timeTaken = messageExchange.timeTaken

if(timeTaken > 10000)  
 fileLogger.info "FAULT: SampleService.login() took:" + timeTaken

You can add an assertion script to your existing Test Request of Test Suite by following screenshots.

Adding Assertion to Test Request
Adding Assertion to Test Request

Selecting Type of Assertion
Selecting Type of Assertion

SoapUI Script Assertion Panel
SoapUI Script Assertion Panel

No comments:

Post a Comment

Thx for reading! Comments are appreciated...