Yes, you got it right testing Web services can be done without using Web services add-in. All this is possible with the help of WinHTTP (Microsoft Windows HTTP service), which provides a high-level interface to the HTTP internet protocol.

We can use the com object WinHTTPRequest in our tests in order to invoke any operation to a web service. We will need to post the SOAP request to the web server using methods and properties of WinHTTPRequest and we can get the corresponding response from the service.

Let us now try this out, open QTP and make sure you have not enabled Web Services Add-in in Add-in manager at startup.To retrieve the COM object of WinHTTP we use CreateObject method

Dim oWinHttp
Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")

Now you can access the different properties and methods of WinHTTPRequest. I have explained some of them which we will need to use here.

Object.Open Method,URL,Async
Open – opens a connection to an HTTP resource.
Method – specifies the HTTP verb used for the open method, like ‘GET’,’PUT’,’POST’ etc
URL – the name of the resource
Async – indicates whether to open in asynchronous mode.

Object.SetRequestHeader Header,Value
SetRequestHeader – adds, changes, or deletes an HTTP request header
Header Specifies the name of the header to be set like depth,Content type, Content length etc.
Value specified the value of header.

Object.Send Body
Send – sends an HTTP request to an HTTP server.
Body – is the data to be sent to the server.

We will use one property ‘ResponseText’ here which retrieves the responses entity body as text. Now as we are now aware of these methods, we can use this in our script. Below example script illustrate the accessing of web services using WinHTTP.

Option Explicit
Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
Dim oWinHttp
Dim sResponse

'Web Service URL
sWebServiceURL ="http://www.w3schools.com/webservices/tempconvert.asmx"

'Web Service Content Type
sContentType ="text/XML"

'Web Service SOAP Action
sSOAPAction = "http://tempuri.org/CelsiusToFahrenheit"

'Request Body
sSOAPRequest = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" & _
"<soap:Body>" & _
"<CelsiusToFahrenheit xmlns="http://tempuri.org/">" & _
"<Celsius>25</Celsius>" & _
"</CelsiusToFahrenheit>" & _
"</soap:Body>" & _
"</soap:Envelope>"

Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")

'Open HTTP connection
oWinHttp.Open "POST", sWebServiceURL, False

'Setting request headers
oWinHttp.setRequestHeader "Content-Type", sContentType
oWinHttp.setRequestHeader "SOAPAction", sSOAPAction

'Send SOAP request
oWinHttp.Send sSOAPRequest

'Get XML Response
sResponse = oWinHttp.ResponseText

' Close object
Set oWinHttp = Nothing

' Extract result
Dim nPos1, nPos2
nPos1 = InStr(sResponse, "Result>") + 7
nPos2 = InStr(sResponse, "</")
If nPos1 > 7 And nPos2 > 0 Then
sResponse = Mid(sResponse, nPos1, nPos2 - nPos1)
End If

' Return result
msgbox sResponse

This gives you the required expected value

 

QTP Web Services

The same we can do using XMLHTTP also, see the example below which illustrate the accessing of web services using XMLHTTP

Option Explicit
Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
Dim oDom, oXmlHttp
Dim sResponse

sWebServiceURL ="http://www.w3schools.com/webservices/tempconvert.asmx" 'Web Service URL
sContentType ="text/XML" 'Web Service Content Type
sSOAPAction = "http://tempuri.org/CelsiusToFahrenheit" ' Web Service SOAP Action

'Request Body
sSOAPRequest = "<?xml version="1.0" encoding="utf-8"?>" & _
"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" & _
"<soap:Body>" & _
"<CelsiusToFahrenheit xmlns="http://tempuri.org/">" & _
"<Celsius>25</Celsius>" & _
"</CelsiusToFahrenheit>" & _
"</soap:Body>" & _
"</soap:Envelope>"

'Create objects to DOMDocument and XMLHTTP
Set oDom = CreateObject("MSXML2.DOMDocument")
Set oXmlHttp = CreateObject("MSXML2.XMLHTTP")

'Load XML
oDom.async = False
oDom.loadXML sSOAPRequest

'Open the webservice
oXmlHttp.open "POST", sWebServiceURL, False

'Create headings
oXmlHttp.setRequestHeader "Content-Type", sContentType
oXmlHttp.setRequestHeader "SOAPAction", sSOAPAction

'Send XML command
oXmlHttp.send oDom.xml

'Get XML Response
sResponse = oXmlHttp.ResponseText

'Close object
Set oXmlHttp = Nothing

'Extract result
Dim nPos1, nPos2
nPos1 = InStr(sResponse, "Result>") + 7
nPos2 = InStr(sResponse, "</")
If nPos1 > 7 And nPos2 > 0 Then
sResponse = Mid(sResponse, nPos1, nPos2 - nPos1)
End If

'Return result
msgbox sResponse

Welcome new readers: Download the Free eBook!

I am Ankur Jain, the owner of LearnQTP and founder of a software organization. This site is run, maintained and updated by me and my team. Feel free to take a look around, you may want to start with What is QTP? 

You can connect with me on twitter @ajonit. 

Please enter your first name and email address below to instantly download the eBook on Optimizing QTP Scripts. In future, we will make sure you get new tips & tricks on QTP delivered direct to your email box.

You have Successfully Subscribed!

Share This