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...
  • Lucian's avatar
    Lucian
    6 years ago

    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()
    	}
    }