How to extract a value from a label if the label is repeated
Hi all
I'm newbie with SOAP but i'm trying to do my best with my very first TestSet automation.
At the moment (most of the test case is done) i have only one issue, i don't know how to look into the Response for a special value (to put it into a propertie) if this value is in a label that is repeated through the Response.
For example:
I would like to extract the "PricedCategoryCode" value but only if the BreakdownType value contain a "C" and Amount Value = 100, so in thsi example the value to extrat to a propertie would be "ST"
<alpha:CategoryOptions> <alpha:CategoryOption CategoryLocation="Deluxe" PricedCategoryCode="RS"> <alpha:PriceInfos> <alpha:PriceInfo Amount="1289.00" BreakdownType="0201A" NCCFAmount="0.00" NonRefundableType="1" PromotionDescription="STD 1" FareCode="D2042767"/> <alpha:PriceInfo Amount="0" BreakdownType="0201A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="01 A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="01 " FareCode="D2042767"/> <alpha:PriceInfo Amount="1289.00" BreakdownType="0202C" NCCFAmount="0.00" NonRefundableType="1" PromotionDescription="STD 1" FareCode="D2042767"/> <alpha:PriceInfo Amount="0" BreakdownType="0202A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="02 A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="02 " FareCode="D2042767"/> </alpha:PriceInfos> </alpha:CategoryOption> <alpha:CategoryOption CategoryLocation="Deluxe" PricedCategoryCode="ST"> <alpha:PriceInfos> <alpha:PriceInfo Amount="100.00" BreakdownType="0201C" NCCFAmount="0.00" NonRefundableType="1" PromotionDescription="STD 1" FareCode="D2042767"/> <alpha:PriceInfo Amount="0" BreakdownType="0201A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="01 A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="01 " FareCode="D2042767"/> <alpha:PriceInfo Amount="1139.00" BreakdownType="0202C" NCCFAmount="0.00" NonRefundableType="1" PromotionDescription="STD 1" FareCode="D2042767"/> <alpha:PriceInfo Amount="0" BreakdownType="0202A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="02 A" FareCode="D2042767"/> <alpha:PriceInfo BreakdownType="02 " FareCode="D2042767"/> </alpha:PriceInfos> </alpha:CategoryOption>
This response change every time y execute my code, so for me, if there are more than one value that match with the criterial, the first one will be fine for me.
Thanks in advance for your support!
As stated, using find you will only get the first result, so no issues with multiple results. The alternative would be findAll.
To solve the error you just need to make sure the result for categoryCode is a String by getting the text(). Same thing as the line above it.
String categoryCode = new XmlSlurper().parseText(context.response)
.CategoryOption
.find {it.PriceInfos.PriceInfo.find { it.'@BreakdownType'.text() == "02 A" }}
.'@PricedCategoryCode'.text()
context.testCase.setPropertyValue('CategoryList', categoryCode)Sorry I missed this before.
coopernico46,
See demo script. (use "log.info" instead of println)See the colored text (changes)
def categoryCode = new XmlSlurper().parseText(context.response).'**'.find{ it.PriceInfos.PriceInfo.find { it.'@BreakdownType'.text() == "02 A" } }.@PricedCategoryCode?.text() context.testCase.setPropertyValue('PricedCategoryCode', categoryCode)