2015-01-26 141 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))    
} 
相關問題