Forum Discussion

chmelvv's avatar
chmelvv
Occasional Contributor
8 years ago

How to modify testStepResult?

Hi,

I use log processing script (as TestSuite tear down script) used for a lot of our projects.

It gets information (like request URL) from testStepResult. But I need to add more information unique for every test step run into this object testStepResult. But it seems has only get... methods.

Is it possible to add custom fields and values into testStepResult object?

  • chmelvv's avatar
    chmelvv
    8 years ago

    Hi Tanya,

    thanks for quick reply!

     

    I chose following solution: I used event listener TestRunListener.afterStep and inside it after each step append needed information into TestSuite property, then in test suite TearDown script wrote it to needed file.

     

    1) TestSuite`s Setup Script:

    To clean data from previous run.

    testSuite.getTestCaseByName("testCaseName").setPropertyValue("logFile", "")

    2) Event Listener

    Name: TestRunListener.afterStep

    Event: TestRunListener.afterStep

    Target: Rest request (inside testCaseName test case)

    if ( "FAILED".equals( testStepResult.getStatus().toString() ) ){
       def URL = testStepResult.getMessageExchanges()[0].getProperty("URL")
    	
       def parameter1 = context.expand( '${DataSource#<SOME_PARAMETER1>}' )
       def parameter2 = context.expand( '${DataSource#<SOME_PARAMETER2>}' )
    
    def logFile = testRunner.getTestCase().getProperty("logFile").getValue() + URL 
    + " " + parameter1 + " " + parameter2 + "\n"
    	testRunner.getTestCase().setPropertyValue("logFile", logFile)
    }

     

    3) TestSuite`s TearDown Script:

    File reportFile = new File("report.txt")
    	
    def logFile = testSuite.getTestCaseByName("testCaseName").getProperty("logFile").getValue()
    reportFile.write(logFile , "utf-8")

     

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Chmelvv,

     

     

    As far as I know, it's impossible to add data to testStepResults. You can only read information from it.

     

    I suggest that you write the data you need to an external file during the test execution. At the end of the test, your parser can read information from the file.

    • chmelvv's avatar
      chmelvv
      Occasional Contributor

      Hi Tanya,

      thanks for quick reply!

       

      I chose following solution: I used event listener TestRunListener.afterStep and inside it after each step append needed information into TestSuite property, then in test suite TearDown script wrote it to needed file.

       

      1) TestSuite`s Setup Script:

      To clean data from previous run.

      testSuite.getTestCaseByName("testCaseName").setPropertyValue("logFile", "")

      2) Event Listener

      Name: TestRunListener.afterStep

      Event: TestRunListener.afterStep

      Target: Rest request (inside testCaseName test case)

      if ( "FAILED".equals( testStepResult.getStatus().toString() ) ){
         def URL = testStepResult.getMessageExchanges()[0].getProperty("URL")
      	
         def parameter1 = context.expand( '${DataSource#<SOME_PARAMETER1>}' )
         def parameter2 = context.expand( '${DataSource#<SOME_PARAMETER2>}' )
      
      def logFile = testRunner.getTestCase().getProperty("logFile").getValue() + URL 
      + " " + parameter1 + " " + parameter2 + "\n"
      	testRunner.getTestCase().setPropertyValue("logFile", logFile)
      }

       

      3) TestSuite`s TearDown Script:

      File reportFile = new File("report.txt")
      	
      def logFile = testSuite.getTestCaseByName("testCaseName").getProperty("logFile").getValue()
      reportFile.write(logFile , "utf-8")