如何从文本文件中同时使用 groovy 设置两个 属性 值
How to set two property value using groovy in the same time from a text file
我想测试 GetWeather 网络服务
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()
}
但我没有阅读文件。
有什么帮助吗,谢谢
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()
returns文件内容的列表,像这样:
[
'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
}
我想测试 GetWeather 网络服务
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()
}
但我没有阅读文件。 有什么帮助吗,谢谢
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()
returns文件内容的列表,像这样:
[
'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
}