Creating automatic folder and files in soapui

让人想犯罪 __ 提交于 2019-12-08 06:45:35

问题


I wrote a groovy script in soapui to create files in certain location in my pc. How can I make it dynamic and enable the user to write the location the files are saved to by write the location in configuration file imported at test suite level.

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

回答1:


If you want to get the path from a testSuite property, you can do it as you do with the project property, using context.expand:

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

Or alternatively you can do the same with:

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

Maybe this is out of scope for your question, but could be helpful. If you need you can also use UISupport to ask the user to enter the path he wants with the follow code:

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

This shows:




回答2:


Define project level custom property REPORT_PATH with value D:/Reports/CSVReport.csv i.e., full path including file and path separate by / slash even on windows platform.

Then use the below script to write the data.

//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)


来源:https://stackoverflow.com/questions/40086411/creating-automatic-folder-and-files-in-soapui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!