Forum Discussion

melajib's avatar
melajib
New Contributor
9 years ago

Updating regression tests when nodes in XML have moved

Hi   I have created a suite of tests and used XPath match assertions.  However when there is a new version of the service I am testing, the output XML nodes may have shifted position, thereby break...
  • nmrao's avatar
    9 years ago

    I believe, you might be having repeating element in the xml and the element order is not always same in the response like the same below. Will try to provide samples below how to deal with the same.

     

     

    <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications 
          with XML.</description>
       </book>
       <book id="bk102">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies, 
          an evil sorceress, and her own childhood to become queen 
          of the world.</description>
       </book>
       <book id="bk103">
          <author>Corets, Eva</author>
          <title>Maeve Ascendant</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-11-17</publish_date>
          <description>After the collapse of a nanotechnology 
          society in England, the young survivors lay the 
          foundation for a new society.</description>
       </book>
       <book id="bk104">
          <author>Corets, Eva</author>
          <title>Oberon's Legacy</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2001-03-10</publish_date>
          <description>In post-apocalypse England, the mysterious 
          agent known only as Oberon helps to create a new life 
          for the inhabitants of London. Sequel to Maeve 
          Ascendant.</description>
       </book>
       <book id="bk105">
          <author>Corets, Eva</author>
          <title>The Sundered Grail</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2001-09-10</publish_date>
          <description>The two daughters of Maeve, half-sisters, 
          battle one another for control of England. Sequel to 
          Oberon's Legacy.</description>
       </book>
       <book id="bk106">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-09-02</publish_date>
          <description>When Carla meets Paul at an ornithology 
          conference, tempers fly as feathers get ruffled.</description>
       </book>
       <book id="bk107">
          <author>Thurman, Paula</author>
          <title>Splish Splash</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-11-02</publish_date>
          <description>A deep sea diver finds true love twenty 
          thousand leagues beneath the sea.</description>
       </book>
       <book id="bk108">
          <author>Knorr, Stefan</author>
          <title>Creepy Crawlies</title>
          <genre>Horror</genre>
          <price>4.95</price>
          <publish_date>2000-12-06</publish_date>
          <description>An anthology of horror stories about roaches,
          centipedes, scorpions  and other insects.</description>
       </book>
       <book id="bk109">
          <author>Kress, Peter</author>
          <title>Paradox Lost</title>
          <genre>Science Fiction</genre>
          <price>6.95</price>
          <publish_date>2000-11-02</publish_date> <description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description> </book> <book id="bk110"> <author>O'Brien, Tim</author> <title>Microsoft .NET: The Programming Bible</title> <genre>Computer</genre> <price>36.95</price> <publish_date>2000-12-09</publish_date> <description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description> </book> <book id="bk111"> <author>O'Brien, Tim</author> <title>MSXML3: A Comprehensive Guide</title> <genre>Computer</genre> <price>36.95</price> <publish_date>2000-12-01</publish_date> <description>The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.</description> </book> <book id="bk112"> <author>Galos, Mike</author> <title>Visual Studio 7: A Comprehensive Guide</title> <genre>Computer</genre> <price>49.95</price> <publish_date>2001-04-16</publish_date> <description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description> </book> </catalog>

     

     

    Here in the above sample there are multiple books. Thats the confusion for many users.

     

    • In order to get autor name of book whose id is 'bk101', the xpath is:

     

    //book[@id="bk101"]/author/text() 
    ----------------------------------
    output will be  - Gambardella, Matthew

    The above example is using attribute value(i.e., when id equals to bk101) of book element to make a right query.

     

    • In order to get title of the book whose author name is 'Ralls, Kim', the xpath is:

     

    //book/author[.='Ralls, Kim']/../title/text()
    or
    //book[author='Ralls, Kim']/title/text()
    -----------------------------------------------
    output willbe - Midnight Rain

    The above example is taking sibiling element of title i.e., author name to query the right value of book title. First gets a book with author name and then taking the reference(here using /../) to get the title.

     

    • Some times, soapUI may not accept if something returns more values, for example see this

     

    //book[author='Corets, Eva']/price/text()
    ----------------------------------------
    Output will be -
     1. 5.95
     2. 5.95
     3. 5.95

     

    Because, there are 3 books authored by Corets, Eva, so it does not know which book. In that case, query should be tuned so that it retuns exact result.

     

    • Change the above one by logical AND - Give the price of the book authored by Corets, Eva and title is 'The Sundered Grail'. The xpath look like this:

     

    //book[author='Corets, Eva' and title='The Sundered Grail']/price/text()
    ---------------------------------------------------------------------------------------
    output will be - 5.95

    If you notice, the above query uses and condion between author and title then get the price.

    Similarly, you may add 'or' condtion.

     

    • Get the title  of the book authored by Thurman, Paula and Published in the year 2000 whose genre is 'Romance'

     

    //book[genre='Romance' and substring(publish_date,0,5)=2000 and author='Thurman, Paula']/title/text()
    -------------------------------------------------
    Output will be - Splish Splash

     

     

    You can see how the xpaths are used without using index values(i.e, //bok[1]/author etc). This type of xpaths will help even xml element order is not fixed, your assertions should always work.

     

    You may search online about xpath queries if you are interested more. By the way, you can test above online as well like http://www.freeformatter.com/xpath-tester.html