acarroz5 acarroz5 - 11 months ago 97
Groovy Question

Property Expansion in DataSink

I'm trying to write test results to a time stamped file in ReadyAPI. I have a DataGen that creates the timestamp in the first step and then later in the DataSink, I use that timestamp in the filename of the out file. I've heard that Property Expansion is allowed in DataSinks but my file isn't being created.

Do I need to initialize and create the file first (Groovy Script)?

Out File Configuration in DataSink:


DataGen Config:
enter image description here

enter image description here

Test Steps

enter image description here


The last run's timestamp is being used in the datasink. So let's say these are the runs:

Run 1: 8:00:00 AM -> Timestamped value ??

Run 2: 8:15:00 AM -> Timestamped value 8:00:00 AM

Run 3: 8:30:00 AM -> Timestamped value 8:15:00 AM

It seems like the datasink is left with the last cached version of the property and this doesn't get updated BEFORE the new run begins

Answer Source

The problem wasn't with timestamps or file creation at all. The issue was trying to use a property expansion in the DataSink. The only way to use a timestamp as the filename is to create it in the Test Case TearDown Script. This ensures that all outfiles are closed and no locks are in place.

To fix this problem, use a temp file to write to during the test - in the DataSink. Then in the TearDown Script, create your timestamp, create the new out file, and write the contents of the old file to it.

Here is the Groovy script I ended up using:

import jxl.*
import jxl.write.*
import java.text.SimpleDateFormat

def timestamp = ''
timestamp = new Date().format('yyyy_MM_dd_HH_mm_ss')

def tempFile = context.testCase.getPropertyValue('tempFile')

def output = tempFile +"_" + timestamp + ".xlsx"
tempFile += ".xlsx"

new File(output) << new File(tempFile).bytes