Forum Discussion

endorium's avatar
endorium
Frequent Contributor
6 years ago
Solved

Verify multiple rows and then data drive

First off I am new to Soup/ReadyAPI. 

I have an API that returns row information for a grid.

I have assertions for the count of rows and the first row. I am also verifying the data against an excel sheet which is all working fine.

 

The problems comes when I add more than one row of data in the excel sheet as i want to validate multiple rows. I cannot get this to work. Ideally I want to validate 3 rows on each run looking at the same excel sheet.

Any ideas how I do this? Eventually I want to datadrive this as well.

 

Screenshots of assertions and data returned plus excel sheet.

 

  • Ok, first things first. Please notice that your excel starts with the Id = 0 while the response starts with Id = 1.

     

    Nevertheless my script was wrong. Here you have the same script with some corrections and logs added to it:

     

    import groovy.json.JsonSlurper
    
    // Get the id from the datasource
    def id = context.expand('${DataSource#ID}') as Integer
    log.info "The id collected from the datasource is '" + id + "'." 
    
    // Get the response from the GetResponse step
    def response = context.expand('${GetResponse#Response}')
    
    // Parse the response with JsonSlurper
    def jsonSlurper = new JsonSlurper()
    def jsonObject = jsonSlurper.parseText( response )
    
    // Get the element where the ID is the same as in the datasource
    log.info "Trying to find the element with id " + id + " in response..."
    for (item in jsonObject.value) {
    	if (item.Id == id) {
    		log.info "Element found. Comparing the datasource data with the response data..."
    		
    		// Start comparison
    		log.info "The TestID value collected from the datasource is '" + context.expand('${DataSource#TestId}') + "' while the response value is " + item.TestID + "'."
    		assert item.TestID.equals( context.expand('${DataSource#TestId}'))
    
    		log.info "The TestName value collected from the datasource is '" + context.expand('${DataSource#TestName}') + "' while the response value is '" + item.TestName + "'."
    		assert item.TestName.equals( context.expand('${DataSource#TestName}'))
    
    		log.info "The Assays value collected from the datasource is '" + context.expand('${DataSource#Assay}') + "' while the response value is '" + item.Assays + "'."
    		assert item.Assays.equals( context.expand('${DataSource#Assay}'))
    
    		log.info "The Analyses value collected from the datasource is '" + context.expand('${DataSource#Analyses}') + "' while the response value is '" + item.Analyses + "'."
    		assert item.Analyses.equals( context.expand('${DataSource#Analyses}'))
    
    		log.info "The Cassettes value from the datasource is '" + context.expand('${DataSource#Cassettes}') + "' while the response value is '" + item.Cassettes + "'."
    		assert item.Cassettes.equals( context.expand('${DataSource#Cassettes}'))
    
    		log.info "The Enabled value from the datasource is '" + context.expand('${DataSource#Enabled}') + "' while the response value is '" + item.Enabled + "'."
    		assert item.Enabled == context.expand('${DataSource#Enabled}').toBoolean()
    
    		log.info "The UserDefined value from the datasource is '" + context.expand('${DataSource#UserDefined}') + "' while the response value is '" + item.UserDefined + "'."
    		assert item.UserDefined == context.expand('${DataSource#UserDefined}').toBoolean()
    	}
    }

45 Replies

  • Lucian's avatar
    Lucian
    Community Hero

    Can you provide a sample excel file/ SoapUI project for us to help you? Are you using a datasource step to read the excel? You said you cannot get the thing to work. What does not work for you and what did you try so far?

    • endorium's avatar
      endorium
      Frequent Contributor

      Hi, can you see the screenshots of the excel file and logs along with the description above?

       

      Basically when I add two rows to validate form the excel column as shown in the screenshot it fails. I need it to verify, for instance, rows 1 and 2 of the grid and have this information stored in excel as detailed above in the screenshot.

      • Lucian's avatar
        Lucian
        Community Hero

        I can see the screenshots but I am wondering what are the steps you are taking for the comparison. Normally you would use the datasources to loop over each row...

  • endorium's avatar
    endorium
    Frequent Contributor

    I will try explain it better.

    So I have an API that returns 424 rows.

    I want to verify the data in rows, for example, 1, 200 and 424 match the data in an excel spreadsheet written like the one above.

     

    Even if its not data driven but does this on one pass. How can i do this? I reference the columns for the expected result as shown above but it fails as I assume for the second row in the sheet it is not looking at just the second row in the spreadsheet but all rows.

     

    Any ideas? I was hoping to avoid having to do manual Groovy scripts as much as possible to begin with till I learn and was hoping this was not that unusual a scenario to use for API tests?

    • Lucian's avatar
      Lucian
      Community Hero

      I'll try to find a solution for you tonight (at work now)

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        I am going to ask some questions, so bear with me. You mention you need to validate, as an example, rows 1, 200, and 424. Will those ALWAYS be the rows required to be validated? If not, then what designates those as the rows you need to validate? 

  • sanj's avatar
    sanj
    Super Contributor

    its possible for sure

    Have a groovy script that actually reads the particular row and assigns its to test case variables

    Then make an assertion for the appropriate row

    I have done something similar where each row is identified using a unique key

    That how I retrieve and assign the values for that row to the test case variable.

     

     

  • endorium's avatar
    endorium
    Frequent Contributor

    I have spent a good amount of time looking at this yesterday and today but I cannot get the code to work.

    I dont think there is just one issue.

     

    I think the issue is the loop at the bottom. 'i' is being set at 48 on the first run.

     

    Screenshot of me running it. On the first loop through it is setting 'i' to 48. I prove this by logging the testID response and this is the test ID of my 48th row.

     

    Any ideas would be greatly appreciated.

     

     

     

     

    • Lucian's avatar
      Lucian
      Community Hero

      Hi,

       

      Can you place again the materials with which you are working? I will try to help you further.

       

      • endorium's avatar
        endorium
        Frequent Contributor

        'i' always seems to start at 48. Below is code and also attached the spreadsheet and the jsonObject log.

        If you need anything else please let me know

         

         

        Code:

        import groovy.json.JsonSlurper

        // Get the id from the datasource
        def id = context.expand( '${DataSource#ID}' )


        // Get the response from the GetResponse step
        def response = context.expand( '${TestRequest#Response}' )


        // Parse the response with JsonSlurper
        def jsonSlurper = new JsonSlurper()
        def jsonObject = jsonSlurper.parseText(response)





        // Get the element where the ID is the same as in the datasource
        for (int i = 0; i < jsonObject.value.size(); i++) {
            if (jsonObject.value[i].Id ==id) {
                // Start comparison

                assert jsonObject.value[i].TestID.equals (context.expand( '${DataSource#TestID}' ))
                assert jsonObject.value[i].TestName.equals( context.expand('${DataSource#TestName}'))
                assert jsonObject.value[i].Assays.equals( context.expand('${DataSource#Assay}'))
                assert jsonObject.value[i].Analyses.equals( context.expand('${DataSource#Analyses}'))
                assert jsonObject.value[i].Cassettes.equals( context.expand('${DataSource#Cassettes}'))
                assert jsonObject.value[i].Enabled == context.expand('${DataSource#Enabled}').toBoolean()
                assert jsonObject.value[i].UserDefined == context.expand('${DataSource#UserDefined}').toBoolean()
            }
        }