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

  1. Dim oWinHttp
  2. 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.

  1. Option Explicit
  2. Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
  3. Dim oWinHttp
  4. Dim sResponse
  5.  
  6. 'Web Service URL
  7. sWebServiceURL ="http://www.w3schools.com/webservices/tempconvert.asmx"
  8.  
  9. 'Web Service Content Type
  10. sContentType ="text/XML"
  11.  
  12. 'Web Service SOAP Action
  13. sSOAPAction = "http://tempuri.org/CelsiusToFahrenheit"
  14.  
  15. 'Request Body
  16. sSOAPRequest = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
  17. "<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/">" & _
  18. "<soap:Body>" & _
  19. "<CelsiusToFahrenheit xmlns="http://tempuri.org/">" & _
  20. "<Celsius>25</Celsius>" & _
  21. "</CelsiusToFahrenheit>" & _
  22. "</soap:Body>" & _
  23. "</soap:Envelope>"
  24.  
  25. Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
  26.  
  27. 'Open HTTP connection
  28. oWinHttp.Open "POST", sWebServiceURL, False
  29.  
  30. 'Setting request headers
  31. oWinHttp.setRequestHeader "Content-Type", sContentType
  32. oWinHttp.setRequestHeader "SOAPAction", sSOAPAction
  33.  
  34. 'Send SOAP request
  35. oWinHttp.Send sSOAPRequest
  36.  
  37. 'Get XML Response
  38. sResponse = oWinHttp.ResponseText
  39.  
  40. ' Close object
  41. Set oWinHttp = Nothing
  42.  
  43. ' Extract result
  44. Dim nPos1, nPos2
  45. nPos1 = InStr(sResponse, "Result>") + 7
  46. nPos2 = InStr(sResponse, "</")
  47. If nPos1 > 7 And nPos2 > 0 Then
  48. sResponse = Mid(sResponse, nPos1, nPos2 - nPos1)
  49. End If
  50.  
  51. ' Return result
  52. 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

  1. Option Explicit
  2. Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
  3. Dim oDom, oXmlHttp
  4. Dim sResponse
  5.  
  6. sWebServiceURL ="http://www.w3schools.com/webservices/tempconvert.asmx" 'Web Service URL
  7. sContentType ="text/XML" 'Web Service Content Type
  8. sSOAPAction = "http://tempuri.org/CelsiusToFahrenheit" ' Web Service SOAP Action
  9.  
  10. 'Request Body
  11. sSOAPRequest = "<?xml version="1.0" encoding="utf-8"?>" & _
  12. "<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/">" & _
  13. "<soap:Body>" & _
  14. "<CelsiusToFahrenheit xmlns="http://tempuri.org/">" & _
  15. "<Celsius>25</Celsius>" & _
  16. "</CelsiusToFahrenheit>" & _
  17. "</soap:Body>" & _
  18. "</soap:Envelope>"
  19.  
  20. 'Create objects to DOMDocument and XMLHTTP
  21. Set oDom = CreateObject("MSXML2.DOMDocument")
  22. Set oXmlHttp = CreateObject("MSXML2.XMLHTTP")
  23.  
  24. 'Load XML
  25. oDom.async = False
  26. oDom.loadXML sSOAPRequest
  27.  
  28. 'Open the webservice
  29. oXmlHttp.open "POST", sWebServiceURL, False
  30.  
  31. 'Create headings
  32. oXmlHttp.setRequestHeader "Content-Type", sContentType
  33. oXmlHttp.setRequestHeader "SOAPAction", sSOAPAction
  34.  
  35. 'Send XML command
  36. oXmlHttp.send oDom.xml
  37.  
  38. 'Get XML Response
  39. sResponse = oXmlHttp.ResponseText
  40.  
  41. 'Close object
  42. Set oXmlHttp = Nothing
  43.  
  44. 'Extract result
  45. Dim nPos1, nPos2
  46. nPos1 = InStr(sResponse, "Result>") + 7
  47. nPos2 = InStr(sResponse, "</")
  48. If nPos1 > 7 And nPos2 > 0 Then
  49. sResponse = Mid(sResponse, nPos1, nPos2 - nPos1)
  50. End If
  51.  
  52. 'Return result
  53. msgbox sResponse