Forum Discussion

hellrid1's avatar
hellrid1
Occasional Contributor
13 years ago

get filename from XML request file in directory datasource

I have a DataSource that reads XML files from a directory using a DataSource loop
for SoapUI Pro Version 4.0.

I also have a Groovy script that writes the XML response to a file in another directory.
Currently, it uses a time/date stamp to name each XML Response file.

I would like to get the filename for each file that is read from the datasource, and use
the input filename to rename the output response XML files, so that I can tell which
response file corresponds to which input request file.

Also, I want to do this using a Groovy script.

Here is a copy of my current Groovy script that uses a time/date stamp to name the files:


date = new Date();
dateFormat = new java.text.SimpleDateFormat('MM-dd-yyyy');
dateStamp = dateFormat.format(date);
createFolder = new File("c:/Output/Responses " + dateStamp);
createFolder.mkdir();

time = new Date();
timeFormat = new java.text.SimpleDateFormat("HH.mm.ss.SS");
timeStamp = timeFormat.format(time);

def FileName = new PrintWriter( new FileWriter( "c:/Output/Responses " + dateStamp + "/" + dateStamp + " " + timeStamp + " response.xml.txt" ));
def ResponseMessage = testRunner.testCase.testSteps["Test Request"].testRequest.response.contentAsString;

def request = testRunner.testCase.getTestStepByName( "Test Request" );
def req = request.getProperty( "Request" );
def resp = request.getProperty( "Response" );

FileName.print( resp.value );
FileName.flush();
FileName.close();
log.info( resp.value);

3 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    try adding a property named "Filename" to the Directory DataSource, it should get populated for each row with the name of the corresponding file - you can then use the value of this property in your script as usual.

    Does that help?

    regards,

    /Ole
    SmartBear Stockholm
  • hellrid1's avatar
    hellrid1
    Occasional Contributor
    That worked!

    The filename was

    "OH Row 00001 ID 1811 PRODUCT-2581-3007 Output.xml"

    I created the "Filename" property in the directory DataSource.



    In the Groovy script, I right clicked for the “Get Data” option to generate this line of code:

    def fn = context.expand( '${ DataSource#Filename}' )

    Then I split the string to get the value that I needed:

    String [] fnSplit = fn.split(" ");
    def id = fnSplit[4];
    log.info(id)


    The only thing This is a very useful feature because I can pass values that are not part of the XML.

    I was wondering is where is this documented.

    How is anybody supposed to know that this feature exists?

    Thank You,
    Dan H.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Dan,

    It's one of those undocumented features that will make your look like a hero when you know them :-)

    You're point is valid of course - this needs to be added to the documentation - sorry for it not being there for now..

    regards!

    /Ole
    SmartBear Stockholm