Forum Discussion

Tamarael's avatar
Tamarael
New Contributor
9 years ago

Nested cycle (Loops)

Hi there,

 

I am starting my SoapUI experience and would like to know if you guys could help me with a problem I encountered:

I got a test that my Boss want automated via SoapUI and what it is supposed to do is run some API with some parameters fed via an excel file. These API have some optional parameters that we should validate and therefore I am in need of help for the following scenario:

 

  • The excel file must be read and will most likely have two or three worksheets;
  • On the first worksheet there are some MAC Addresses and on the following worksheets there will be the other optional parameters that we should validate.

 

For this test, lets say my method is "alerts" and "alerts" has only one optional parameter and it is "X". For "X" it would be aceptable as a parameter value to inputs: "A" and "B".

 

So I need to read the file, get the lenght of the second worksheet (in this case it is 2 - A and B)and run the first line against all the Mac Addresses of the first worksheet, after all the lines have being ran, it should then go to the next line of the second worksheet and run all the Mac Addresses of the first worksheet and so on untill all the lines of the second worksheet have being processed.

 

I know it is a bit complex but that is why I am having some issues with it. I really appreciate all the help you guys can offer.

    • Tamarael's avatar
      Tamarael
      New Contributor

      kondasamy wrote:

      This should be possible through Groovy Script! I hope the below links would help to solve your scenario,

       

      Thanks,

      Samy

       

      Did my reply answer your question? Give Kudos or Accept it as a Solution to help others, Thanks. ↓↓↓


      Samy, thanks for the reply but what the links contain have already been implemented on my code, I can get one cicle working but I want to automate the whole process and I believe that to achieve it I must get a nested cycle.

       

      I might be wrong but given the little knowledge I have, I believe it just might be it. I got some groovy scripts working on that end. My test case is like this:

       

      1. Groovy script

      2. Properties

      3. Soap request

      4. Loop

       

      On the groovy script I got:

       

      import com.eviware.soapui.support.XmlHolder
      import jxl.*
      import jxl.write.*
      
      def myTestCase = context.testCase
      def counter,next,previous,size
      Workbook workbook1 = Workbook.getWorkbook(new File("C:\\Users\\User\\Documents\\IN\\PH\\NI\\macs.xls"))
      Sheet sheet1 = workbook1.getSheet(0)
      size=sheet1.getRows().toInteger()
      propTestStep = myTestCase.getTestStepByName("Properties")
      propTestStep.setPropertyValue("Total",size.toString())
      counter = propTestStep.getPropertyValue("Count").toString()
      counter = counter.toInteger()
      next = (counter>size-2?0:counter+1)
      
      Cell u = sheet1.getCell(0,counter)
      workbook1.close()
      MAC = u.getContents()
      
      propTestStep.setPropertyValue("MAC",MAC)
      propTestStep.setPropertyValue("Count",next.toString())
      next++
      propTestStep.setPropertyValue("Next",next.toString())
      
      if(counter==size-1)
      {
      	propTestStep.setPropertyValue("StopLoop","T")
      	log.info"Setting the stoploop property now..."
      }
      else if(counter==0)
      
      {
      	def runner=new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase,null)
      	propTestStep.setPropertyValue("StopLoop","F")
      }
      else
      {
      	propTestStep.setPropertyValue("StopLoop","F")
      }

      With that code, I can get the script to run all the Mac Addresses I insert on the file but I need to insert another cycle to run this script for every line on the second worksheet of the "macs.xls" file.