cancel
Showing results for 
Search instead for 
Did you mean: 

SoapUI script logging content access when running with Maven or command line

SOLVED
Highlighted
Contributor

SoapUI script logging content access when running with Maven or command line

Hi,

 

All the Groovy script log contents of a test step or from a script assertion of a request test step,  can be fetched using the below line 

com.eviware.soapui.SoapUI.logMonitor.getLogArea("script log") )

 

cos we defining the log area here as script log . fine  ... This is fine if we add this line of script as part of set up or a tear down script of a test case and when the execution happens from SoapUI GUI tool...

 

 

But same will not work when we using testRunner or Maven plugin to run the project file, as it can not instantiate the getLogArea because I guess the logMonitor is meant for only UI tool...

 

Do we have any way to make this possible when running the project file using Maven in a comman line

or any work around also will be helpful...

 

Thanks & Warm Regards

Musaffir

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

Re: SoapUI script logging content access when running with Maven or command line

Hello @nmrao

 

To give you more details on what I was trying to achieve here ,

 

I have a test suite where I have many test cases , each test cases hitting on different API resources to fetch the response back. And in the tc request test step I have Script Assertions to validate the responses and various other things , I also try to fetch the full response contents in the same script assertions . And I am trying to log it , all responses and the results of  other assertions etc .

And all these done by Groovy scripting ..

 

So basically I am interested to fetch all the Groovy Log contents , and I am also interested to write individual test case specific script logs to individual external file say a txt file ... this is for my later check and reference on response contents and other things.

 

When I perform the execution with SoapUI tool it self , this was easy to fetch the script logs with ,

def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "script log" );

 

but this won't work when the same suite runs using Maven plugin, it will give a NullPointerException for the logMonitor part..

so I was looking for an another approach to get this done when the suite runs with maven or command line..

 

 

@ankush

 

Now I figured out the below approach,

 

In SoapUI ,all the logging activities are done with the log4j framework, and default log4j configuration file can be seen in your soapUI installation {bin\soapui-log4j.xml}..

I am not modifying any thing in this xml file , instead I wrote a custom logger class using groovy which will get the default config logger instance with name ''groovy.log'' ... this 'groovy.log' can be seen in the above xml file ... to this instance I am applying a rolling appender with my own pattern layout 

so the sample of groovy class will look like ,

 

import org.apache.log4j.FileAppender
import org.apache.log4j.Logger
import org.apache.log4j.PatternLayout
import org.apache.log4j.RollingFileAppender

class LoggerClass {

def projectPath

// constructor
LoggerClass(def projectPath)
{
this.projectPath=projectPath
}


def Logger getLogger(def testCaseName)
{
def path = "//target//groovy-explicit-logs//"
def fileName = projectPath + path +testCaseName+".log"
def groovyLogger = Logger.getLogger('groovy.log')

PatternLayout layout = new PatternLayout("%d{HH:mm:ss,SSS} %-5p %x - %m%n")

RollingFileAppender fileAppender = new RollingFileAppender(layout, fileName)

groovyLogger.addAppender(fileAppender)

return groovyLogger
}

}

 

//testCaseName - is my test case name

//projectPath - is my project path

// just passing this during run to keep the log file in that location..

 

 

I will compile this groovy class and make a jar of it , and then keeps in the classpath . 

If you running soapUI tool, in the {ext} folder of installation. or in the Maven class path if i am running using maven..

 

 

Later in each test case script assertion I will invoke this logger.and using that logger instance I will do all my logging activities

 

import net.LoggerClass
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
def testCase = messageExchange.modelItem.testStep.testCase.name

 

// this is the class you have written earlier,
LoggerClass loggerClass = new LoggerClass(projectPath)
def log = loggerClass.getLogger(testCase)

 

log.info "log what ever you want .."

 

Finally I was achieving individual test case groovy log contents in individual log files it self ...

 

@nmrao

Please let me know if you have come across any better approach.

 

 

Thanks & Warm Regards

Musaffir

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View solution in original post

8 REPLIES 8
Highlighted
Occasional Contributor

Re: SoapUI script logging content access when running with Maven or command line

Hi ,

 

I am facing the same issue.

 

Any luck yet?

Highlighted
Community Hero

Re: SoapUI script logging content access when running with Maven or command line

What are you trying to achieve? or what is the use case?


Regards,
Rao.
Highlighted
Contributor

Re: SoapUI script logging content access when running with Maven or command line

Hello @nmrao

 

To give you more details on what I was trying to achieve here ,

 

I have a test suite where I have many test cases , each test cases hitting on different API resources to fetch the response back. And in the tc request test step I have Script Assertions to validate the responses and various other things , I also try to fetch the full response contents in the same script assertions . And I am trying to log it , all responses and the results of  other assertions etc .

