Forum Discussion
OceanMachine
15 years agoNew Contributor
Thanks for the reply. I spent quite a bit of time looking at this, and found I understood what I was trying to achieve a little bit better.
I wrote a groovy script (which I'm sure isn't the most elegant code, and I'm sure there is probably a better way to do this) and I thought I would share it.
This script is designed to pull XML from a file, copy it into a Request, and submit the request. It will then loop through the whole file doing the same until it reaches the end.
The XML file should have requests with blank lines between whole requests (blank lines denote the end of the request). The script will concatenate lines together until it reaches a blank line in the file, then it will submit that concatenated string via the request inside the test case, clear the variable, and start concatenating again, etc.
For some reason this don't work using the "right click > Launch TestRunner" option, but it works if you open the Groovy Script editor for the test step and use the "Run this script in a separate thread using a mock testRunner and testContext" button.
Obviously change the name of your Request step to match what you have. I hope this helps someone else.
I wrote a groovy script (which I'm sure isn't the most elegant code, and I'm sure there is probably a better way to do this) and I thought I would share it.
This script is designed to pull XML from a file, copy it into a Request, and submit the request. It will then loop through the whole file doing the same until it reaches the end.
The XML file should have requests with blank lines between whole requests (blank lines denote the end of the request). The script will concatenate lines together until it reaches a blank line in the file, then it will submit that concatenated string via the request inside the test case, clear the variable, and start concatenating again, etc.
For some reason this don't work using the "right click > Launch TestRunner" option, but it works if you open the Groovy Script editor for the test step and use the "Run this script in a separate thread using a mock testRunner and testContext" button.
Obviously change the name of your Request step to match what you have. I hope this helps someone else.
import javax.swing.JFileChooser
//create the file chooser object with prompt
def fc = new JFileChooser(dialogTitle:"Choose input XML file")
// create the filter for "*.xml" files and set the default directory
fc.addChoosableFileFilter(new fileFilter())
fc.setCurrentDirectory(new File("C:\\"))
// open the 'chooser'
if (fc.showOpenDialog() == JFileChooser.APPROVE_OPTION)
{
// get the chosen file and path
File file = fc.getSelectedFile()
filename = file.getAbsolutePath()
// init some vas
xmlLine = ""
number = 1
totalnumber = 0
// count the total number of lines in the file
file.eachLine{totalnumber++}
// for debugging
log.info("total number of lines in " + filename + " is " + totalnumber)
// concatenate the lines until a blank line is found and then run the XML
file.eachLine
{ line ->
log.info("processing line " + number + " of " + totalnumber)
if ( line ) // if the line was not blank
{
if ( xmlLine != "" )
xmlLine += "\n"
xmlLine += line // concatenate the line
}
else if (xmlLine) // as long as the concat string is not empty, run it
{
log.info("submitting request for line number " + number)
testRunner.testCase.getTestStepByName("despatchShippingGroup - Request 1").getProperty("request").setValue(xmlLine)
testRunner.runTestStepByName("despatchShippingGroup - Request 1")
log.info("submit complete for line number " + number)
xmlLine = "" // clear the concat var
}
number++ // keep track of current line being processed
}
// deal with the last line in the file (would not run in the above if last line was not blank)
if ( xmlLine != "" )
{
log.info("submitting request for line number " + number)
testRunner.testCase.getTestStepByName("despatchShippingGroup - Request 1").getProperty("request").setValue(xmlLine)
testRunner.runTestStepByName("despatchShippingGroup - Request 1")
log.info("submit complete for line number " + number)
}
}
else // if user cancels the file choose dialog, do nothing.
{
log.info("File chooser cancel button clicked")
return
}
// class for file filter
class fileFilter extends javax.swing.filechooser.FileFilter
{
public boolean accept(File file)
{
if (file.isDirectory()) { return true }
String filename = file.getName()
return filename.endsWith(".xml")
}
public String getDescription() { return "*.xml" }
}
Related Content
- 2 years ago
Recent Discussions
- 55 minutes ago