文件未存储在文件夹目录中

时间:2017-01-15 14:15:18

标签: groovy soapui

我在尝试将文件放在我创建的文件目录中时遇到问题。我希望文件进入创建的文件夹' GET_Tests {Test}'但它没有进入这个文件夹,而是将文件放在文件夹所在的同一目录中。

我尝试了一些尝试让它工作但没有运气,我需要更改什么才能将文件存储在文件夹中?

以下是代码。一个脚本是ReadData,另一个是PrintToLogFile。 ReadData创建文件夹,而PrintTologFile创建文件。

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 个答案:

答案 0 :(得分:0)

只需将响应保存到文件中,就不需要额外的Groovy Script测试步骤。相反,可以使用下面的Script Assertion添加到同一个REST测试步骤请求中,按照内嵌注释进行操作。

脚本断言

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)