Forum Discussion

davecoleman's avatar
davecoleman
Contributor
5 years ago

Groovy assert using Datasource value always returns false when correct

Hi,

we have the following script pulling data from our DataSource excel to validate the response of the Status in the Response.

The actual value is passed out (seen in the Datasink) and the input value is also correct. as per the attached image both values look correct but fails the assertion.

What am I doing wrong?

 
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
//def row = messageExchange.modelItem.testSteps["DataSource"].currentRow
def expectedHTTPResponse = context.expand('${DataSource#code}' )

def ActualHTTPResponse = messageExchange.modelItem.testStep.testCase.testSteps["StepName"].testRequest.response.responseHeaders["#status#"]
//#status# is the name of the header you want to extract
//Read this value into a parameter - writes the header value into the Properties test step
groovyUtils.setPropertyValue("StatusCodeParse", "status",ActualHTTPResponse[0])
//compare the 2 values and if not the same throws an error
assert expectedHTTPResponse == ActualHTTPResponse
 
  • The data isource might be having value as "[HTTP/1.1 200 OK]" which includes the square brackets. is it?

    Then remove them ("[", "]") in the data source.

    Also, use below statement to assert

    assert trim(expectedHTTPResponse) == messageExchange.response.responseHeaders['#status#'][0]

    Or

    //define in source value as 200; say as below this makes life more easier
    def expectedHTTPStatusCode = '200'
    assert expectedHTTPStatusCode == messageExchange.response.responseHeaders['#status#'][0].split(' ')[1]
  • Radford's avatar
    Radford
    Super Contributor

    Are you sure that the two values you are comparing in the assert are exactly the same? For example does one of the values have a trailing space?

     

    I'd log the two values as follows, just before the assert statement to check:

     

    log.info('expectedHTTPResponse = "' + expectedHTTPResponse + '"')
    log.info('ActualHTTPResponse = "' + ActualHTTPResponse + '"')

     

  • nmrao's avatar
    nmrao
    Champion Level 3

    The data isource might be having value as "[HTTP/1.1 200 OK]" which includes the square brackets. is it?

    Then remove them ("[", "]") in the data source.

    Also, use below statement to assert

    assert trim(expectedHTTPResponse) == messageExchange.response.responseHeaders['#status#'][0]

    Or

    //define in source value as 200; say as below this makes life more easier
    def expectedHTTPStatusCode = '200'
    assert expectedHTTPStatusCode == messageExchange.response.responseHeaders['#status#'][0].split(' ')[1]
    • davecoleman's avatar
      davecoleman
      Contributor

      Many thanks for the pointers here. I've used both of your responses to do the comparison and have trimmed the [] from the response and input which has fixed the assertion

       

      def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
      //use CONTEXT package to parse in the EXCEL sheet records
      def expectedHTTPResponse = context.expand('${DataSource#code}' )
      def customer=context.expand('${DataSource#customer}' )
      //output the value of ACTUAL response with the related PPSN
      log.info('expectedHTTPResponse =  ' + expectedHTTPResponse + '-' + customer + '')
      
      def ActualHTTPResponse = messageExchange.modelItem.testStep.testCase.testSteps["StepName"].testRequest.response.responseHeaders["#status#"][0]
      //#status# is the name of the header you want to extract
      //Read this value into a parameter - writes the header value into the Properties test step
      
      groovyUtils.setPropertyValue("StatusCodeParse", "status",ActualHTTPResponse)
      // parse in the file status details to the script (need to check the syntax here)
      ///pull back the response details
      //output the value of ACTUAL response with the related PPSN
      log.info('ActualHTTPResponse = ' + ActualHTTPResponse + '-' + customer+ '')
      //compare the 2 values and if not the same throws an error
      assert expectedHTTPResponse == ActualHTTPResponse

      Again, thanks all. I haven't parsed the 200 / 404 out of the response as I couldn't get working but solution is good for now.