Testing web services with TestComplete is obsolete. We recommend that you use ReadyAPI, another SmartBear's automated testing tool for SOAP and REST web services. If needed, you can run ReadyAPI tests from TestComplete. |
Usually, to perform functional testing of a web service with TestComplete, you add your tested web service to the WebServices collection in your TestComplete project and call the needed service’s methods from your tests directly (see Creating Web Service Tests). However, sometimes, you may need to submit custom requests to your tested web service. This topic describes how you can create a custom SOAP request in TestComplete scripts and send the request to your tested web service.
About the SOAP Request Format
Web services use the SOAP protocol to transmit data in the XML format to web service methods and to return the results to the caller.
According to the SOAP protocol, XML data transmitted to a web service as a request contains a root Envelope
element that includes:
-
The required
Body
element (the SOAP body part). Method parameters are transferred in the request’s SOAP body. -
An optional
Header
element (the SOAP header part). SOAP headers can contain some additional information transmitted to the web service. For instance, SOAP headers may be used for passing authentication credentials to the web service.
For more information on the format of SOAP requests, see the Understanding SOAP article in the MSDN Library.
How to Prepare SOAP Requests in Scripts
In TestComplete, to create a custom SOAP request, you can use the WebServiceInfo.PrepareRequestObject
method. Use the object the method returns to specify the parameters to be passed to your tested web service. Then use the WebServiceInfo.PrepareRequest
method to create an XML document with the specified parameters in the SOAP body. This way, you do not need to manually create and edit the XML data to be sent to your server.
Note: However, the generated SOAP request will not contain any headers. To add a header to your request, you will have to create the appropriate XML elements and add them to the request manually.
The instructions below explain how to create a custom SOAP request, add headers to it, and then send the request to the tested web service:
-
If your TestComplete project does not have the WebServices collection, add it. See Creating Web Service Tests.
-
(Optional.) Add your tested web service to the WebServices collection. See Creating Web Service Tests.
-
In your script, create a
WebServiceInfo
object that will provide access to your tested web service methods and data types.If your tested web service is added to your project’s WebServices collection, use the
WebServices.CreateWebServiceInfoFromItem
method:JavaScript
let serviceInfo = WebServices.CreateWebServiceInfoFromItem("SampleWebService");JScript
var serviceInfo = WebServices.CreateWebServiceInfoFromItem("SampleWebService");Python
serviceInfo = WebServices.CreateWebServiceInfoFromItem("SampleWebService")VBScript
Set serviceInfo = WebServices.CreateWebServiceInfoFromItem("SampleWebService")DelphiScript
var serviceInfo;
…
serviceInfo := WebServices.CreateWebServiceInfoFromItem('SampleWebService');C#Script
var serviceInfo = WebServices["CreateWebServiceInfoFromItem"]("SampleWebService");Otherwise, use the
WebServices.CreateWebServiceInfo
method:JavaScript
let URL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx?WSDL";
let serviceName = "SampleWebService";
let soapVersion = WebServices.svSoap11;
let serviceInfo = WebServices.CreateWebServiceInfo(URL, serviceName, soapVersion);JScript
var URL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx?WSDL";
var serviceName = "SampleWebService";
var soapVersion = WebServices.svSoap11;
var serviceInfo = WebServices.CreateWebServiceInfo(URL, serviceName, soapVersion);Python
URL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx?WSDL"
serviceName = "SampleWebService"
soapVersion = WebServices.svSoap11
serviceInfo = WebServices.CreateWebServiceInfo(URL, serviceName, soapVersion)VBScript
URL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx?WSDL"
serviceName = "SampleWebService"
soapVersion = WebServices.svSoap11
Set serviceInfo = WebServices.CreateWebServiceInfo(URL, serviceName, soapVersion)DelphiScript
var URL, serviceName, soapVersion, serviceInfo;
…
URL := 'http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx?WSDL';
serviceName := 'SampleWebService';
soapVersion := WebServices.svSoap11;
serviceInfo := WebServices.CreateWebServiceInfo(URL, serviceName, soapVersion);C#Script
var URL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx?WSDL";
var serviceName = "SampleWebService";
var soapVersion = WebServices["svSoap11"];
var serviceInfo = WebServices["CreateWebServiceInfo"](URL, serviceName, soapVersion); -
Use the
WebServiceInfo.PrepareRequestObject
to create a helper object that will store the parameters to be passed to a web service method:JavaScript
let methodName = "SetSampleObject";
let requestObj = serviceInfo.PrepareRequestObject(methodName);JScript
var methodName = "SetSampleObject";
var requestObj = serviceInfo.PrepareRequestObject(methodName);Python
methodName = "SetSampleObject"
requestObj = serviceInfo.PrepareRequestObject(methodName)VBScript
methodName = "SetSampleObject"
Set requestObj = serviceInfo.PrepareRequestObject(methodName)DelphiScript
var methodName, requestObj;
…
methodName := 'SetSampleObject';
requestObj := serviceInfo.PrepareRequestObject(methodName);C#Script
var methodName = "SetSampleObject";
var requestObj = serviceInfo["PrepareRequestObject"](methodName); -
The properties of the created helper object correspond to the tested web service method’s parameters. Assign the values you want to pass to your tested web service method to the object properties:
JavaScript
// The method takes the obj parameter that is an object of the SampleTestClass
requestObj.obj = serviceInfo.TypeFactory.SampleTestClass;
…JScript
// The method takes the obj parameter that is an object of the SampleTestClass
requestObj.obj = serviceInfo.TypeFactory.SampleTestClass;
…Python
# The method takes the obj parameter that is an object of the SampleTestClass
requestObj.obj = serviceInfo.TypeFactory.SampleTestClass
…VBScript
' The method takes the obj parameter that is an object of the SampleTestClass
Set requestObj.obj = serviceInfo.TypeFactory.SampleTestClass
…DelphiScript
// The method takes the obj parameter that is an object of the SampleTestClass
requestObj.obj := serviceInfo.TypeFactory.SampleTestClass;
…C#Script
// The method takes the obj parameter that is an object of the SampleTestClass
requestObj["obj"] = serviceInfo["TypeFactory"]["SampleTestClass"];
… -
Use the
WebServiceInfo.PrepareRequest
method to generate an XML document from the helper object:JavaScript
let requestXml = serviceInfo.PrepareRequest(methodName, requestObj);JScript
var requestXml = serviceInfo.PrepareRequest(methodName, requestObj);Python
requestXml = serviceInfo.PrepareRequest(methodName, requestObj)VBScript
Set requestXml = serviceInfo.PrepareRequest(methodName, requestObj)DelphiScript
var requestXml;
…
requestXml := serviceInfo.PrepareRequest(methodName, requestObj);C#Script
var requestXml = serviceInfo["PrepareRequest"](methodName, requestObj);The generated XML document contains a SOAP body with the specified method parameters to be passed to the tested web service:
XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns1="http://smartbear.com">
<ns1:SetSampleObject>
<ns1:obj xsi:type="ns1:SampleTestClass">
…
</ns1:obj>
</ns1:SetSampleObject>
</s:Body>
</s:Envelope>If needed, you can use properties and methods of the
IXMLDOMDocument
interface to modify the content of the created XML document. To learn more, see the description of the IXMLDOMDocument interface in the MSDN Library.For example, you may need this to add a
Header
to your SOAP request:JavaScript
let root = requestXml.documentElement;
let headerElement = requestXml.createElement("s:Header");
let headerNode = root.insertBefore(headerElement, root.childNodes.item(0));JScript
var root = requestXml.documentElement;
var headerElement = requestXml.createElement("s:Header");
var headerNode = root.insertBefore(headerElement, root.childNodes.item(0));Python
root = requestXml.documentElement
headerElement = requestXml.createElement("s:Header")
headerNode = root.insertBefore(headerElement, root.childNodes.item[0])VBScript
Set root = requestXml.documentElement
Set headerElement = requestXml.createElement("s:Header")
Set headerNode = root.insertBefore(headerElement, root.childNodes.item(0))DelphiScript
var root, headerElement, headerNode;
…
root := requestXml.documentElement;
headerElement := requestXml.createElement('s:Header');
headerNode := root.insertBefore(headerElement, root.childNodes.item(0));C#Script
var root = requestXml["documentElement"];
var headerElement = requestXml["createElement"]("s:Header");
var headerNode = root["insertBefore"](headerElement, root["childNodes"]["item"](0));Note: If your tested web service does not require that SOAP requests contain headers, you do not need to add them.
-
Create an
XMLHTTP
object:JavaScript
let xmlHttpRequest = getActiveXObject("MSXML2.XMLHTTP.3.0");JScript
var xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");Python
xmlHttpRequest = Sys.OleObject["MSXML2.XMLHTTP.3.0"]VBScript
Set xmlHttpRequest = CreateObject("MSXML2.XMLHTTP.3.0")DelphiScript
var xmlHttpRequest;
…
xmlHttpRequest := Sys.OleObject('MSXML2.XMLHTTP.3.0');C#Script
xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");The object allows sending XML data to HTTP servers and receiving server responses.
-
Use the
open
andsend
methods of the createdXMLHTTP
object to send the created SOAP request to the tested web service:JavaScript
let serviceURL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx";
xmlHttpRequest.open("POST", serviceURL, false);
xmlHttpRequest.send(requestXml);JScript
var serviceURL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx";
xmlHttpRequest.open("POST", serviceURL, false);
xmlHttpRequest.send(requestXml);Python
serviceURL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx"
xmlHttpRequest.open("POST", serviceURL, False)
xmlHttpRequest.send(requestXml)VBScript
serviceURL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx"
Call xmlHttpRequest.open("POST", serviceURL, False)
xmlHttpRequest.send(requestXml)DelphiScript
var serviceURL;
…
serviceURL := 'http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx';
xmlHttpRequest.open('POST', serviceURL, false);
xmlHttpRequest.send(requestXml);C#Script
var serviceURL = "http://secure.smartbearsoftware.com/samples/testcomplete15/webservices/Service.asmx";
xmlHttpRequest["open"]("POST", serviceURL, false);
xmlHttpRequest["send"](requestXml); -
(Optional.) To get the server response in your script, use the
responseXML
property of theXMLHTTP
object:JavaScript
let responseXML = xmlHttpRequest.responseXML;JScript
var responseXML = xmlHttpRequest.responseXML;Python
responseXML = xmlHttpRequest.responseXMLVBScript
Set responseXML = xmlHttpRequest.responseXMLDelphiScript
var responseXML;
…
responseXML := xmlHttpRequest.responseXML;C#Script
var responseXML = xmlHttpRequest["responseXML"];It returns the response body as an XML document.
You can use the
WebServiceInfo.ParseResponse
method to process the response and get the object whose properties contain the output parameters of a method of the web service:JavaScript
let response = serviceInfo.ParseResponse(methodName, responseXML);
Log.Message("Result Value: " + response.SetSampleObjectResult);JScript
var response = serviceInfo.ParseResponse(methodName, responseXML);
Log.Message("Result Value: " + response.SetSampleObjectResult);Python
response = serviceInfo.ParseResponse(methodName, responseXML)
Log.Message("Result Value: " + response.SetSampleObjectResult)VBScript
Set response = serviceInfo.ParseResponse(methodName, responseXML)
Call Log.Message("Result Value: " & response.SetSampleObjectResult)DelphiScript
var response;
…
response := serviceInfo.ParseResponse(methodName, responseXML);
Log.Message('Result Value: ' + response.SetSampleObjectResult);C#Script
var response = serviceInfo["ParseResponse"](methodName, responseXML);
Log["Message"]("Result Value: " + response["SetSampleObjectResult"]);
You can view the whole sample code that shows how to prepare and send SOAP requests below.
How to Add WCF Authentication Headers to SOAP Requests
If your tested web service uses the WCF technology and requires complex authentication, you cannot create a proper request header from scripts directly. As a workaround, you can do the following:
-
Add your tested web service to the WebServices collection in your project.
-
Add the Events collection to your TestComplete project:
-
Add an Event Control item to the collection and then add a handler for the
OnWebServiceRequest
event:TestComplete will call the handler whenever your test sends a request to the tested web service.
-
The event handler provides access to the content of the SOAP request to be sent and to the method of the tested web service to which the request is being sent. In the event handler, you can:
-
Add a custom SOAP header to the request. See Adding Custom Headers to SOAP Requests.
-
Set up authentication using the
WebServices.Your_WebService_Item_Name.Credentials
property. -
Use the
Request
parameter of the handler to get the request contents and then modify it as needed by using theIXMLDOMDocument
interface. See above.
-
-
Send a request to your tested web service using the appropriate method of the
WebServices.Your_WebService_Item_Name
object.Before sending a request, TestComplete will raise the
OnWebServiceRequest
event handler and process your request.
Example
The code below shows how you can prepare SOAP requests and send them to your tested web service from scripts.
The code sends a SOAP request to the SetSampleObject
method of the tested SampleWebService
web service. The method takes the obj
input parameter that is an object of the SampleTestClass
class. The object has the following properties:
-
X
- Integer. -
Y
- Integer. -
Name
- String. -
IntArray
- Array of integers.
In addition, the code adds authentication credentials to the request’s header: the Credentials
element with the UserID
and Password
child elements. The code shows how to add such a header to the request using the IXMLDOMDocument interface.
See Also
About Testing Web Services
Creating Web Service Tests
WebServiceInfo Object
About Testing Web Services