And all these done by Groovy scripting ..

 

So basically I am interested to fetch all the Groovy Log contents , and I am also interested to write individual test case specific script logs to individual external file say a txt file ... this is for my later check and reference on response contents and other things.

 

When I perform the execution with SoapUI tool it self , this was easy to fetch the script logs with ,

def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "script log" );

 

but this won't work when the same suite runs using Maven plugin, it will give a NullPointerException for the logMonitor part..

so I was looking for an another approach to get this done when the suite runs with maven or command line..

 

 

@ankush

 

Now I figured out the below approach,

 

In SoapUI ,all the logging activities are done with the log4j framework, and default log4j configuration file can be seen in your soapUI installation {bin\soapui-log4j.xml}..

I am not modifying any thing in this xml file , instead I wrote a custom logger class using groovy which will get the default config logger instance with name ''groovy.log'' ... this 'groovy.log' can be seen in the above xml file ... to this instance I am applying a rolling appender with my own pattern layout 

so the sample of groovy class will look like ,

 

import org.apache.log4j.FileAppender
import org.apache.log4j.Logger
import org.apache.log4j.PatternLayout
import org.apache.log4j.RollingFileAppender

class LoggerClass {

def projectPath

// constructor
LoggerClass(def projectPath)
{
this.projectPath=projectPath
}


def Logger getLogger(def testCaseName)
{
def path = "//target//groovy-explicit-logs//"
def fileName = projectPath + path +testCaseName+".log"
def groovyLogger = Logger.getLogger('groovy.log')

PatternLayout layout = new PatternLayout("%d{HH:mm:ss,SSS} %-5p %x - %m%n")

RollingFileAppender fileAppender = new RollingFileAppender(layout, fileName)

groovyLogger.addAppender(fileAppender)

return groovyLogger
}

}

 

//testCaseName - is my test case name

//projectPath - is my project path

// just passing this during run to keep the log file in that location..

 

 

I will compile this groovy class and make a jar of it , and then keeps in the classpath . 

If you running soapUI tool, in the {ext} folder of installation. or in the Maven class path if i am running using maven..

 

 

Later in each test case script assertion I will invoke this logger.and using that logger instance I will do all my logging activities

 

import net.LoggerClass
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
def testCase = messageExchange.modelItem.testStep.testCase.name

 

// this is the class you have written earlier,
LoggerClass loggerClass = new LoggerClass(projectPath)
def log = loggerClass.getLogger(testCase)

 

log.info "log what ever you want .."

 

Finally I was achieving individual test case groovy log contents in individual log files it self ...

 

@nmrao

Please let me know if you have come across any better approach.

 

 

Thanks & Warm Regards

Musaffir

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View solution in original post

Highlighted
Occasional Contributor

Re: SoapUI script logging content access when running with Maven or command line

@Musaffir if I understand this correct. you just want to log all your activities.

 

There is a much simpler way to do this.

Just use log.info(<anything you want to log>) in your groovy script .

 

the log is available by default with the Groovy test step, you don't need to write your own solution to log everything.

 

Whatever you log through the log object in Groovy will generate a global-groovy.log file .

Highlighted
Contributor

Re: SoapUI script logging content access when running with Maven or command line

@ankush

 

Yes log.info "something" will create a global groovy log file ,as this log instance is from the default log4j configuration.

But I don't want all the logs , i.e logs generated from different test cases etc to be stored in the single same global groovy file.

It will be difficult to go through a single file especially when you have a suite with many test case.

I would like to see them in individual log files , so I am customizing it for my needs .

 

Cheers

Musaffir

 

Highlighted
Occasional Contributor

Re: SoapUI script logging content access when running with Maven or command line

Okay, so you want a log for individual test cases.

 

I think your solution is fine.

You can simply write the code as a test suite/ test case setup script instead of packaging as a jar .

Highlighted
Contributor

Re: SoapUI script logging content access when running with Maven or command line

@ankush

 

Nope , that won't help here .

I see couple of reasons for it 

1. If you compile your groovy class and package it as a jar , you can utilize it across multiple projects . It is going to be more of a general feature available to you . you can simple import it and use it where ever you want it .No need to write repeated test case scripts at each test case level.

2. Also its better not to override the log4j configs with in the test case level

 

Cheers

Musaffir

Highlighted
Contributor

Re: SoapUI script logging content access when running with Maven or command line

Hello Mussafir, 

 

I have exact same issue. I posted it sometime back and saw that you have already solved it. I tried to comprehend the steps but not clear on what to do after adding your code to a groovy script? Can you help share some more details on how you add your code to SoapUI (and where) and how and if you pass it to command line? Thanking you in anticipation and kudos to you for figuring out to fix this problem.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors