In this Groovy Script example, lets see how can we capture the soapUI logs and save it into a file. At the bottom of soapUI, you might have noticed different tabs named soapUI log, http log, jetty log, error log, wsrm log, memory log and script log. Each tab will have the logs based on log category. For example, if you are invoking a Web Service request/ response, you will be able to see the web service request and response with the http headers under http tab. Same way if you have groovy script to on your soapUI test steps and if those groovy scripts are printing any dynamic values while test execution, you might also see that in the tab "script log". Lets see the below groovy script to capture the logs and save it into a text file.
//The below groovy script step is to capture the soapUI log area.
// "soapUI log" can be replaced with http log, jetty log, script log, error log etc based on the need.
def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "soapUI log" );
//Below two lines of groovy script is to get the project directory, we will be saving the soapUI log contents on a file in that directory.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
def logFile = new File(projectPath + "\\soapUI-Logs-to-file.txt")
//This is the first line in the soapUI log file.
logFile.write("soapUI Logs In a file.\r\n")
if( logArea !=null )
{
def model = logArea.model
if( model.size > 0 )
for( c in 0..(model.size-1) )
logFile.append(model.getElementAt( c ))
}
very nice tip. Can you also explain it's application, when this tip would be really useful?
Hi Ajay..
Say you have a groovy script test step which has some logic in it and if you want to get the output in a file, you can print it and save the log at the end of test execution. Or else if you want to capture the http header information of a request and response you can do that too. Hope this helps
What is the best place to place this script so all the groovy step outputs are logged into the file?
You might try this on test case / test suite tear down script section. May require slight changes on getting the project path thats all
Running the same script in test suite tear down area works, however the generated log file not showing any log messages.
Does the "soapUI log" tab has anything displayed?
I changed following line with "script log"
def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "soapUI log" );
My script log tab has few log statements to display.
Wooww!!!! dat was soo quick Sudeesh. Thanks for introducing this new thread. Your blog is simply amazing
Hey Sudeesh,
I have few queries in Groovy step will you be able to help??
Thanks!!!!
Please feel free to post the queries. I will try to address it in few days.
Hi Sudeesh
One query over accessing objects and variables in the Groovy script.
Can I access objects at each test case level which are retrieved in the Project Load script?
For example I get config object after I read property file using ConfigSlurper class or even projectPath variable which is retrieved using GrrovyUtils class in the project load script.
I would like to use (access) them in my test case level groovy scripts rather than retrieving them again. Currently I receive error that it is a not property of Script.
Is there any concept like global variable/object in Groovy, something similar to Member variable in Java which is accessible throughout soapUI project?
Thanks in advance.
Really a good question. For each and every object in soapUI, scope is predefined and that is the reason you are getting error. Regarding saving an object in a global variable, I have to try that to answer. Will keep you posted.
Let me answer by question partially.
I know how can I access variable by making it as a custom variable of the project.
What about objects which are not simply variables? In my case, config object which I get after reading property file?
Hey Sudeesh,
I am not able to attach any screen shot here. Can you plz share your email id by sending a test mail at swati.kaul22@gmail.com
Thanks!!!!
Hi Sudeesh
Do you have a script to create groovy test step, delay test step or any JDBC test step automatically in the Project load script?
I want to use this script so that it creates these mandatory test steps in each test case.
I can save the time of creating them manually as well as I make sure that all my test cases have mandatory test steps.
Further I want to add basic assertions for all wsdl test steps through script. But this is not on my priority.
Can you help in creating groovy, delay and JDBC test steps through groovy script?
Thanks.
Any answer for this Sudeesh?
I believe if you can create a temporary test case with all your required steps, you can clone that on project load. Try the below code on project load.
project.getTestSuiteByName("soapUI-TestSuite").cloneTestCase(project.getTestSuiteByName("soapUI-TestSuite").getTestCaseByName("soapUI-testCase"), "soapUI-New-testCase")
Hope this helps..
Hey Sudeesh,
I have few queries for this article I would really appreciate if you can reply to these queries in ur free time. Below mentioned are those queries.
1- My all script logs are logging in a single line I want to have them line by line just as they are shown up in Soap UI .
2- Can we also capture "TestCase log" which gets captured after running a suite from test case editor.
Would really appreciate if you can share ur comments on the above queries.
Thanks!!!
Regards,
Swati
Hi Sudeesh
Sometimes back I had asked you question regarding storing object in the context or somewhere so that we can access it later point of time may be in each test case level groovy scripts.
In the Project load script, I store config object inside context variable, which I have received from ConfigSlurper class.
Later in the test case level groovy script step, I try to retrieve config object from the Context variable like
def config = context.config
and just print config however it is null.
I am just wondering where is that config object which I had stored in the context variable at project load script?
Any clue you can give? I wanted to use the config object further to read property file.
Hi Swati,
Answer to your first question. Try replacing the line "logFile.append(model.getElementAt( c ))" in the above example to "logFile.append(model.getElementAt( c ).toString() + "\r\n") "
Answer to your second question:
Try this on test case tear down script.
Hi Anonymous,
Even if you store the details on context object, you may not be able to access it in other places as the scope and availability of the objects are different in different places in soapUI.
Hi Sudeesh
Thank you for the answer. This looks to me a drawback of soapUI or may be I am not aware of how to achieve object state & value transfer across project.
Thanks.
Rgds
AJ
Hi,
i used the above code and trying to print the whole content of script log in a file. But after running the test case, i am seeing only partial content written in the file. Could you please help me?
Hi ..
Can you tell me the code pls how to write the status of each test step in soap ui..
Iam trying to write the code from so many days, but still its not working for me
Hie,
I need your help as from past few days I am trying to write the code to get the status of each test step in file using SOAPUI. Can anybody help me on this issue, asap.
Thanks in advance.
i am not able to invoke the above script through command line
getting the below exception
TestStep: Groovy Script
Error:java.lang.NullPointerException: Cannot invoke method getLogArea() on null object
Hi, it is possible to save the log output to an external file? I mean log.info, log.warn statements? if yes, then how to do it?
Please note, that you have to change the getLogArea Line! It has to be "( "SoapUI log" ); --- not ( "soapUI log" );
Can you tell what is the default path of saved files?
If you are getting error on line 3. Try using this
def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "http log" );
suppose my request fails due to certificate error and i want to capture the error in my results also i want to abort the test case if error is received , how can this be achieved ?
Thanks! I used and tweaked this script as per my requirement. I am using this script in event handler section. The issue is, if I am running tests in commandline then Script log area is not accessible or soapui is not writing logs in that section. How to make this work when running soapui from commandline.
Hi,
I too experience the same thing, I run the suite using Maven
And there are a lot of groovy script logs which i would like to see in a file ...
It works when running with the soapui tool... but not using maven in command line , the reason is prety clear the log area monior is null there ...
is ther any other way to fetch the script log from command line ?
Is there any way to save "TestSuite Log" in soapUI using groovy to an external file.
THanks a lot.. its really helpful.. Only correction required in above step is the name of soapui log
def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "SoapUI log" )
S should be capital and UI also
While running as separate test step, it works fine.
logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "tools" )
Getting error while using TestRunner as below:
java.lang.NullPointerException: Cannot invoke method getLogArea() on null object
Any idea on this?
how to get http status for all apis/teststeps in groovy script
def httpResponseHeaders = context.testCase.testSteps[i.name].testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]
log.info( httpStatusCode)