问题
I am having issues trying to place files within a file directory I have created. I want the files to go into the created folder 'GET_Tests{Test}' but instead of going into this folder, it is placing the files on the same directory the folder is within.
I have tried a few things to try and get it working but no luck, what do I need to change in order to get the files stored within the folder?
Below is the code. One script is ReadData and the other is PrintToLogFile. ReadData creates the folder whilst PrintTologFile creates the files.
ReadData:
// define properties required for the script to run.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def dataFolder = groovyUtils.projectPath
def date = new Date()
def folderTime = date.format("yyyy-MM-dd HH-mm-ss")
//Define an empty array list to load data from datasheet
def DataTable = [];
//Create a folder directory for the responses
RootResultFolder = dataFolder + "/Responses" + "\\GET_Tests{Test} - " + folderTime
CreateResultFolder = new File(RootResultFolder)
CreateResultFolder.mkdir()
PrintToLogFile
import groovy.json.JsonOutput
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def casename= testRunner.testCase.name
def response = testRunner.testCase.getTestStepByName("GET_Tests{Test}").getProperty("Response").getValue();
def hotelId = testRunner.testCase.getPropertyValue('hotelid')
def date = new Date().format("yyyy-MM-dd")
def time = new Date().format("HH.mm.ss")
def fileName = hotelId + " - D" +date+ " T" +time+ ".txt"
def dataFolder = context.getProperty("RootResultFolder")
def rootFolder = dataFolder + fileName
def logFile = new File(rootFolder)
回答1:
An additional Groovy Script
test step is not really needed to just save the response into a file. Instead a Script Assertion
can be added to the same REST test step request with below, follow in-line comments.
Script Assertion
import com.eviware.soapui.support.GroovyUtils
//Save the contents to a file
def saveToFile(file, content) {
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
log.info "Directory did not exist, created"
}
file.write(content)
assert file.exists(), "${file.name} not created"
}
//Get the project path
def dataFolder = new GroovyUtils(context).projectPath
//Create today's date for storing response
def today = new Date().format("yyyy-MM-dd")
def filePrefix = "${dataFolder}/Responses/GET_Ratings_hotelId_${today}" as String
def fileNamePart = new Date().format("yyyy-MM-dd'T'HH.mm.ss")
//Check if there is response
assert context.request, "Request is empty or null"
//create file object with file name
def file = new File("${filePrefix}/hotelId_${fileNamePart}.json")
//Call above method to save the content into file
saveToFile(file, context.response)
来源:https://stackoverflow.com/questions/41662120/files-not-being-stored-in-folder-directory