Forum Discussion

roja1's avatar
roja1
Contributor
5 years ago
Solved

Unable to read response of one rest test step

Hi,

 

I have 4 test steps in my test case.  My groovy script is able to read and compare response of all the test steps except the last test step. So I have tried removing all the steps and kept only one step.I'm not sure why my script is not able to read the last step. In my excel file I have 5 rows in my excel. In excel I have test step name in the col A and expected response in the col D.Basically I'm reading expected response from excel and comparing the response from soapUI.Below is the code I have used.

Appreciate your help in advance

 

def k=1
(0..context.testCase.testStepCount-1).each{
	def step1 = context.testCase.testStepList[it]
	if ( step1 instanceof RestTestRequestStep) {
		for(j=1;j<no_of_rows;j++){
			Cell f=inputSheet.getCell(0,j)
 		     step_name=f.getContents()
 		     Cell f1=inputSheet.getCell(3,j)
 		     def String step_response=f1.getContents()
 		     def String response = context.expand(step1.getPropertyValue('Response')) 
 		     if(response!=null)
 		     {
 		     response=response.replaceAll(" ","")
 		     }
 		     if(step_response!=null)
 		     {
 		     step_response=step_response.replaceAll(" ","") 
 		     }
 		     if((step1.name==step_name)&&(step_response!='')){
 		     	try{	     
 		     		JSONAssert.assertEquals(step_response,response,false)
 		     		log.info k
 		     		log.info step_name
 		     		log.info "from excel"+step_response
 		     		log.info "from UI"+response
 		     		jxl.write.Label status =  new jxl.write.Label(1,k ,"Passed")
 		     	     sheet_writable.addCell(status)
 		     	     pass_count=pass_count+1
 		     	}catch(AssertionError  e){
 		     		log.info "${e}"
 		     		String exception = e.toString()
 		     		exception=exception.replace("java.lang.AssertionError:","")
 		     		jxl.write.Label status =  new jxl.write.Label(1,k ,"Failed")
 		     	     sheet_writable.addCell(status) 
 		     	     jxl.write.Label reason =  new jxl.write.Label(5,k ,exception)
 		     	     sheet_writable.addCell(reason)
 		     	     fail_count=fail_count+1
 		     	}
 		     	k=k+1
 		     }
 		     else 
 		     if((step1.name==step_name)&&(response==null))
 		     {
 		     	 if(response==null)
				{
 		     	log.info "from else "+k
 		     	log.info "response from if"+response
 		     	jxl.write.Label status =  new jxl.write.Label(1,k ,"Passed")
 		     	sheet_writable.addCell(status)
 		     	k=k+1
 		     	pass_count=pass_count+1
				}
				else
				{
				  jxl.write.Label status =  new jxl.write.Label(1,k ,"Failed")
 		     	  sheet_writable.addCell(status)
 		     	   k=k+1
 		     	   fail_count=fail_count+1
				}
 		     }
 		     
		}
	}
}

 

23 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    What is the type of each test step in the test case such as SOAP, REST, JDBC etc?

    And where is the above script present?

    NOTE: request you to format the code for better readability.

    • roja1's avatar
      roja1
      Contributor

      Hi ,

       

      It was rest request and its present in soapUI.

       

      Thank you,

      roja

      • nmrao's avatar
        nmrao
        Champion Level 3
        Are all 4 steps of rest type? What about groovy script step? A screen shot of test case (with steps) possible?
    • roja1's avatar
      roja1
      Contributor

      Hi nmrao ,

       

      I have edited my post. I have interpreted wrongly so I have edited  my post.

       

      Thank you,

      Roja 

  • nmrao's avatar
    nmrao
    Champion Level 3

    Please see if the below helps.

     

    Note that, don't know exactly in which cell you want to put the data n could not test. Give it a try.

    context.testCase.testStepList.findAll { it instanceof RestTestRequestStep}.each { step ->	
    	def k = context.testCase.getTestStepIndexByName(step.name) + 1	
    	for (j = 1; j < no_of_rows; j++) {
    		Cell f = inputSheet.getCell(0, j)
    		step_name = f.getContents()
    		Cell f1 = inputSheet.getCell(3, j)
    		def step_response = f1.getContents()
    		def response = context.expand(step.getPropertyValue('Response'))
    		if (response != null) {
    			response = response.replaceAll(" ", "")
    		}
    		if (step_response != null) {
    			step_response = step_response.replaceAll(" ", "")
    		}
    		if ((step.name == step_name) && (step_response != '')) {
    			try {
    				JSONAssert.assertEquals(step_response, response, false)
    				log.info k
    				log.info step_name
    				log.info "from excel" + step_response
    				log.info "from UI" + response
    				jxl.write.Label status = new jxl.write.Label(1, k, "Passed")
    				sheet_writable.addCell(status)
    				pass_count = pass_count + 1
    			} catch (AssertionError e) {
    				log.info "${e}"
    				String exception = e.toString()
    				exception = exception.replace("java.lang.AssertionError:", "")
    				jxl.write.Label status = new jxl.write.Label(1, k, "Failed")
    				sheet_writable.addCell(status)
    				jxl.write.Label reason = new jxl.write.Label(5, k, exception)
    				sheet_writable.addCell(reason)
    				fail_count = fail_count + 1
    			}
    		} else if ((step.name == step_name) && (response == null)) {
    			if (response == null) {
    				log.info "from else " + k
    				log.info "response from if" + response
    				jxl.write.Label status = new jxl.write.Label(1, k, "Passed")
    				sheet_writable.addCell(status)
    				pass_count = pass_count + 1
    			} else {
    				jxl.write.Label status = new jxl.write.Label(1, k, "Failed")
    				sheet_writable.addCell(status)
    				fail_count = fail_count + 1
    			}
    		}
    	}
    }

    Removed incremens of k as not needed.

     

    • nmrao's avatar
      nmrao
      Champion Level 3

      roja1 

      Can't try your code.

       

      ONly suggest, if the response is null or empty instead of just checkinfor null.

      • roja1's avatar
        roja1
        Contributor

        Hi nmrao ,

         

        I didn't understand your point. Can you please eloborate.

         

        Thank you