banner



How To Validate Xml Response From Web Service

All messages received by sampler TestSteps are internally converted to an XML counterpart, which provides a common ground for assertions and other post-processing. This also makes manner for two of the near powerful assertions, XPath Lucifer and XQuery Match, which both utilize the named technologies to provide fine-grained message validation possibilities. Let's accept a look at each together with some examples!

Quick Tip:Both the XPath and XQuery Match assertions make use of the Saxon XPath / XQuery processor which supports most of the latest standards in this expanse. Check out their website to get details on supported standards, reference documentation, etc.

1. The XPath Friction match Assertion

The XPath assertion applies a specified XPath expression to the received bulletin and validates the resulting nodes confronting an expected value. If the values match the assertion passes, otherwise it fails. Let's jump correct in, here is a login response message that we want to validate;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.example.org/sample/">    <soapenv:Header/>    <soapenv:Trunk>       <sam:loginResponse>          <sessionid>10873286937963711</sessionid>       </sam:loginResponse>    </soapenv:Torso> </soapenv:Envelope>        

Basically we want to check for the loginResponse and sessionid elements in the SOAP Trunk, only ignore the actual value of the sessionid since it volition change between each asking.

Start by selecting the "Add together Exclamation" button in the Assertion Toolbar and selecting the "XPath Friction match" assertion in the prompting dialog:

add-xpath-match-assertion

After pressing OK, the post-obit (empty) configuration dialog is opened:

empty-xpath-match-assertion-configuration

The dialog is divided into two areas: the top specifies the XPath expression to apply and the bottom the Expected Result. Side by side step is to specify the XPath expression that selects the loginResponse element;

declare namespace sam='http://www.example.org/sample/';
//sam:loginResponse[i]

Entering this expression into the top edit field and the pressing "Select from electric current" in the toolbar below gives us the following:

xpath-match-initial-configuration

As y'all can meet, the "Select from current" action applied the XPath expression to the underlying response message and shows the consequence in the "Expected Result" field; this is now the value that SoapUI will compare against each time a new response is received and the assertion is executed. If the value doesn't match, the assertion fails.

one.one. Wildcards

Obviously there is a problem here; the sessionid volition be dissimilar each time around which will fail the exclamation, let'southward utilize the wildcard feature to work around this;

xpath-match-wildcard-configuration

To a higher place we take selected the "Allow Wildcards" option and replaced the sessionid value with a '*' which will result in SoapUI ignoring the sessionid value when asserting the result.

If we would want to assert the unabridged response bulletin we could change the configuration to the following:

xpath-assertion-for-whole-message

ane.two. XPath Wizards

In ReadyAPI the above creation of the assertion or just its XPath argument is extremely like shooting fish in a barrel; the top left push in the XPath Lucifer configuration dialog opens a dialog that allows you to select the desired target node for the XPath expression, which will so be created automatically for you. For the in a higher place scenario this looks every bit follows:

xpath-creation-wizard

(Pressing OK in the dialog inserts the selected XPath statement into the editor field)

ReadyAPI also adds a number of wizards to the right-click popup menu of outline view of the response message; correct-clicking the node you want to assert in the outline view opens the following popup:

xpath-assertion-wizard-popup

The get-go iv options here all create preconfigured XPath assertions for you lot, for example selecting the "for Existence" option creates the following XPath Assertion;

for-existance-xpath-assertion

If we instead had selected the "for Content matching RegEx" option, nosotros would commencement accept been prompted for a regular expression that should exist used to validate the selected node;

add-regex-xpath-assertion

hither we specified the ".\d" regular expression which matches just numbers (run into this page for a regex reference), after pressing OK we get

regex-xpath-configuration

2. The XQuery Match Assertion

The XQuery Match exclamation works just like the above described XPath assertions, with the only departure that instead of XPath it uses an XQuery expression to select the XML that should be validated, which has some distinct advantages for circuitous validations;

  • Merely desired nodes tin can be selected and merged into one XML result, making exclamation management much easier
  • Results can be ordered allowing you to create assertions that are non dependent on the ordering of items in an XML message
  • etc.

This calls for an example; lets say you desire to validate the following response message;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/lather/envelope/"> 	    <soapenv:Header/> 	    <soapenv:Body> 	        <sam:searchResponse> 	            <sam:searchResponse> 	                <item> 	                    <id>1</id> 	                    <description>One handy protocol droid</description> 	                    <price>1</toll> 	                </detail> 	                <detail> 	                    <id>2</id> 	                    <description>Item nr two</description> 	                    <toll>two</price> 	                </item> 	                <item> 	                    <id>three</id> 	                    <description>Particular nr 3</clarification> 	                    <price>3</price> 	                </item> 	                <particular> 	                    <id>4</id> 	                    <clarification>Item nr 4</clarification> 	                    <price>iv</price> 	                </item> 	                <item> 	                    <id>5</id> 	                    <description>Item nr 5</clarification> 	                    <cost>5</price> 	                </item> 	            </sam:searchResponse> 	        </sam:searchResponse> 	    </soapenv:Body> 	</soapenv:Envelope>

Now let's say y'all always expect these items merely the order is undefined and you are but interested in validating the price. The following XQuery Match assertion volition do the trick:

xquery-match-configuration

The specified XQuery expression selects all items, orders them by their id, and then simply extracts the price into a temporary xml result. Pressing "Select from current" gives the above response.

Source: https://www.soapui.org/docs/functional-testing/validating-messages/validating-xml-messages/

Posted by: matthewsmande1942.blogspot.com

0 Response to "How To Validate Xml Response From Web Service"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel