在soapui中创建自动文件夹和文件

Creating automatic folder and files in soapui

我在 soapui 中写了一个 groovy 脚本来在我电脑的特定位置创建文件。如何使其动态化并使用户能够通过在测试套件级别导入的配置文件中写入位置来写入保存文件的位置。

if(context.expand('${#Project#ProduceReports}') == 'true') {    
    def resultDir = new File("D:\Reports"); 
    if(!resultDir.exists()) {
        resultDir.mkdirs();
    }
    def resultsFile = new File(resultDir, "CSVReport.csv");
}

如果您想从测试套件 属性 获取路径,您可以像处理项目 属性 一样使用 context.expand:

def yourPath = context.expand('${#TestSuite#pathDirectory}')

或者您也可以使用以下方法执行相同操作:

def yourPath = context.testCase.testSuite.getPropertyValue('pathDirectory')

也许这超出了您的问题范围,但可能会有所帮助。如果你需要你也可以使用UISupport让用户输入他想要的路径,代码如下:

def ui = com.eviware.soapui.support.UISupport;
// the prompt question, title, and default value
def path = ui.prompt("Enter the path","Title","/base/path");
log.info path

这表明:

使用值 D:/Reports/CSVReport.csv 定义项目级自定义 属性 REPORT_PATH,即包括文件和路径在内的完整路径即使在 windows 平台上也由 / 斜杠分隔.

然后使用下面的脚本写入数据。

//Define the content that goes as report file. Of course, you may change the content as need by you
def content = """Name,Result
Test1,passed
Test2,failed"""

//Read the project property where path is configured
def reportFileName = context.expand('${#Project#REPORT_PATH}')

//Create file object for reports
def reportFile = new File(reportFileName)

//Create parent directories if does not exists
if (!reportFile.parentFile.exists()) {
    reportFile.parentFile.mkdirs()
}

//Write the content into file
reportFile.write(content)