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
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..
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.RollingFileAppenderclass 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 ...
Please let me know if you have come across any better approach.
Thanks & Warm Regards
Musaffir