title: SoapUI Groovy - Creating Dynamic XML based on person count
body: <p>I'm trying to dynamically create a chunk of xml in SoapUI using Groovy scripting that outputs names, emails etc as firstnameONE, firstnameTWO etc from the count of passengers that my test passes in.</p>
  
  <p>I've got as far as generating the passenger block for each passenger, per type of passenger (adult, child, infant). However I'm struggling to define the naming convention based on the count.</p>
  
  <p>In addition to this the passenger XML needs to take in to consideration the passenger type it is on i.e. if it is an infant use the infant list for titles and update the  value to 'Infant'.</p>
  
  <p>Here's my code so far - some of the values are passed in from test properties but you can swap for stored values:</p>
  
  <pre><code>numOfAdults = testRunner.testCase.testSuite.getPropertyValue("NumberOfAdults").toInteger()
  numOfChildren = testRunner.testCase.testSuite.getPropertyValue("NumberOfChildren").toInteger()
  numOfInfants = testRunner.testCase.testSuite.getPropertyValue("NumberOfInfants").toInteger()
  
  
  totalPax = numOfAdults + numOfChildren + numOfInfants
  
  firstName = "Firstname"
  surname = "Surname"
  emailAdd = "firstname" + ".surname@random.com"
  
  adultTitle = ["Mr","Mrs","Miss","Ms"].sort{new Random()}?.take(1)[0]
  infantTitle = ["Mstr","Miss"].sort{new Random()}?.take(1)[0]
  gender = ["Female","Male"].sort{new Random()}?.take(1)[0]
  
  log.info "Total Pax " + totalPax
  
  def passengersXML = ""
  
  def passengerXML = """
  &lt;Passenger&gt;
      &lt;DateOfBirth&gt;2001-01-05T09:24:39.7911558+00:00&lt;/DateOfBirth&gt;
      &lt;EmailAddress&gt;FirstNameOne.SurnameOne@random.com&lt;/EmailAddress&gt;
      &lt;FirstName&gt;$firstName&lt;/FirstName&gt;
      &lt;Gender&gt;$gender&lt;/Gender&gt;
      &lt;PassengerNum&gt;1&lt;/PassengerNum&gt;
      &lt;PersonGuid&gt;abcdefghijklmno&lt;/PersonGuid&gt;
      &lt;Surname&gt;SurnameOne&lt;/Surname&gt;
      &lt;Title&gt;Miss&lt;/Title&gt;
      &lt;Type&gt;Adult&lt;/Type&gt;
  &lt;/Passenger&gt;
  """
  
  for (i = 1; i &lt;=numOfAdults; i++){
  
      passengersXML += passengerXML
  }
  //
  //
  for (i = 1; i &lt;=numOfChildren; i++){
  
      passengersXML += passengerXML
  }
  //
  //
  for (i = 1; i &lt;=numOfInfants; i++){
  
      passengersXML += passengerXML
  }
  
  
  def langs = new XmlParser().parseText(passengerXML)
  
  log.info passengersXML
  
  testRunner.testCase.testSuite.setPropertyValue("PassengerResponse", passengerXML)
  </code></pre>
  
  <p>Desired output:</p>
  
  <p>This is an example based on 1 adult, 1 child and 1 infant but could be any number of these:</p>
  
  <pre><code>&lt;Passenger&gt;
      &lt;DateOfBirth&gt;2001-01-05T09:24:39.7911558+00:00&lt;/DateOfBirth&gt;
      &lt;EmailAddress&gt;FirstNameOne.SurnameOne@random.com&lt;/EmailAddress&gt;
      &lt;FirstName&gt;$firstNameOne&lt;/FirstName&gt;
      &lt;Gender&gt;$gender&lt;/Gender&gt;
      &lt;PassengerNum&gt;1&lt;/PassengerNum&gt;
      &lt;PersonGuid&gt;abcdefghijklmno&lt;/PersonGuid&gt;
      &lt;Surname&gt;SurnameOne&lt;/Surname&gt;
      &lt;Title&gt;Miss&lt;/Title&gt;
      &lt;Type&gt;Adult&lt;/Type&gt;
  &lt;/Passenger&gt;
  &lt;Passenger&gt;
      &lt;DateOfBirth&gt;2001-01-05T09:24:39.7911558+00:00&lt;/DateOfBirth&gt;
      &lt;EmailAddress&gt;FirstNameTwo.SurnameTwo@random.com&lt;/EmailAddress&gt;
      &lt;FirstName&gt;$firstNameTwo&lt;/FirstName&gt;
      &lt;Gender&gt;$gender&lt;/Gender&gt;
      &lt;PassengerNum&gt;2&lt;/PassengerNum&gt;
      &lt;PersonGuid&gt;abcdefghijklmno&lt;/PersonGuid&gt;
      &lt;Surname&gt;SurnameTwo&lt;/Surname&gt;
      &lt;Title&gt;Mr&lt;/Title&gt;
      &lt;Type&gt;Child&lt;/Type&gt;
  &lt;/Passenger&gt;
  &lt;Passenger&gt;
      &lt;DateOfBirth&gt;2001-01-05T09:24:39.7911558+00:00&lt;/DateOfBirth&gt;
      &lt;EmailAddress&gt;FirstNameThree.SurnameThree@random.com&lt;/EmailAddress&gt;
      &lt;FirstName&gt;$firstNameThree&lt;/FirstName&gt;
      &lt;Gender&gt;$gender&lt;/Gender&gt;
      &lt;PassengerNum&gt;3&lt;/PassengerNum&gt;
      &lt;PersonGuid&gt;abcdefghijklmno&lt;/PersonGuid&gt;
      &lt;Surname&gt;SurnameThree&lt;/Surname&gt;
      &lt;Title&gt;Miss&lt;/Title&gt;
      &lt;Type&gt;Infant&lt;/Type&gt;
  &lt;/Passenger&gt;
  </code></pre>