Forum Discussion

Sriraj's avatar
Sriraj
Contributor
8 years ago

Structured logging of the results using EventHandler

Hi, 

 

Please navigate to the below path section 8 Logging results

 

https://www.soapui.org/scripting-properties/tips-tricks.html#1-4-Test-step-name

 

The code helps in creating the request and response files for each step but all of them get created in one location/folder

 

Can someone help me in extending this to have the Project name , Suite name and the Test case names as folders ?

 

Something like below 

 

  • Parent Folder : ProjectName_CurrentTimestamp
    • Child Folder (Inside Parent Folder) : <TestSuitename>
      • Child Folder (Inside Test suite Folder) : <TestCaseName>
        • Here place all the request response files

Actual code from the path provided above : 

 

filePath = 'c:/users/henrik/soapUI-results/'
fos = new FileOutputStream( filePath + testStepResult.testStep.label + '.txt', true )
pw = new PrintWriter( fos )
testStepResult.writeTo( pw )
pw.close()
fos.close()

Basically i want a structured logging of the results.

 

 

  • Hi Sriraj,

     

    You can use the following code in the TestRunListener.afterStep event handler to create such a folders structure:

     

     

    def pName = context.currentStep.testCase.testSuite.project.name //get project name
    def pDate = new Date().format( 'yyyyMMdd' )//get current date
    def sDate = pDate.toString()//convert date to string
    def pTestSuite = context.currentStep.testCase.testSuite.name//get TestSuite name
    def pTestCase = context.currentStep.testCase.name//get TestCase name
    def filePath = 'D:/soapUI-results/'+pName+'_'+sDate+'/'+pTestSuite+'/'+pTestCase+'/'//compose the folder path
    
    File file = new File(filePath)
    if (!file.exists()) file.mkdirs()//create the destination folder
    
    fos = new FileOutputStream(filePath+ testStepResult.testStep.label + '.txt', true)
    pw = new PrintWriter( fos )
    testStepResult.writeTo( pw )
    pw.close()
    fos.close()

    I hope this helps.

  • LexiAQA's avatar
    LexiAQA
    SmartBear Alumni (Retired)

    Hi Sriraj,

     

    You can use the following code in the TestRunListener.afterStep event handler to create such a folders structure:

     

     

    def pName = context.currentStep.testCase.testSuite.project.name //get project name
    def pDate = new Date().format( 'yyyyMMdd' )//get current date
    def sDate = pDate.toString()//convert date to string
    def pTestSuite = context.currentStep.testCase.testSuite.name//get TestSuite name
    def pTestCase = context.currentStep.testCase.name//get TestCase name
    def filePath = 'D:/soapUI-results/'+pName+'_'+sDate+'/'+pTestSuite+'/'+pTestCase+'/'//compose the folder path
    
    File file = new File(filePath)
    if (!file.exists()) file.mkdirs()//create the destination folder
    
    fos = new FileOutputStream(filePath+ testStepResult.testStep.label + '.txt', true)
    pw = new PrintWriter( fos )
    testStepResult.writeTo( pw )
    pw.close()
    fos.close()

    I hope this helps.

    • Sriraj's avatar
      Sriraj
      Contributor

      LexiAQA Your code works like a charm. I had to make a small change to add time in the folder name.   

    • Sriraj's avatar
      Sriraj
      Contributor

      Thanks LexiAQA . i will try it out , appreciate your quick help.

       

      nmrao I am attaching the as is results folder screen-print. Due to customer restrictions i had to mask few areas.

  • nmrao's avatar
    nmrao
    Champion Level 3

    Can you show the screen shot of TestRunListener.afterTestStep in the Events?

    • Sriraj's avatar
      Sriraj
      Contributor

      Hi nmrao ,

       

      Pardon my ignorance in the tool. Currently i have just copy pasted what was available in the link. And the code snippet is as below .

       

      filePath = 'D:/Sriraj/Results/'
      fos = new FileOutputStream( filePath + testStepResult.testStep.label + '.txt', true )
      pw = new PrintWriter( fos )
      testStepResult.writeTo( pw )
      pw.close()
      fos.close()
      • nmrao's avatar
        nmrao
        Champion Level 3

        Thank for the Screen shot. Can you please confirm by trying if the request and responses are saved? If so, how does the file name of req & response file names appear?