Ask a Question

Iterating through Data source values and verifying the matched value

SOLVED
mu123
Occasional Contributor

Iterating through Data source values and verifying the matched value

This is the scenario that I am trying to test: 

1. I have a REST API step that returns number of ids and names for locations. (Sometimes zero records are returned, sometimes one and sometimes multiple records are returned)

2. I have to write a test to iterate through the REST Response and verify the location name and get the corresponding id.

------------------------------------

I tried to test this by

1. Creating a Data source from the Json response of the REST API step. 11 rows of id and name were returned.

2. I am trying to write a groovy script to iterate through all the rows of Data source but couldn't do it.

  I can only get the last row in the groovy script. Please let me know how to test this scenario.

10 REPLIES 10
richie
Community Hero

Hey @mu123,

Can you post the groovy script you've done so far please?

Also people are going go need the payload content for jsonpath to help wirh your script.

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
ChrisAdams
Community Leader

Hi,

 

I'm assuming you have the licensed version of ReadyAPI if you're using a Datasource step.  If you are, do you have a data source loop with the groovy script in between?

 

Or, if unlicensed, we'd need to see what you've put together this far as @richie suggests.

mu123
Occasional Contributor

I have attached the Json response and my tests

ChrisAdams
Community Leader

Hi,

 

You're over-thinking this.  The solution is much easier and you're not far off.

 

Data-driven tests that use a Datasource step need a Datasource Loop step.  This handles the iteration of the data set.  Steps in between the Datasource and Datasource Loop are called for each row in the data source.

 

Using your screen shot as a guide, here's what I think you should be moving towards....

 

ChrisAdams_0-1656684087565.png

 

Note the Data Source Loop step and how Ready API indicates the loop with the arrow.

 

When you add the Data Source Loop step, you have to configure it.  Here's how I did mine for your example...

ChrisAdams_1-1656684194963.png

The first value is the link to the data source of interest.  The second value is the first step inside the loop that you want to call.

 

Before moving onto the Groovy step, it's worth looking at the Datasource.  Again, I borrow from your example...

ChrisAdams_2-1656684326284.png

 

These properties are accessible within the loop and contains the values for the current row in the datasource.  Without the loop step, you always get the last row.

 

Ok, Groovy script and how to access the current values....

Here is the content of the "Let's log stuff - Groovy script' step.

 

ChrisAdams_3-1656684579792.png

 

You can type all this in, but now is a good opportunity to mention "Get Data".  In your Groovy Step, if you right-click, there is an option called Get Data.  If you click this, you can navigate to the Datasource step and select the value of interest without typing.

 

You'll notice I added an additional step call "REST Request to call in each iteration".  You can apply the same Get Data idea.  If you want to call another service for each id in your datasource, then instead of calling with a hard-coded value, again right click where the param needs to use get data.  E.g.

 

ChrisAdams_4-1656684873773.png

 

 

 

 

 

mu123
Occasional Contributor

Thank you all for the excellent suggestions. I am sorry still I seem to be missing some piece. I have added the Data Source loop, still not able to get the required value.

mu123
Occasional Contributor

I tried modifying the groovy script. But every time it references only the last row of the data source. Any help would be appreciated.

ChrisAdams
Community Leader

Can you share the Groovy script?

Hey @mu123 

 

As @ChrisAdams said - can you please publish the groovy script?   I know you did attach a word doc which included the groovy script - but it doesn't render correctly and I can't read the groovy properly.

 

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
mu123
Occasional Contributor

I changed the start row to null, end row to null, and Rows per iteration to null. Now the script works.

Thank you all for the suggestions.

cancel
Showing results for 
Search instead for 
Did you mean: