专注于软件性能测试与自动化测试的学习与应用

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.

  1. lr_xml_get_values() //Retrieves values of XML elements found by a query
  2. lr_xml_set_values() //Sets the values of XML elements found by a query
  3. lr_xml_extract() //Extracts XML string fragments from an XML string
  4. lr_xml_delete() //Deletes fragments from an XML string
  5. lr_xml_replace() //Replaces fragments of an XML string
  6. lr_xml_insert() //Inserts a new XML fragment into an XML string
  7. lr_xml_find() //Verifies that XML values are returned by a query
  8. 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:

  1. <books>
  2.   <book>
  3.       <id>123</id>
  4.       <author>John Smith</author>
  5.       <title>Working with Legacy code</title>
  6.       <publisher>Microsoft</publisher>
  7.   </book>
  8. </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:

  1. Action()
  2. {
  3.      web_add_header("SOAPAction","\"CallMe\"");
  4.      lr_start_transaction("AUTHOR");
  5.      soap_request("StepName=Sample Soap Request",
  6.        "ExpectedResponse=ANY",
  7.        "URL=http://foo.com/api",
  8.        "SOAPEnvelope= "
  9.        "<?xml version=\"1.0\"encoding=\"utf-8\"?>"
  10.        "<soap:Envelope "
  11.        "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
  12.        "<soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  13.        "<GetBookDetails>"
  14.        "<idValue>123</idValue>"
  15.        "</GetBookDetails>"
  16.        "</soap:Body>"
  17.        "</soap:Envelope>",
  18.        "Snapshot=t765765765.inf",
  19.        "ResponseParam=Response_Xml",LAST);
  20.  
  21.  
  22.      lr_xml_get_values("XML={Response_Xml}",
  23.          "ValueParam=Author_Name",
  24.          "Query=/books/book/author",
  25.           LAST);
  26.  
  27.      lr_output_message(lr_eval_string("Author is = {Author_Name}"));
  28.  
  29.      if(strcmp(lr_eval_string("{Author_Name}"),"John Smith")==0)
  30.      {
  31.         lr_end_transaction("AUTHOR", LR_PASS);
  32.      }
  33.      else
  34.      {
  35.         lr_end_transaction("AUTHOR", LR_FAIL);
  36.      }
  37.      return0;
  38. }

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

 

评分:0

我来说两句

cow

cow

个人主要专注于性能测试与自动化测试方向的学习与应用

日历

« 2024-04-23  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 12843
  • 日志数: 13
  • 建立时间: 2011-03-08
  • 更新时间: 2011-06-12

RSS订阅

Open Toolbar