Ask a Question

File Written twice on executing the Groovy Script

Unibet_Support
Contributor

File Written twice on executing the Groovy Script

Hi
I am trying to create a Script Assertion which writes into a file
On running the test suite the file is written more than once. I'm not sure why it does this .
here is my script assertion

def startDate = new Date().format("yyyy_MM_dd_HHmmssSSS")
def filename = "C:/Users/smipan/Desktop/SoapUIPRO/LiveEvents/EventId_"+startDate+"_LiveFeeds.csv"
dataStore = new File( filename )
for (i in 1..8)
{

def current = "Test Line" + i
log.info current
dataStore.append( current)
}


The same script executed inside a groovy script is writing the file only once. What is the difference in how the script is executed when within a groovy script and within a script assertion.


Thanks and regards
Smitha
7 REPLIES 7

Re: File Written twice on executing the Groovy Script

Hi Smitha,

this is a known issue - ie that a script assertion is being executed twice in some situations - for now you will need to guard against this yourself, for example store a flag in the context or make sure that you only create the file if it hasn't already been created.

Sorry for the inconvenience,

regards!

/Ole
SmartBear Software
Unibet_Support
Contributor

Re: File Written twice on executing the Groovy Script

Hi
How do I store a flag in the context ?
I append the filename with a time stamp and it is Looping through multiple times for different values meant to create unique files for diffferent values (it might not be unique and that's what i'm trying to identify that there is no duplicate content generated by my application) . Therefore its not easy to identify whether the script was executed as a part of the loop or is this just as a result of "multiple execution". Is there a fix planned for this problem in a future release ? Do you have an estimate if and when it will be fixed ?

As I see it , the best option i would have is to use a datastore to loop through and use a groovy script to store the files rather than just through a script assertion . Is there another easier alternative ?

thanks and Regards
Smitha
M_McDonald
Super Contributor

Re: File Written twice on executing the Groovy Script

Does this help?

Change the assertion to this:

if (context.alreadyWritten == null || !context.alreadyWritten) {
def startDate = new Date().format("yyyy_MM_dd_HHmmssSSS")
def filename = "C:/Users/smipan/Desktop/SoapUIPRO/LiveEvents/EventId_"+startDate+"_LiveFeeds.csv"
dataStore = new File( filename )
for (i in 1..8)
{
def current = "Test Line" + i
log.info current
dataStore.append( current)
}
context.setProperty('alreadyWritten', true)
} else {
log.info ('Already written!')
}


Now add a Groovy script step after the request that resets the flag.

context.setProperty('alreadyWritten', false)


This is to allow the request to write the files if it is re-executed due to looping.
"Ignorance more frequently begets confidence than does knowledge"
RhettS
New Contributor

Re: File Written twice on executing the Groovy Script

Is this problem still a known bug in SoapUI Pro 5.1.1?

I encountered the same problem today, and want to make sure it's not caused by something else.

Re: File Written twice on executing the Groovy Script

Hi RhettS,

I confirm this is still currently a bug in 5.1.2 (SOAP-2783).
You can take a look at M McDonald's workaround script using a flag to prevent multiple executions of the script.

Regards,

Giscard
SmartBear Support
FC
Frequent Visitor

Re: File Written twice on executing the Groovy Script

Hi.

 

Is this still issue still present in 5.3.0 ? I am encountering this issue when assertion script is ran and I am appending to file.

 

I have applied the workaround but it still prints twice.

 

Cheers

FC

 

 

spartanhooah
Occasional Contributor

Re: File Written twice on executing the Groovy Script

The workaround worked for me (non-Pro v 5.3.0), but holy cow, this is still a bug after five and a half years?! That is a very long time for such a bug to persist!

cancel
Showing results for 
Search instead for 
Did you mean: