Sunday, 17 May 2015

Testing Web Services With a Standard Web Vuser

It is possible to test web services using the standard Web (HTTP/HTML) virtual user type instead of the Web Services vuser type. The main disadvantage of this is that you cannot generate your SOAP body from the WSDL file using the VuGen wizard. But if you know what your XML request should look like, then you shouldn’t have any real problems.
Here are my tips:
  • Send your SOAP payload using lr_custom_request().
  • Add a SOAPAction HTTP header using web_add_header().
  • Remove unnecessary HTTP headers (that are generated automatically by VuGen) with web_remove_auto_header().
  • Don’t forget to verify that you get a valid response. Use web_reg_find() for a simple check. For better verification of the SOAP response use lr_xml_find().
  • To extract values from the response body, use lr_xml_get_values(). Brush up on your XPath qeries beforehand though.
  • It may be necessary to HTML-encode some characters in your XML/SOAP message (e.g. convert “&” to “&”). Unfortunately VuGen does not provide this functionality (but HP could easily add it to the web_convert_param function), so you will have to either write (or find) a function to do it, or convert all the entries in your data table before running the script.
As an example, here is a simple script that makes use of a web service that will look up the source of a Shakespeare quote for you. The WSDL is available from http://www.xmlme.com/WSShakespeare.asmx?wsdl.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Action()
{
  // ContentCheck Rules for known error messages
  web_global_verification("Text=Speech not found", "ID=SpeechNotFound", LAST);
 
  lr_start_transaction ("Search For Shakespeare Quote");
 
  // By default, VuGen sends a user-agent header.
  // Let's remove this as an example of removing automatically generated headers.
  web_remove_auto_header("User-Agent", "ImplicitGen=No", LAST); 
 
  // Add a SOAPAction HTTP header
  web_add_header("SOAPAction", "http://xmlme.com/WebServices/GetSpeech");
 
  // Save entire body from the HTTP response for later checking with lr_xml_find.
  web_reg_save_param("ResponseBody",
    "LB=",
    "RB=",
    "Search=Body",
    "IgnoreRedirections=Yes",
    LAST);
 
  // Note that the text to search for would normally be replaced with a parameter,
  // and so would the <Request> element of the below SOAP message.
  web_reg_find("Text=TWELFTH NIGHT", LAST);
 
  web_custom_request("Search Shakespeare", 
    "URL=http://www.xmlme.com/WSShakespeare.asmx", 
    "Method=POST", 
    "Resource=0", 
    "Referer=", 
    "Snapshot=t1.inf", 
    "Mode=URL", 
    "EncType=text/xml; charset=utf-8", 
    "Body=" // As it is SOAP, you are unlikely to have to use BodyBinary, unless your request has CDATA.
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/03/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
    "<soap:Body>"
    "<GetSpeech xmlns=\"http://xmlme.com/WebServices\">"
    "<Request>Be not afraid of greatness</Request>"
    "</GetSpeech>"
    "</soap:Body>"
    "</soap:Envelope>",
    LAST);
 
  // The response from the web service looks like this:
  /*
  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <GetSpeechResponse xmlns="http://xmlme.com/WebServices">
        <GetSpeechResult>
          &lt;SPEECH&gt;
          &lt;PLAY&gt;TWELFTH NIGHT&lt;/PLAY&gt;
          &lt;SPEAKER&gt;MALVOLIO&lt;/SPEAKER&gt;
          'Be not afraid of greatness:' 'twas well writ.&lt;/SPEECH&gt;
        </GetSpeechResult>
      </GetSpeechResponse>
    </soap:Body>
  </soap:Envelope>
  */
 
  // An example of extracting the a value from a SOAP reponse.
  // This saves the <GetSpeechResult> element into {OutputParameter}.
  // The same syntax could be used with lr_xml_find to check the response.
  lr_xml_extract("XML={ResponseBody}", 
    "XMLFragmentParam=OutputParameter", 
    "Query=/soap:Envelope/soap:Body/GetSpeechResponse/GetSpeechResult", LAST);
  lr_output_message("Source of Shakespeare quote: %s", lr_eval_string("{OutputParameter}"));
 
  lr_end_transaction ("Search For Shakespeare Quote", LR_AUTO);
 
  return 0;
}

No comments:

Post a Comment