Forum Discussion

jkrier's avatar
jkrier
Regular Contributor
8 years ago

Exporting the Script Log to an external file

I would like to be able add a Groovy script in a Teardown Script from either Project, Test Suite, or Test Case or even in an Event handler that exports the "current" output of the Script Log to an external file.

 

I had opened a message about this in the past but the person who was "helping" me decided to stop helping me. That thread is at https://community.smartbear.com/t5/SoapUI-NG/Exporting-ReadyAPI-logs-to-an-external-file/td-p/128118

 

Basically what I was told was that because ReadyAPI! already logs all Script Log output to a file that I shouldn't have any need to do what I am doing but unfortunately not everyone's needs are the same.

 

I have tried something new from what was in my previous thread such as

 

timeStamp = System.currentTimeMillis()
fileName = log.name
new File("C:/MyFolder"+timeStamp+fileName).withWriter { writer ->
    writer.writeLine fileName
}

My SoapUI project runs without error but nothing is created. I would prefer to do this without having to edit the log4j.xml file; I don't need a new file every time I run something but at some point my SoapUI project will be complete and other people will be running it and it would be beneficial if the Script Log output could live in the same working directory as the SoapUI project, which will not be the same every time for every project.

  • Hello there! This is interesting because I am almost using similar script you pointed in your earlier discussion and it works! Currently I am exporting all the script logs via Event Handler. I am capturing logs and exporting them to a file after entire test-suite runs. Also please note that log file derives its name from TestSuite Name.

     

    My events are at TestSuite level 

     

    Clean up previous log entities  - TestSuiteRunListener.beforeRun

     

    //Clear Script Logs
    com.eviware.soapui.SoapUI.logMonitor.getLogArea("Script log").clear()

    //## Get TestSuite name ##//
    def TSName = testRunner.testSuite.name

    //Get LogFile directory and Construct LogFile
    def LogFilePath = context.expand('${#Project#TestReportPath}')
    def LogFile = TSName + "-Logs.txt"
    LogFile = LogFilePath + LogFile

    def LogFiles = new File(LogFile)

    log.info "Clearing previous logs..."

    LogFiles.delete()

     

    Write script logs to a file - TestSuiteRunListener.afterRun

     

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

    //## Get TestSuite name ##//
    def TSName = testRunner.testSuite.name

    //Get LogFile directory and Construct LogFile
    def FileName = TSName + "-Logs.txt"
    def LogFile = new File(context.expand('${#Project#TestReportPath}') + FileName)

    LogFile.write("Generating Script logs....\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).toString() + "\r\n")
    }
    }
    }

     

    Hope this helps!

  • New2API's avatar
    New2API
    Frequent Contributor

    Hello there! This is interesting because I am almost using similar script you pointed in your earlier discussion and it works! Currently I am exporting all the script logs via Event Handler. I am capturing logs and exporting them to a file after entire test-suite runs. Also please note that log file derives its name from TestSuite Name.

     

    My events are at TestSuite level 

     

    Clean up previous log entities  - TestSuiteRunListener.beforeRun

     

    //Clear Script Logs
    com.eviware.soapui.SoapUI.logMonitor.getLogArea("Script log").clear()

    //## Get TestSuite name ##//
    def TSName = testRunner.testSuite.name

    //Get LogFile directory and Construct LogFile
    def LogFilePath = context.expand('${#Project#TestReportPath}')
    def LogFile = TSName + "-Logs.txt"
    LogFile = LogFilePath + LogFile

    def LogFiles = new File(LogFile)

    log.info "Clearing previous logs..."

    LogFiles.delete()

     

    Write script logs to a file - TestSuiteRunListener.afterRun

     

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

    //## Get TestSuite name ##//
    def TSName = testRunner.testSuite.name

    //Get LogFile directory and Construct LogFile
    def FileName = TSName + "-Logs.txt"
    def LogFile = new File(context.expand('${#Project#TestReportPath}') + FileName)

    LogFile.write("Generating Script logs....\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).toString() + "\r\n")
    }
    }
    }

     

    Hope this helps!

    • jkrier's avatar
      jkrier
      Regular Contributor

      Thank you so much, this works!!!! This is so helpful I can't thank you enough. I had to change the path because I was getting permission errors but that's OK I would have done that anyways. I just changed it to use the path of the project, which is how I want it to be used.

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Jkrier,

     

    There is no built-in way to export the Script Log. However, you can write data, which you usually post to the Log, to your external file. There will be your own export function you will need to use instead of log.info. In this case, all script-related data will be stored in the external file instead of the Script Log. 

    You can use the following property to get the path to your project so that you could keep the external file in the same folder:

    context.testCase.testSuite.project.path

    Does it help?

     

    • jkrier's avatar
      jkrier
      Regular Contributor

      Hi Tanya,

       

      I was aware that I could do this already. I was really hoping that someone knew of a way to export the "Script Log" or "groovy.log" that is created when the runner is kicked off. It would be so much easier if this was possible I think. Thank you for responding.