【问题标题】:Groovy - How to create path directory with variable insideGroovy - 如何创建内部变量的路径目录
【发布时间】:2019-04-28 12:26:23
【问题描述】:

我在 groovy 中有一个脚本来创建一个带有结果的文本文件

我定义的路径文件是 C:/temp/SoapTest/,我想动态创建一个新文件夹,它会在任何新用户测试中发生变化,所以 我想使用定义的变量作为文件夹,所以本例中的变量是 Affected CI 。

这是我的完整代码:

nl=System.getProperty("line.separator");
def Date0 = testRunner.testCase.testSuite.getPropertyValue( "Date")
def DateIn = testRunner.testCase.testSuite.getPropertyValue( "DateIn")
def Summary = testRunner.testCase.testSuite.getPropertyValue( "Summary")
def SummaryIn = testRunner.testCase.testSuite.getPropertyValue( "SummaryIn")
def RandomID = testRunner.testCase.testSuite.getPropertyValue( "RandomID")
def AffectedCI = testRunner.testCase.testSuite.getPropertyValue( "AffectedCI")
def SourceStatus = testRunner.testCase.testSuite.getPropertyValue( "SourceStatus")
def event = 'Active-Inactive'
def INC = testRunner.testCase.testSuite.getPropertyValue( "INC")
def IID_Active = testRunner.testCase.testSuite.getPropertyValue( "IID Active")
def IID_Inactive = testRunner.testCase.testSuite.getPropertyValue( "IID Inactive")
class LeftShiftNewlineCategory {
    static Writer leftShift(Writer self, Object value) {
        self.append value + "\n\r"
    } 
}
use(LeftShiftNewlineCategory) {
def pathfile = 'C:/temp/SoapTest/'
def subpath = 'Active-Inactive '
def fileformat = '.txt'
def FinRes = AffectedCI
def datetime = new Date().format(" dd MM yy' 'HH-mm-ss") 
def file = new File(pathfile + subpath + FinRes + datetime + fileformat).withWriter { out ->
        out << "Date:" +"\t"+"\t"+"\t"+ Date0  
        out << "${nl}DateIn:" +"\t"+"\t"+"\t"+ DateIn 
        out << "${nl}RandomID:" +"\t"+"\t"+ RandomID
        out << "${nl}AffectedCI:" +"\t"+"\t"+ AffectedCI 
        out << "${nl}SourceStatus" +"\t"+"\t"+ SourceStatus
        out << "${nl}event" +"\t"+"\t"+"\t"+ event
        out << "${nl}INC" +"\t"+"\t"+"\t"+ INC 
        out << "${nl}IID Active" +"\t"+"\t"+ IID_Active
        out << "${nl}IID Inactive" +"\t"+"\t"+ IID_Inactive 
        out << "${nl}"               
        out << "${nl}Payload:" +"\t"+"\t"+ Summary        
        out << "${nl}"               
        out << "${nl}PayloadIn:" +"\t"+"\t"+ SummaryIn
    }
}

非常感谢您!

【问题讨论】:

标签: java groovy


【解决方案1】:

您可能需要先创建父目录:

def file = new File(pathfile + subpath + FinRes + datetime + fileformat)
def success = file.parentFile.mkdirs()
println "created ${file.parentFile}: ${success}"
file.withWriter { out ->
  ...
}

【讨论】:

  • 主要的父目录已经定义好了。我不能创建使用变量“AffectedCI”自动命名的子路径文件夹所以如果我在不同的测试中使用几个不同的 AffectedCI,groovy 脚本将根据 AffectedCI 名称创建新文件夹
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 2010-11-25
  • 2021-10-02
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
相关资源
最近更新 更多