cancel
Showing results for 
Search instead for 
Did you mean: 

How to verify response content

SOLVED
Loli
Occasional Contributor

How to verify response content

Hello,I have to verify the response content dynamically using groovy but I really don't know how to do it.I am currently using the script assertion to verify the contents.Can anyone help me out? Using the Rest

Loli_0-1619810064368.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
richie
Community Hero

Re: How to verify response content

Regariding the groovy list of values.

You could insert that list into a single property and then split each value from the list into individual values using the split() metho maybe? This would include using a list but again make things tidier if just using properties. Just a thought. Going yo bed now

Ta

Rich.
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

View solution in original post

19 REPLIES 19
richie
Community Hero

Re: How to verify response content

Hey @Loli,

For each of the responses can you confirm what you actually need to verify?

I.e. are you trying to assert a specific attribute has a specific value or something else? Some of the attributes in the responses have nulls do you want to assert that these fields display nulls? Do you want to verify the structure of responses? Do you want to verify the datatypes (string? Numeric? Boolean? Null?) of the attributes in the responses? The content in the responses....do any of the attribute values in the response relate directly to the parm values submitted in the request? (This would allow you to avoid hardcoding the response values and base your assertion results on the request inputs)

Anyone that tries helping you with this is gonna need at least this minimal information before they can sort you out.

The more info you provide the more effectively people will be able to help you,

Cheers,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Loli
Occasional Contributor

Re: How to verify response content

Yes sorry for such a bland explonation.For an example when i put the first request in this response comes in.I need to validate the response values with the assertion that this came in.I tried using this method: 

def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText('{"code": "1200","message": "UserName and Password required."}')

assert result.code == "1200"
assert result.message == "UserName and Password required."

 

But i do think that it is not dynamic so i want to change it.Thanks for the replies&help

Loli_0-1619817806942.png

 

richie
Community Hero

Re: How to verify response content

Hey @Loli,

Where did you get the code from?

Reason i ask is that youre declaring a 'result' variable that youre not actually using.

Also, would you mind attaching the screenshot rather than embedding it please? I cant actually see the content properly with embedded screenshots. I can only see attached screenshots instead.


Also, your code has multiple assertions. If youre including this code in a single script assertion or groovystep this means if one assertion fails the whole teststep will fail. Its adviseable to split out your assertions into individual teststeps UNLESS youre sure that if one assertion within a teststep passes then they all will pass.

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Loli
Occasional Contributor

Re: How to verify response content

Yes i wrote the code myself,It was just a first thing that came to my mind and it needs to be changed.Although I have got no clue how really.This photo might clear some things up.Capture.PNG

 

richie
Community Hero

Re: How to verify response content

Hey @Loli,

Can you please indicate what you should be verifying in the response please?

Do you need to pass on the token value to other, subsequent calls in your test or are you just trying to assert against this particular response?

This is for the "homework", right? In your previous homework message you also mentioned asserting the http status headers, right? Is there anything else you want to assert against?

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Loli
Occasional Contributor

Re: How to verify response content

As far as i understood,I should be verifying response tokens.Capture.PNG

 

 

Here is the code I wrote and it works:

def headers = messageExchange.getResponseHeaders()
log.info headers["#status#"]
assert ['HTTP/1.1 200 OK'] == headers["#status#"]
def actualStatus = headers["#status#"].get(0)
def excelExpectedValue = '200 OK'
def expectedStatus = 'HTTP/1.1 '+excelExpectedValue
assert expectedStatus == actualStatus

 

def responseContent = messageExchange.response.responseContent
def jsonSlurper = new groovy.json.JsonSlurper().parseText(responseContent)
def actual=jsonSlurper.status
def actuelResult = jsonSlurper.result
assert actual == "Success"
assert actuelResult == "User authorized successfully."

 

First one confirmes that the HTTP code is correct.and the second one confirmes that the message I got from the request mathes the response.Although I did 3 test cases for 3 different scenarios.My teacher asked me to loop it and make it shorter.No clue on how to do that though.Any help would be appreciated 

richie
Community Hero

Re: How to verify response content

Hey @Loli,

You wont be able to assert the token value is correct cos theres no way of defining it before you actually execute the thing (well....other than the first 2 chars anyway).

Im typing this out on my phone so i'll sort out the looping tomorrow when im infront of my laptop, but can you confirm what loop you want? Are you asking for a single testcase that iterates through the execution of multiple testdata files within the same test case?

messageExchange is not available in Groovy script steps, just script assertions. Im assuming its ok if you use script assertions instead of groovy steps....can you confirm?

In regards to your homework. Are you able to use the SoapUI GUI functionality or have you gotta just use groovy?

Cheers

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Loli
Occasional Contributor

Re: How to verify response content

Yes that's the for loop I am looking for.I need to shorten the 3 test cases to 1 by using the loop as you have said.Yes I also can use normal script assertions so it should be fine.as for the Gui I am pretty sure I can use it as well.Thanks for the replies&help
richie
Community Hero

Re: How to verify response content

Hey @Loli,

You probably need to get confirmation on whether you can use the GUI for your assertions.

Reason im saying this is that you can actually do all your requirer assertions via the GUI (Jsonpath Match criteria assertion and valid http status code assertions).

The only bit youd need groovy script for in the scenario youre discussing is the loop (unless youre using the licensed version of soapui (i.e. soapui pro/readyapi.....the pro version includes data driven functionality that means you wouldnt have to bother using groovy script at all)

Cheers

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
New Here?
Join us and watch the welcome video:
Top Kudoed Authors