Forum Discussion

Timtimminey's avatar
Timtimminey
New Contributor
8 years ago

Need help with groovy script command to archive xml file after processing

I am new to SoapUI and Groovy but after a lot of research I was able to build a simple TestCase with 3 steps that loops through all *.xml files in a folder, submits them one at a time automatically, and captures the responses in a separate folder. It works great except it leaves the original XML message in the Requests folder and I'd like to move the file, after the Response file is created successfully, and rename it with ".xml.processed". The archive folder path would be C:\XML_SUBMISSION\XML_Requests\archive\.

 

I've spent hours researching "archiving" or "moving" xml requests after submission and have tried numerous renaming samples but nothing seems to work. I would think this would be a somewhat common practice when testing multiple submissions.

 

Here is the code:

 

Step 1 to get the file name:

 

def fileList = []

new File("C:\\XML_SUBMISSION\\XML_Requests").eachFile
{ f ->
if (f.isFile()&& f.name.endsWith('.xml'))
{
 def filename = f.name[0..-1]
 fileList.add(filename)
 //log.info filename
}
}

if (fileList.size() <1)
{
testRunner.fail("No request files found")
}
context.put('fileList',fileList)

 

Step 2 is the SoapUI Submission:

${=new File("C:\\XML_SUBMISSION\\XML_Requests\\" + (context.get('fileList')).last()).text}

 

 

 

Step 3 to capture the Response and loop back to Step 2:

 

def fileList = context.get('fileList')
 
def fileName = fileList.pop()
def newname = fileName[0..-5]
 
def response = context.expand( '${Step2#Response}' )
def f = new File("C:\\XML_SUBMISSION\\XML_Responses\\${fileName}_Response.xml")
f.write(response, "UTF-8")

//I thought the archiving command would go here but maybe at Step 3 I no longer have the original file context?
 
if(fileList.size() >0)
{
testRunner.gotoStepByName("Step2")
}

 

It seems like this is simple and I'm just missing something. Any help you're able to provide would be appreciated.

 

Thank you.

 

6 Replies

  • Radford's avatar
    Radford
    Super Contributor

    I know this isn't what you asked, but have you concidered using a Directory type Data Source step (paired with a DataLoop test step), these test steps will take care of all the complexities of looping through a directories of files, keeping track of the current file, etc.

     

    Attached is a trivial TestCase example of how to do this (Just import it into one of your TestSuites). While using Groovy is an invaluble tool, I always try to use the built-in test steps if there is one that does what I need. It tends to be a lot quicker, and I personally like the fact I can look at the list of test steps in a test case and get a good idea of whats going on by looking at the types.

     

    Note: While the documentation states the current file contents are put into the first property and all others are blank, as you will see from the attached example the second property is named "fileName" and this does contain the filename. Perhaps someone from SmartBear could comment? Is this just a case of functionality missing from the docs?

    • Timtimminey's avatar
      Timtimminey
      New Contributor

      Thanks Radford. I'm a functional consultant and tasked with testing a mass amount of order completions so I don't have the technical background to do much more than copy, paste and tweak. My current design works (maybe not the most elegant option) and was based on the one script I could find that actually loops through a bunch of individual xml files:

      http://www.codeproject.com/Articles/820414/Automating-SoapUI-using-Groovy-A-Walk-Through

       

      I've already processed thousands of records successfully but when this effort is done I'll see if I can redesign it based on your example.

       

      I appreciate it. 

    • Timtimminey's avatar
      Timtimminey
      New Contributor

      Thanks nmrao for your response. I do appreciate your review.

       

      I may not have been clear in my original Post. My current design loops through all of the xml files in a folder successfully and captures the Responses in a separate file. It works great. What I wanted to add as a 'nice to have' feature where after the original xml submission file was submitted and received a response, it would be moved to an archive folder. 

       

      One big reason for this is that if I have a thousand xml files in the submission folder and my process dies somewhere in the middle, I'll have a faster way of knowing which xml files weren't processed.

       

      I've read through the stackoverflow posts you linked to and they look like different variations of submitting a single file and capturing the response in a new file. I'm already doing this part successfully. If there were instructions on moving the original xml file after submission then I couldn't find it.

      • nmrao's avatar
        nmrao
        Champion Level 3
        Are you using data-driven feature of SoapUI NG feature in this test case?