Forum Discussion

mu123's avatar
mu123
Occasional Contributor
3 years ago

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.

  • 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....

     

     

    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...

    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...

     

    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.

     

     

    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.

     

     

     

     

     

     

  • richie's avatar
    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
  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    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.

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    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....

     

     

    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...

    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...

     

    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.

     

     

    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.

     

     

     

     

     

     

    • mu123's avatar
      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's avatar
    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.

    • richie's avatar
      richie
      Community Hero

      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

  • mu123's avatar
    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.

    • fgimenes's avatar
      fgimenes
      Occasional Contributor

      Hi mu123 , would you mind sharing your final script? I am trying a similar implementation and I haven't figured out how to advance the data source to the next line. I am not able to make the data source to go to the next line. It always prints the same value. I am not if it's because my Data Source is in another Test Suite??. This is my code:

       

      def tStep = testRunner.testCase.testSuite.project.testSuites["Innitialization"].testCases["Test Data Setup"].getTestStepByName("Data Source for Registration")
      def data = tStep.getDataSource()
      
      //run the datasource test step
      data.prepare(testRunner,context,data.getPreparedProperties() )
      tStep.run(testRunner,context)
      
      //loop through the datasource to find number of rows
      def cnt = 1
      while(data.isExhausted() == false) {
      	data.next(testRunner,context,data.getPreparedProperties() )
      	def MIP = context.expand( '${#[Innitialization#Test Data Setup#Data Source for Registration]#MIP}' )
      	def IS_CMA_ACCOUNT_CREATED = context.expand( '${#[Innitialization#Test Data Setup#Data Source for Registration]#IS_CMA_ACCOUNT_CREATED}' )
      	def mobileNumber = context.expand( '${#[Innitialization#Test Data Setup#Data Source for Registration]#mobileNumber}' )
      	
      	log.info("MIP: " + MIP)
      	log.info("IS_CMA_ACCOUNT_CREATED: " + IS_CMA_ACCOUNT_CREATED)
      	log.info("mobileNumber: " + mobileNumber)
      	cnt++
      }
      log.info("Number of rows in the datasource is: " + cnt)