2

I am running some API tests and I am using a groovy script to get out the test run report (SoapUI free version). My issue is that the reports comes in a .csv format, although I need it to be a .json format.

First the reports were generated as .csv, but now we must switch to json and I can`t figure out on what to change to the script to export as .json.

try {
// 1. Create a "SoapUIResults" folder in the project path 
      // Retrieve the project root folder
      def projectPath = new com.eviware.soapui.support.GroovyUtils(context).projectPath
      // Specify a folder inside project root to store the results
      String folderPath = projectPath + "/SoapUIResults";
      // Create a File object for the specified path
      def resultFolder = new File(folderPath);
      // Check for existence of folder and create a folder
      if(!resultFolder.exists())
      {
        resultFolder.mkdirs();
      }
/* ------------------------------------------------------------------------------- */
// 2. Create a subfolder (with timestamp) to store the request-response local copy 
      // Retrieve the latest execution date-time
      Date d = new Date();
      def executionDate = d.format("dd-MMM-yyyy HH_mm");
      // Specify the subfolder path with name Request-Response_CurrentTimeStamp
      String subfolderPath1 = folderPath+ "/Request-Response_"+executionDate;
      // Create this sub-folder
      new File(subfolderPath1).mkdirs();
/* ------------------------------------------------------------------------------- */
// 3. Create another subfolder "JSON Reports" to store the reports file 
      // Specify the subfolder path with name JSON Reports
      String subfolderPath2 = folderPath+ "/JSON Reports";
      // Create this sub-folder
      new File(subfolderPath2).mkdirs();
/* ------------------------------------------------------------------------------- */
// 4. Create a Report.json file inside the JSON Reports folder 
      // Create a File object for Report json file (with timestamp)
      def reportFile = new File(subfolderPath2, "Report_"+executionDate+".json");
      // Check for existence of report file and create a file
      if(!reportFile.exists())
      {
        reportFile.createNewFile();
        // Create required column names in the report file
        reportFile.write('"Test Suite","Test Case","Test Step","Step Type","Step Status",'
                        +'"Result message","Execution Date"');
      }
/* ------------------------------------------------------------------------------- */
// 5. Inserting data in the file
      // Iterate over all the test steps results
  for(stepResult in testRunner.getResults())
  {
    // Retrieve Test Suite name
   def testSuite = testRunner.testCase.testSuite.name;
   // Retrieve Test Case name
   def testCase = testRunner.testCase.name;
   // Retrieve Test Step
   def testStep = stepResult.getTestStep();
   // Retrieve Test Step name
   def testStepName = testStep.name
   // Retrieve Test Step type
   def type = testStep.config.type
   // Retrieve Test Step status
   def status = stepResult.getStatus()
   // Creating new line in report file
   reportFile.append('\n');
   // Write all the necessary information in the file
   reportFile.append('"' + testSuite + '",');
   reportFile.append('"' + testCase + '",');
   reportFile.append('"' + testStepName + '",');
   reportFile.append('"' + type + '",');
   reportFile.append('"' + status + '",');
   // Retrieve the test result messages
   reportFile.append('"');
   for(resMessage in stepResult.getMessages())
   {
     // Write messages and separate multiple messages by new line
     reportFile.append('Message:' + resMessage + '\n');
   }
   reportFile.append('",');
   //Write executionDate in the file
   reportFile.append('"' + executionDate + '",');
/* ------------------------------------------------------------------------------- */
// 6. Extract the request and response and save it to external file
      // Verify if the test step type is request: SOAP project or restrequest: REST project
        if((type=="request").or(type=="restrequest"))
        {
          // Extract the request from the test step
          def extRequest = testStep.properties["Request"].value;    
      // Create a file in the reports folder and write the request
      // Naming convention: request_TestSuiteName_TestCaseName_TestStepName.txt
      def requestFile=subfolderPath1+"/request_"+testSuite+"_"+testCase+"_"+testStepName+".txt";
      def rqfile = new File(requestFile);
      rqfile.write(extRequest, "UTF-8");
      // Extract the response from the test step
      def extResponse = stepResult.getResponseContent();    
      // Create a file in the reports folder and write the response
      // Naming convention: response_TestSuiteName_TestCaseName_TestStepName.txt
      def responseFile=subfolderPath1+"/response_"+testSuite+"_"+testCase+"_"+testStepName+".txt";
      def rsfile = new File(responseFile);
      rsfile.write(extResponse, "UTF-8");
     }
   }
 }
catch(exc)
{
   log.error("Exception happened: " + exc.toString());
} 

I would expect the output to be a json file, but it is a .csv one.

Edit: TearDown script follows:

// Code to execute the GenerateJSONReport test step
testRunner.testCase.testSuite.project.testSuites["Library"].testCases["Reporting_Utility"].
testSteps["GenerateJSONReport"].run(testRunner, context);
nirolfum
  • 21
  • 5
  • Connected: https://stackoverflow.com/questions/33895330/export-result-in-text-file-by-soapui. – Mate Mrše Apr 12 '19 at 10:56
  • Try something like this in your Project TearDown script: https://stackoverflow.com/a/45407960/3124333 – SiKing Apr 12 '19 at 18:10
  • Hi, I have tried that in the TearDown script and also tried to embed it in the script, but with no luck. The report is still generated as .csv. I will keep digging and post a solution if I`ll find something working. – nirolfum Apr 17 '19 at 07:26

0 Answers0