我正在使用 Jmeter 向某些 API 發送多個 HTTPS 請求。我每次都需要用不同的資料組合這些請求的 XML 主體。
我有一個 csv 檔案,它看起來像這樣:
iDocType,iDocId,iName,iDate
P,555551555,Braiden,2022-12-31
I,100000001,Dominique,2024-12-10
P,100000002,Joyce,2025-11-15
.........
我在 JSR223 前處理器(在 HTTPS 采樣器下)中有這段代碼,它創建多個 XML 物件并用 csv 值一一填充它們,最后將此物件提供給 Jmeter 以在采樣器中用作請求正文資料:
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
def pReqID = 'ID0000'
def numberOfNodes = 3
//input from csv file
travelDocNbr = vars.get('iDocId').toInteger();
def lines = new File("TestData\\test.csv").readLines()
xml.nodes() {
1.upto(numberOfNodes, { lineNo ->
xml.object(a: 'false', b: 'false', pReqID: pReqID (lineNo).toString()) {
xml.al(ad: '2021-09-20', alc: 'bla', bla: '2021-09-20T11:00:00.000Z', sn: 'AB8912')
xml.doc(docType: lines.get(lineNo).split(',')[0],
docId: lines.get(lineNo).split(',')[1],
name: lines.get(lineNo).split(',')[2],
date: lines.get(lineNo).split(',')[3])
}
}
)
}
def nodeAsText = writer.toString()
//log.info(nodeAsText)
vars.put('passengers3XmlObj', nodeAsText)
這總是給出這個結果:
<nodes>
<object a='false' b='false' pReqID='ID00001'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='P' docId='555551555' name='Braiden' date='12/31/2022' />
</object>
<object a='false' b='false' pReqID='ID00002'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='P' docId='100000001' name='Dominique' date='12/10/2024' />
</object>
<object a='false' b='false' pReqID='ID00003'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='P' docId='100000002' name='Joyce' date='11/15/2025' />
</object>
</nodes>
The problem is that this script starts composing objects from the first value in the CSV file and always creates the same XML object in each sampler request's body. I need it to change values from the CSV each time. For this, I was thinking if I could have a variable which always changes in each sampler iteration, like the iName value (I get a different one on each sampler iteration thanks to the CSV Data Set config test element) and use that to find the line in the CSV file and start composing objects from that line onwards.
So, for example, given that the csv file has just the 3 lines mentioned at the beginning and I have the value "Dominique" stored in a variable I want to pass it to a function and get the following result:
<nodes>
<object a='false' b='false' pReqID='ID00001'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='P' docId='100000001' name='Dominique' date='12/10/2024' />
</object>
<object a='false' b='false' pReqID='ID00002'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='P' docId='100000002' name='Joyce' date='11/15/2025' />
</object>
<object a='false' b='false' pReqID='ID00003'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='P' docId='555551555' name='Braiden' date='12/31/2022' />
</object>
</nodes>
Can someone please tell me how this can be done?
uj5u.com熱心網友回復:
如果要獲取Dominique文本所在的行數,可以使用findIndexOf() 函式完成,例如:
def dominiquePosition = lines.findIndexOf { line -> line.contains('Dominique') }
然后您可以從上一行中檢索到的位置開始構建您的 XML:
dominiquePosition.upto(numberOfNodes, { lineNo ->
可以使用代表JMeterVariables類實體的vars.getIteration()函式獲得迭代。有關此和其他可用于 JSR223 測驗元素的JMeter API速記的更多詳細資訊,請參閱您應該與 Groovy 一起使用的 8 個 JMeter Java 類文章vars
uj5u.com熱心網友回復:
我會以一種自然的方式來撰寫代碼。
更新:我添加了更多 CSV 行來演示如何numberOfNodes影響輸出。
def pReqID = 'ID0000'
def numberOfNodes = 2
String startFrom = 'Dominique'
int pos = -1
def csv = """\
iDocType,iDocId,iName,iDate
P,555551555,Braiden,2022-12-31
I,100000001,Dominique,2024-12-10
P,100000002,Joyce,2025-11-15
P,555551555,Braiden,2022-12-31
I,100000001,Dominique,2024-12-10
P,100000002,Joyce,2025-11-15
""".readLines().drop( 1 ).withIndex().collect{
def ( docType, docId, name, date ) = it[ 0 ].trim().split( ',' )
if( -1 == pos && startFrom == name ) pos = it[ 1 ]
[ docType:docType, docId:docId, name:name, date:date ]
}
//here the skipped lines are reodered to the bottom, if needed
def reoderedCsv = -1 < pos ? csv[ pos..-1 ] csv[ 0..<pos ] : csv
def writer = new StringWriter()
new groovy.xml.MarkupBuilder(writer).nodes() {
reoderedCsv.take numberOfNodes eachWithIndex{ data, ix ->
object(a: 'false', b: 'false', pReqID:"$pReqID${ix 1}" ) {
al ad:'2021-09-20', alc:'bla', bla:'2021-09-20T11:00:00.000Z', sn:'AB8912'
doc data
}
}
}
println writer
印刷:
<nodes>
<object a='false' b='false' pReqID='ID00001'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='I' docId='100000001' name='Dominique' date='2024-12-10' />
</object>
<object a='false' b='false' pReqID='ID00002'>
<al ad='2021-09-20' alc='bla' bla='2021-09-20T11:00:00.000Z' sn='AB8912' />
<doc docType='P' docId='100000002' name='Joyce' date='2025-11-15' />
</object>
</nodes>
與numberOfNodes = 3多碼列印你的期望的輸出
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/337591.html
