Reputation: 849
I am using SoapUI Pro to test a set of web services for a postal company.
As part of my test cases I generate label and manifest documents in pdf format. I store these pdfs in a folder that is created by a groovy script at the start of the test case. Sometimes when I run the test no labels or pdfs are generated because I am testing for error codes i.e. using test input that does not create a shipment and thus no label is produced.
I have a script at the end of the test case which will delete the folder that I have created if it is empty. I want to modify it to zip the entire folder if it is not empty i.e. has label pdfs in it. But not sure how to do this.
Below is my script that deletes the empty folders. Sorry I haven't attempted any code to do the zipping.
import groovy.xml.NamespaceBuilder
import org.apache.tools.antBuilder.*
//This script deletes the pdf sub folder created by 'CreateFolderForPDFs' if no pdfs are generated as part of test
// i.e test does not produce valid shipments.
def pdfSubFolderPath = context.expand( '${#TestCase#pdfSubFolderPath}' )
def pdfSubFolderSize = new File(pdfSubFolderPath).directorySize()
if( pdfSubFolderSize == 0){
new File(pdfSubFolderPath).deleteDir()
log.info "Deleted the pdf sub folder " +pdfSubFolderPath +"because contains no pdfs"
}
else
{
//zip folder and contents
}
Upvotes: 1
Views: 1136
Reputation: 18507
I modify your code to zip all the files in your pdfSubFolderPath
:
import groovy.xml.NamespaceBuilder
import org.apache.tools.antBuilder.*
import java.util.zip.ZipOutputStream
import java.util.zip.ZipEntry
import java.nio.channels.FileChannel
//This script deletes the pdf sub folder created by 'CreateFolderForPDFs' if no pdfs are generated as part of test
// i.e test does not produce valid shipments.
def pdfSubFolderPath = context.expand( '${#TestCase#pdfSubFolderPath}' )
def pdfSubFolderSize = new File(pdfSubFolderPath).directorySize()
if( pdfSubFolderSize == 0){
new File(pdfSubFolderPath).deleteDir()
log.info "Deleted the pdf sub folder " +pdfSubFolderPath +"because contains no pdfs"
}
else
{
//zip folder and contents
def zipFileName = "C:/temp/file.zip" // output zip file name
def inputDir = pdfSubFolderPath; // dir to be zipped
// create the output zip file
def zipFile = new ZipOutputStream(new FileOutputStream(zipFileName))
// for each file in the directori
new File(inputDir).eachFile() { file ->
zipFile.putNextEntry(new ZipEntry(file.getName()))
file.eachByte( 1024 ) { buffer, len -> zipFile.write( buffer, 0, len ) }
zipFile.closeEntry()
}
zipFile.close()
}
Hope this helps,
Upvotes: 1