专注于软件性能测试与自动化测试的学习与应用
Validating Web Service response with XPath
上一篇 /
下一篇 2011-06-11 10:44:15
/ 个人分类:LoadRunner
************************本文非原创 **************
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);
}
return0;
}
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.
收藏
举报
TAG:
LoadRunner
loadrunner
Loadrunner
webservice
WebService
xpath