我正在使用SoapUI Pro为邮政公司测试一组Web服务。
作为我的测试用例的一部分,我以pdf格式生成标签和清单文档。我将这些pdf存储在一个文件夹中,该文件夹是在测试用例开始时由groovy脚本创建的。有时,当我运行测试时,没有生成标签或pdf,因为我正在测试错误代码,即使用不创建货件的测试输入,因此不会生成标签。
我在测试用例的末尾有一个脚本,如果它是空的,它将删除我创建的文件夹。我想修改它以压缩整个文件夹,如果它不是空的,即其中有标签pdfs。但不知道该怎么做。
以下是删除空文件夹的脚本。对不起,我还没有尝试过任何代码来进行压缩。
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
}
答案 0 :(得分:1)
我修改您的代码以压缩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()
}
希望这有帮助,