Groovy如何将控制台输出保存到文件?

时间:2015-01-26 17:53:16

标签: groovy soapui

我在Soapui有一个项目和一个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中是否有等效的代码? 谢谢你

1 个答案:

答案 0 :(得分: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))               
}