cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy assert using Datasource value always returns false when correct

SOLVED
Highlighted
Occasional Contributor

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
 
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Groovy assert using Datasource value always returns false when correct

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]


Regards,
Rao.

View solution in original post

3 REPLIES 3
Highlighted
Super Contributor

Re: Groovy assert using Datasource value always returns false when correct

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 + '"')

 

Highlighted
Community Hero

Re: Groovy assert using Datasource value always returns false when correct

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]


Regards,
Rao.

View solution in original post

Highlighted
Occasional Contributor

Re: Groovy assert using Datasource value always returns false when correct

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.

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors