1

I'm using RestAssured to help me with some testing.

Given the following XML:

 <OptionalExtra ID="PREB" Description="Premium meal beef" Code="PREB" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="79.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
    <OptionalExtra ID="CHML" Description="Child meal" Code="CHML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
    <OptionalExtra ID="VLML" Description="Vegetarian meal" Code="VLML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
    <OptionalExtra ID="GFML" Description="Gluten-free meal" Code="GFML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>

How can I pick out all meals that contain the word 'child' in the Description attribute? I need it to be case insensitive.

The following throws no exceptions, but neither does it find the Code attribute 'CHML' I need:

List<String> allChildMeals;
            allChildMeals = response.xmlPath().getList("FAB_BasketRS.CurrentBasket.Itinerary.ItineraryOptions.OptionalExtra.findAll{it.@Type=='Meal' && it.@Description.grep(/[Child]/)}*.@Code");

I guess my Regex/grep is wrong?

Steerpike
  • 1,712
  • 6
  • 38
  • 71
  • `[Child]` matches a single `C`, `h`, `i`, `l` or `d` character. I guess you want `.*Child.*` instead, which will match any text that includes the `Child` subsequence. – Aaron Jun 11 '18 at 12:10
  • Possible duplicate of [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – Aaron Jun 11 '18 at 12:11
  • @Aaron thanks for the tip. What you say makes sense and I'm learning fast...but the improved regex you suggest still returns no results. I guess it's something to do with the context in which I am using it - XmlPath javadoc states "The XmlPath implementation of rest-assured uses a Groovy shell to evaluate expressions" – Steerpike Jun 11 '18 at 12:59
  • I'm not familiar with xmlpath's grep, but I notice you're using a `/regex/` construct that is usual in JS but unusual in Java (or Groovy AFAIK); you might want to try without the slashes, unless xmlpath's doc mentions to use them. If you did not have the execution environment necessary to run the regex I would expect an error to be raised and show up in the logs, I assume you have no error and just no result? – Aaron Jun 11 '18 at 13:36
  • yes, exactly - with the slashes I get no results but no exceptions. Without them I get java.lang.IllegalArgumentException: Invalid path: unexpected token: . @ line 1, column 132. al' && it.'@Description'.grep(.*Child.*) The XmlPath Javadoc seems to have no reference to a grep method. the only context I have seen it used is here: https://www.ontestautomation.com/using-jsonpath-and-xmlpath-in-rest-assured/ – Steerpike Jun 11 '18 at 14:01
  • I see there's an `~` in the linked article, i.e. `.grep(~/regex/)`, I guess it's worth a try? Also no `*` after the `.findAll{}` segment. I'm sorry, it'd be better if I could try the whole thing and come up with a definitive answer, but I haven't got much free time currently and such an environment wouldn't be trivial to set up... – Aaron Jun 11 '18 at 16:22

1 Answers1

1

I've managed to get results with your XML using this XPath query:

/foo/OptionalExtra[@Type='Meal' and contains(@Description, 'Child')]/@Code

I've modified the input a bit - inserted 'foo' as a root node and added a second result.

The modified input XML:

<foo>
<OptionalExtra ID="PREB" Description="Premium meal beef" Code="PREB" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="79.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
        <OptionalExtra ID="CHML" Description="Child meal" Code="FIRST RESULT" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
        <OptionalExtra ID="VLML" Description="Vegetarian meal" Code="VLML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
        <OptionalExtra ID="GFML" Description="Gluten-free meal" Code="GFML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
        <OptionalExtra ID="CHML" Description="Child meal" Code="SECOND RESULT" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
</foo>

Here's how it looks like. The return type used here is "Nodeset", otherwise not all results are returned.

enter image description here

You can test it at http://www.utilities-online.info/xpath/?save=55e705ac-14be-4d75-8fda-507c8da69e2d-xpath

tpx
  • 165
  • 2
  • 10