2015-11-16 59 views
1

設置中同時有兩個屬性值我想測試中的GetWeather web服務如何使用Groovy從文本文件

http://www.webservicex.com/globalweather.asmx 

我有這個內容的文本文件: 蒙特利爾 加拿大 卡爾加里 加拿大

我的要求是:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET"> 
     <soap:Header/> 
     <soap:Body> 
      <web:GetWeather> 
      <!--Optional:--> 
      <web:CityName>${#Project#City}</web:CityName> 
      <!--Optional:--> 
      <web:CountryName>${#Project#Country}</web:CountryName> 
      </web:GetWeather> 
     </soap:Body> 
    </soap:Envelope> 

我Groovy代碼是:

def f = new File("c:\\temp\\Data.txt") 
def fr= new FileReader(f) 

def br = new BufferedReader(fr) 
def s = br.readLine() 
def x = br.readLine() 

while(s && x !=null) 
{ 
testRunner.testCase.setPropertyValue("City",s) 
testRunner.testCase.setPropertyValue("Country",x) 

testRunner.runTestStepByName("GetWeather - Request 1") 
s = br.readLine() 
x = br.readLine() 
} 

但我沒有讀取文件。 請任何幫助,謝謝

回答

2

Groovy簡化了閱讀文本文件的行。在你的情況下,由於創紀錄的由兩條線組成,試試這個:

def f = new File('c:\temp\Data.txt') 
def records = f.readLines().collate(2) 

records.each { 
    testRunner.testCase.setPropertyValue("City",it[0]) 
    testRunner.testCase.setPropertyValue("Country",it[1]) 

    testRunner.runTestStepByName("GetWeather - Request 1") 
} 

它是如何工作

讓我們假設輸入文件包含以下行:

New York 
USA 
Istanbul 
Turkey 

線1和2是城市和2號線和4號線是國家。 f.readLines()返回的文件內容的列表,這樣的說法:

[ 
    'New York', 
    'USA', 
    'Istanbul', 
    'Turkey' 
] 

爲了使數據更容易使用,我把它變成了城市和國家對的列表。這就是collate(2)做:

[ 
    ['New York', 'USA'], 
    ['Istanbul', 'Turkey]' 
] 

有了這個新的列表,each(Closure)用於通過對迭代。

records.each { 
    // it[0] is the city 
    // it[1] is the country 
} 
+0

謝謝你,它正在閱讀txt文件,但如何閱讀每一行,因爲我有不同的城市和國家名稱的多行。 – parisFoxparis

+0

不幸的是,我不明白你在問什麼。我在我的答案中添加了一個解釋。也許它會回答你的問題。 –

+0

現在我明白了,謝謝你的解釋。這個對我有用 – parisFoxparis