free seo tool

Capture soapUI Logs to File

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 ))       
      
   }


38 comments:
  • Ajay says:
    April 28, 2012

    very nice tip. Can you also explain it's application, when this tip would be really useful?

  • Sudeesh says:
    April 29, 2012

    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

  • Anonymous says:
    April 30, 2012

    What is the best place to place this script so all the groovy step outputs are logged into the file?

  • Sudeesh says:
    April 30, 2012

    You might try this on test case / test suite tear down script section. May require slight changes on getting the project path thats all

  • Anonymous says:
    May 01, 2012

    Running the same script in test suite tear down area works, however the generated log file not showing any log messages.

  • Sudeesh says:
    May 01, 2012

    Does the "soapUI log" tab has anything displayed?

  • Anonymous says:
    May 02, 2012

    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.

  • Swati says:
    May 07, 2012

    Wooww!!!! dat was soo quick Sudeesh. Thanks for introducing this new thread. Your blog is simply amazing

  • Swati says:
    May 07, 2012

    Hey Sudeesh,

    I have few queries in Groovy step will you be able to help??

    Thanks!!!!

  • Sudeesh says:
    May 08, 2012

    Please feel free to post the queries. I will try to address it in few days.

  • Anonymous says:
    May 09, 2012

    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.

  • Sudeesh says:
    May 09, 2012

    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.

  • Anonymous says:
    May 09, 2012

    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?

  • Swati says:
    May 10, 2012

    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!!!!

  • Anonymous says:
    May 11, 2012

    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.

  • Anonymous says:
    May 16, 2012

    Any answer for this Sudeesh?

  • Sudeesh says:
    May 16, 2012

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

  • Swati says:
    May 27, 2012

    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

  • Anonymous says:
    May 29, 2012

    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.

  • Sudeesh says:
    May 31, 2012

    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.

  • Sudeesh says:
    May 31, 2012

    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.

  • Anonymous says:
    June 01, 2012

    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

  • Anonymous says:
    August 18, 2013

    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?

  • Anonymous says:
    November 29, 2013

    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

  • Anonymous says:
    November 29, 2013

    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.

  • KiranKumarT says:
    September 15, 2014

    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

  • Anonymous says:
    January 20, 2015

    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?

  • Anonymous says:
    February 03, 2015

    Please note, that you have to change the getLogArea Line! It has to be "( "SoapUI log" ); --- not ( "soapUI log" );

  • NDAYashoda says:
    March 03, 2016

    Can you tell what is the default path of saved files?

  • Anonymous says:
    March 30, 2016

    If you are getting error on line 3. Try using this
    def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea( "http log" );

  • Anonymous says:
    August 30, 2016

    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 ?

  • Anonymous says:
    December 12, 2016

    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.

  • simplehumblecooking says:
    March 23, 2017

    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 ?

  • P PAL says:
    May 30, 2017

    Is there any way to save "TestSuite Log" in soapUI using groovy to an external file.

  • Gaurav Khurana says:
    September 08, 2017

    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

  • Anonymous says:
    January 17, 2021

    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?

  • Anonymous says:
    April 05, 2022

    how to get http status for all apis/teststeps in groovy script

  • Skanda Bhat says:
    April 28, 2022

    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)

Post a Comment

This is a new website mainly for SOA Testers using soapUI. Let me know your thoughts/ suggestions.