2016-11-07 43 views
1

要求:從該文件夾讀取xml文件並將該文件的內容傳遞給Soap請求。Groovy腳本讀取一個xml文件並更新文件內容的下一步請求

問題我正在嘗試使用groovy腳本讀取保存在文件夾中的文件,但無法讀取文件的內容。在嘗試打印xml文件的內容時,我得到空指針異常。

def fileList = [] 
new File("C:\\Users\\Documents\\Groovy Scripts\\requests").eachFile 
{ f -> 
if (f.isFile()&& f.name.endsWith('.xml')) 
{ 
def filename = f.name[0..-5] 
fileList.add(filename) 
log.info filename 

} 
} 
if (fileList.size() <1) 
{ 
testRunner.fail("No request files found") 
} 
context.put('fileList', fileList) 

def f = new File("C:\\Users\\Documents\\Groovy Scripts\\requests\\${context.fileList}.last().text") 
log.info f 

更新根據意見,增加了問題。

我的測試用例包含3個步驟。第1步:從文件夾中讀取xml文件。第2步:使用XML文件內容作爲肥皂請求輸入。步驟3:將輸出文件夾中步驟2的響應保存爲xml。

+0

你在做數據驅動的測試嗎?你能展示你的測試用例的結構嗎? – Rao

+0

的可能的複製[如何閱讀在Groovy文件轉換成字符串?](http://stackoverflow.com/questions/7729302/how-to-read-a-file-in-groovy-into-a-string) – Rao

+0

我的測試用例包含3個步驟。第1步:從文件夾中讀取xml文件。第2步:使用XML文件內容作爲肥皂請求輸入。步驟3:將輸出文件夾中步驟2的響應保存爲xml。 – user3212324

回答

1

這是明白,你需要做的數據驅動測試,其中請求被保存在目錄中。

以前,一種方法被提供here到環路直通的數據和保存的響應。

你現在可能需要的變化是在第一步 - 讀取目錄,並循環通過你的文件和設置文件內容的要求和運行SOAP請求的一步。

Groovy腳本的第一步:

import groovy.io.FileType 

//change your input directory name below 
def dir = new File('path/to/input/dir') 
dir.eachFile (FileType.FILES) { file -> 

    //Get the step 
    def step = context.testCase.getTestStepAt(1) 
    //Set the file content as test step request 
    step.testRequest.requestContent = file.text 
    log.info "New request is set for step2 : ${request}" 
    //Run the step2 
    step.run(testRunner, context) 
} 
//By now all the orders got executed, now need to exit the step without additionally running step2 
//So, jump to step2, index is 2 
testRunner.gotoStep(2) 

您可以繼續使用剩下的步驟在上面提供的鏈接提到。

+0

非常感謝它的工作:) – user3212324

+1

很高興知道這是有幫助的。如果你希望你能提高對你有用的答案。 – Rao

+0

我正在嘗試爲自動化添加更多功能。在這裏,我試圖在Excel工作表中添加這些字段。只有最後一個文件響應才被記錄下來。你能幫我寫一下如何爲所有的文件回覆寫回應。注意:每個文件的反應必須在新行 – user3212324

相關問題