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

时间:2016-10-17 12:29:07

标签: groovy 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");
}

2 个答案:

答案 0 :(得分:0)

如果要从testSuite属性获取路径,可以像使用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

这表明:

enter image description here

答案 1 :(得分:0)

使用值REPORT_PATH定义项目级别自定义属性D:/Reports/CSVReport.csv,即,即使在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)