Validating Web Service response with XPath
The easiest way for checking web service response in HP LoadRunner is by using XPath query language. LR API contains few functions designed especially for dealing with XML.
-
lr_xml_get_values() //Retrieves values of XML elements found by a query
-
lr_xml_set_values() //Sets the values of XML elements found by a query
-
lr_xml_extract() //Extracts XML string fragments from an XML string
-
lr_xml_delete() //Deletes fragments from an XML string
-
lr_xml_replace() //Replaces fragments of an XML string
-
lr_xml_insert() //Inserts a new XML fragment into an XML string
-
lr_xml_find() //Verifies that XML values are returned by a query
-
lr_xml_transform() //Applies Extensible Stylesheet Language (XSL) Transformation to XML data
Now, lets say we have sample web service for on-line book store and we want to ask what is the author for book id 123. Our web service can send following XML as a response:
-
<books>
-
<book>
-
<id>123</id>
-
<author>John Smith</author>
-
<title>Working with Legacy code</title>
-
<publisher>Microsoft</publisher>
-
</book>
-
</books>
For checking if the “author” element within XML response contains “John Smith” value we will use lr_xml_get_values() function. Here is the code that calls web service and checks if the value is as expected:
-
Action()
-
{
-
web_add_header("SOAPAction", "\"CallMe\"");
-
lr_start_transaction("AUTHOR");
-
soap_request("StepName=Sample Soap Request",
-
"ExpectedResponse=ANY",
-
"URL=http://foo.com/api",
-
"SOAPEnvelope= "
-
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
-
"<soap:Envelope "
-
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" >"
-
"<soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
-
"<GetBookDetails>"
-
"<idValue>123</idValue>"
-
"</GetBookDetails>"
-
"</soap:Body>"
-
"</soap:Envelope>",
-
"Snapshot=t765765765.inf",
-
"ResponseParam=Response_Xml",LAST);
-
-
-
lr_xml_get_values("XML={Response_Xml}",
-
"ValueParam=Author_Name",
-
"Query=/books/book/author",
-
LAST);
-
-
lr_output_message(lr_eval_string("Author is = {Author_Name}"));
-
-
if(strcmp(lr_eval_string("{Author_Name}"),"John Smith") == 0)
-
{
-
lr_end_transaction("AUTHOR", LR_PASS);
-
}
-
else
-
{
-
lr_end_transaction("AUTHOR", LR_FAIL);
-
}
-
return 0;
-
}
First argument in lr_xml_get_values() call is parameter name that holds response XML. Second argument in name of new parameter that will hold author value extracted from response XML. Third argument is XPath query that extracts author element value.

I agree that XPath is great, and that any performance tester who works with XML should take the time to learn it.
With your code example, you might want to clean up you code a little.
Here are some things to fix:
* Your transaction should wrap around your SOAP request, not just your verification. You want to measure the response time of your application, not how long it takes to verify the response.
* Your soap_request is missing “ResponseParam=Author_Name”, so the parameter is never set. Your if statement will always evaluate to \"false\", so your transaction will always fail.
* You should not be verifying with lr_xml_get_values and strcmp, you should use lr_xml_find if you already know what value the element should be.
* As your response body is very simple, you could also have done your verification using web_reg_find.
Cheers,
Stu.
Hi Stuart
First of all I can’t imagine performance test that doesn’t verify output returned by the server. You could have like 10 ms response but it doesn’t matter if you have errors only
1) Yes, you are right. I updated the code.
2) I believe not. lr_xml_get_values() call will create “Author_Name” parameter at run-time with value from XML.
3) I can agree that strcmp() is highly prohibited in C programming in general. Maybe lr_xml_find() would be a good example as well. I will add another sample.
4) The idea was to show a little bit of XPath