2015-01-26 61 views
1

我在Soapui有一个项目和一个运行项目所有测试的groovy代码。 结果可以在输出控制台中看到。 所以我想要的是保存输出ina文件的内容。Groovy如何将控制台输出保存到文件?

在Java中,我们可以这样做:

//create a buffered reader that connects to the console, we use it so we can read lines 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

     //read a line from the console 
     String lineFromInput = in.readLine(); 

     //create an print writer for writing to a file 
     PrintWriter out = new PrintWriter(new FileWriter("c:/temp7/output.txt")); 

     //output to the file a line 
     out.println(lineFromInput); 

     //close the file 
     out.close(); 

是否有一个等效代码在Groovy? 谢谢您

+0

我相信你会从soapui工具运行该项目。对?但是为了收集输出,你在哪里运行上面的java代码呢?顺便说一句,相同的代码应该也适用于常规。 – Rao 2015-01-26 18:06:22

+0

我在所有测试结束时从groovy步骤运行此代码。我尝试了相同的代码,但我有错误,它应该是这样的def file = new File('c:/temp7/sample.txt') file.withWriter('UTF-8'){ it.writeLine '将此文字添加到文件中。' }在这里,我应该添加控制台输出的内容。 – 2015-01-26 18:09:35

+0

你的意思是说,该项目是从一个groovy步骤本身运行? – Rao 2015-01-26 18:12:43

回答

0

当然,您可以阅读控制台输出。 但是你需要从GUI执行你的测试,而不是从testrunner的命令行执行。

// Can be altered to 'soapUI log', 'http log', 'jetty log', 'error log' etc. 
def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea("http log"); 

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
if(logArea !=null) 
{ 
    def model = logArea.model 
    // only continue of logArea not empty 
    if(model.size > 0)    
    for(c in 0..(model.size-1)) 

    // DO SOMETHING HERE... could write to file, could run assertions, etc. 
    writeToFile(model.getElementAt(c))    
} 
相关问题