Forum Discussion

tamnor's avatar
tamnor
Occasional Contributor
8 years ago

How do I log raw request and response messages to a file every time test case or suite is run?

Hi 

 

I would like to know how I can create a file that write all raw request and response messages to this every time a test case or suite is run?

 

Is this an inbuilt feature of Ready API or do I need to write an event handler?

 

Thanks.

2 Replies

    • tamnor's avatar
      tamnor
      Occasional Contributor

      Thanks for that. I've also found that I can add a TestRunListener.afterStep event with the following code ...

       

      //creates subfolder name from testCase name and dateTime Stamp
      def subFolderName = testRunner.getTestCase().label.toString() + new Date().format("yyyy-MM-dd'T'HH.mm").toString()
      //creates variable to store parentFoldername
      def parentFolder = context.expand('${#Project#reqRespParentFolder}')

      //makes new directory out of parentFolder and subFolder name
      new File(parentFolder+subFolderName).mkdir()

      //creates variable for subFolder path
      def reqRespSubFolderPath = parentFolder+subFolderName+"/"

      //sets the subFolder path and subFolderName test case properties
      testRunner.getTestCase().setPropertyValue('reqRespSubFolderPath',reqRespSubFolderPath)
      testRunner.getTestCase().setPropertyValue('subFolderName',subFolderName)

      //creates new file in subFolder path with correct subFolderName
      def requestResponseInputFile = new File(reqRespSubFolderPath + subFolderName + ".txt")

      //set property value for requestResponseInputFile to write to
      testRunner.getTestCase().setPropertyValue('requestResponseInputFile',requestResponseInputFile.toString())
      //From here

      //creates variable to store requestREsponseInputFile
      def inputFileResponse = context.expand('${#TestCase#requestResponseInputFile}')

      //fos = new FileOutputStream( filePath + inputFileResponse.testStep.label + '.txt', true )
      //creates file output stream and printWriter variables
      fos = new FileOutputStream(inputFileResponse, true )
      pw = new PrintWriter( fos )

      //if step has a response property write result to the file.
      if(context.currentStep.hasProperty("Response"))
      {
      testStepResult.writeTo( pw )
      }
      pw.close()
      fos.close()