Forum Discussion
PaulDonny
12 years agoRegular Contributor
divijd wrote: Thanks Roa its working fine after removing CData
def holder = groovyUtils.getXmlHolder( 'Search#Response' );
but now it is printing all the values. I need a random no which will select the values from the results fetched.
Steps will be.
Step 1. Count the no. of results provided in the response.
Step 2. Then get a random number from that count.
Step 3. Then use those specific values for the input of next step.
log.info matcher[random.nextInt(matcher.size().toInteger())][2];
That will return the value, if you want both the company name and model number, your going to have to manipulate the code a bit.
//Initializing groovyUtils
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
//Getting the XML response holder from the test case named 'CData Test'
def holder = groovyUtils.getXmlHolder("CData Test#Response");
//Let's make it pretty
def xml = holder.getPrettyXml()
//def regexp = ("<([^<>]+)>(.*?)</\\1>");
//I edited the regex to look for the company name. The above regex was commented out.
def regexp = ("<(companyName)>(.*?)</\\1>");
//Find the matches for the above regex
def matcher = xml =~ regexp;
//Repeat these steps for the modelNumber portion
def regexp2 = ("<(modelNumber)>(.*?)</\\1>");
def matcher2 = xml =~ regexp2;
//At this point, all of your data has been grabbed and parsed with regex, stored in matcher and matcher 2. Now we need to grab out random values
//To do this, get the size of one of the matchers and make a random number using that as the largest number possible. Store it to a variable
random = new Random();
int rand = random.nextInt(matcher.size().toInteger());
//Use the random number 'rand' to get the values and print it to the console. Change log.info to be the code you need to continue your testing.
log.info matcher[rand][2];
log.info matcher2[rand][2];
Again, change the test name (The CData Test portion), and instead of log.info, manipulate the data how you want it to be manipulated.