Forum Discussion

magi-i-luften's avatar
magi-i-luften
Occasional Contributor
7 years ago
Solved

How to get actual value for assertion in script - okay to get expected value with getExpectedContent

Hi forum

 

I'm facing a problem and would like to figure out if my approach is wrong.

 

Functionality:

In ReadyAPI I have a groovy-teststep to control all of my REST webservice-calls.

My webservice has several assertions that is added manually to the REST teststep.

 

I'd like to execute the REST teststep from within the groovy-script, and when the request has been executed, I'd like to check the response. Was the call failing? If so, check which assertion went wrong, and report the error with expected value and actual value for current assertion.

 

What do I have to write instead of  "??????????" to get the actual value from the response that my assertion is comparing with? And I would like to keep in it a dynamically way - so future assertions will be handled as well. 

 

Example:

def thisTestcase = testRunner.testCase.testSuite.testCases["myTestCase"];

 

    //Call the "REST myService" - script
    def tStepCallService = thisTestcase.testSteps["REST myService"];
    def resultStepCallService = tStepCallService.run(testRunner, context);    

 

    if (resultStepCallService.getStatus() != AssertionStatus.VALID){     
      def list = tStepCallService.getAssertionList();
      for(assertion in list)
      {
        if (assertion.status != AssertionStatus.VALID){
         def iExpected = assertion.getExpectedContent();
         def assertionExpectedValue = context.expand(iExpected);
         def assertionActualValue = ??????????;
         log.error("Assertion-error: '" + assertion.name + "', Status: '" + assertion.status + "'. Expected value: '" + assertionExpectedValue + "'");
        }
      }
     }

 

 

Best regards

Jens

  • I'm not sure if you can get the actual value from the test step assertion, to the best of my knowledge this is just the definition of the assertion.

     

    What you want comes from the test step result, have you investigated what the result messages return to see if they provide what you need, something like:

     

    resultStepCallService.getMessages().each(){ message ->
    	log.error('Error message: ' + message) 
    }

     

2 Replies

  • Radford's avatar
    Radford
    Super Contributor

    I'm not sure if you can get the actual value from the test step assertion, to the best of my knowledge this is just the definition of the assertion.

     

    What you want comes from the test step result, have you investigated what the result messages return to see if they provide what you need, something like:

     

    resultStepCallService.getMessages().each(){ message ->
    	log.error('Error message: ' + message) 
    }

     

    • magi-i-luften's avatar
      magi-i-luften
      Occasional Contributor

      Hi Radford

       

      Thank you - this is just a fine solution.

      Now I'll just skip my own "expected" versus "actual" value, and just go for the full message.

       

      Best regards

      Jens