我有一个带有以下 XML 帖子正文的采样器,
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetSoapRequest xmlns="http://www.demandmatrix.net/HBSIXML4/">
<InterfacePayload>
<ChannelIdentifierId>Dnata_XML4R</ChannelIdentifierId>
<Version>2005A</Version>
<Interface>HBSI XML 4 OTA</Interface>
<TimeOut>30</TimeOut>
</InterfacePayload>
<Message>
<RequestId>20220728122327zQOxgSf6ZQ</RequestId>
<Transaction>HotelRateAmountNotifRQ</Transaction>
<XML>
<OTA_HotelRateAmountNotifRQ xmlns="http://www.abcd.org/ORR/2003/05" TimeStamp="2022-07-28T12:23:27+00:00" Target="TEST" Version="1.003" PrimaryLangID="en-us" HotelCode="${HotelCode9}" HotelCityCode="XYZ" HotelName="abc_Hotel">
<POS>
<Source>
<RequestorID Type="abc" ID="abc"/>
<BookingChannel Type="2" Primary="true">
<CompanyName>abc</CompanyName>
</BookingChannel>
</Source>
</POS>
<RateAmountMessages HotelCode="${HotelCode9}" HotelCityCode="MEL" HotelName="XYZ">
<RateAmountMessage LocatorID="1">
<StatusApplicationControl Override="1" RatePlanCodeType="RatePlanCode" InvCodeApplication="InvCode" IsRoom="1" Start="${__timeShift(yyyy-MM-dd ,,P${StartDate9}D,,)}" End="${__timeShift(yyyy-MM-dd ,,P${EndDate9}D,,)}" RatePlanCode="${RatePlanCode9}" InvCode="${RoomCode9}" />
<Rates>
<Rate NumberOfUnits="1" RateTimeUnit="Day" UnitMultiplier="1">
<BaseByGuestAmts>
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${AdultPrice9}" AmountAfterTax="${__jexl2(${AdultPrice9}+(${AdultPrice9}*${Tax9}))}" NumberOfGuests="1" AgeQualifyingCode="10" />
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9}+${AdultPrice9}*2*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="2" AgeQualifyingCode="10" />
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9}+${AdultPrice9}*3*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="3" AgeQualifyingCode="10" />
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9}+${AdultPrice9}*4*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="4" AgeQualifyingCode="10" />
</BaseByGuestAmts>
</Rate>
</Rates>
</RateAmountMessage>
</RateAmountMessages>
</OTA_HotelRateAmountNotifRQ>
</XML>
</Message>
<Login>
<LoginType>Component</LoginType>
<ComponentInfo>
<ComponentType>Hotel</ComponentType>
<Id>${HotelCode}</Id>
<User>abc</User>
<Pwd>hu@QTUls#*3U2C$Yt0Ni</Pwd>
</ComponentInfo>
</Login>
</GetSoapRequest>
</soap:Body>
</soap:Envelope>
有两个要求,
csv 文件如下,
95106,BAR1,Double Deluxe Room,142,162,25.1,5,0.9,0.1
95106,BAR2,Double Deluxe Room,142,162,35.2,10,0.9,0.1
95106,BAR3,Double Deluxe Room,142,162,45.3,15,0.9,0.1
95106,BAR4,Double Deluxe Room,142,162,55.4,20,0.9,0.1
95106,BAR5,Double Deluxe Room,142,162,65.5,25,0.9,0.1
95106,BAR1,Double Room,142,162,75.6,30,0.9,0.1
95106,BAR2,Double Room,142,162,85.7,35,0.9,0.1
95106,BAR3,Double Room,142,162,95.8,40,0.9,0.1
我们尝试在 jsr223 的帮助下执行此操作,但它无法从 csv 中获取值,也无法解析 __jexl2 函数。我使用的是 Jmeter 5.6.3 有人可以帮忙吗!
您可以添加JSR223预处理器并使用如下代码:
def message = '''
<RateAmountMessage LocatorID="1">
<StatusApplicationControl Override="1" RatePlanCodeType="RatePlanCode" InvCodeApplication="InvCode" IsRoom="1" Start="${__timeShift(yyyy-MM-dd ,,P${StartDate9}D,,)}" End="${__timeShift(yyyy-MM-dd ,,P${EndDate9}D,,)}" RatePlanCode="${RatePlanCode9}" InvCode="${RoomCode9}" />
<Rates>
<Rate NumberOfUnits="1" RateTimeUnit="Day" UnitMultiplier="1">
<BaseByGuestAmts>
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${AdultPrice9}" AmountAfterTax="${__jexl2(${AdultPrice9}+(${AdultPrice9}*${Tax9}))}" NumberOfGuests="1" AgeQualifyingCode="10" />
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9}+${AdultPrice9}*2*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="2" AgeQualifyingCode="10" />
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9}+${AdultPrice9}*3*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="3" AgeQualifyingCode="10" />
<BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9}+${AdultPrice9}*4*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="4" AgeQualifyingCode="10" />
</BaseByGuestAmts>
</Rate>
</Rates>
</RateAmountMessage>'''
def randomNumber = org.apache.commons.lang3.RandomUtils.nextInt(1, 51)
def messages = new StringBuilder()
1.upto(randomNumber, { index ->
def lineFromCSV = new File('/path/to/your/file.csv').readLines().get(index - 1)
//do what you need with CSV file here
messages.append(message)
})
vars.put('messages', messages.toString())
然后将帖子正文从第 25 行到第 37 行替换为
${messages}
请参阅 Apache Groovy:Groovy 用于什么? 文章,了解有关 JMeter 中 Groovy 脚本编写的更多信息。
__jexl2() 函数在 JMeter 5.6.3 中工作正常: