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
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
Not able to Run this code Syntax error
“Expected end of statement”
Please help me out..
Thank you.
HI I tried using the same code which you mentioned in the site, but getting following error :
The server name or address could not be resolved
Could you please let me know the solution ….
HI..
I have copy,pasted the same code what u have posted for web-service testing without using Add-ins , but I’m getting a error while executing the code
Error :-
The server name or address could not be resolved…
Could you please help me with the solution
Yes it should work, you should try it.
does QTP 9.2 will support Webservices testing without Add-ins?
I am getting the SOAPException while invoking the web service through the QTP
Error Message : Javax.xml.soap.SOAPException:Error parsing envlope:most likely due to an invalid SOAP message:Unexpected EOF in prolog
Can any help me out plzzzzzz???
Hi Saket,
I am trying to execute the same code as defined above on testing webservice without using Webservice Add-on by following steps:
-> There is no Add-on(webservice) Installed on my machine
-> I have copied and pasted above code “accessing of web services using WinHTTP.”
while running the code through QTP I got an error at
sSOAPRequest = “” & _
saying “Expected end of statement”
so I have changed the “sSOAPRequest ” variable as following :
arg1 = “”
arg2 = “”
arg3 = “”
arg4 = “”
arg5 = “string”
arg6 = “”
arg7 = “”
arg8 = “”
‘Request Body
sSOAPRequest = arg1 & arg2 & arg3 & arg4 & arg5 & arg6 & arg7 & arg8
-> Now I can run the application with out any errors but the result(MsgBox sResponse) is showing blank. I observed the variable “sResponse = oXmlHttp.ResponseText” in Add watch shows as sResponse = “”
means
I am getting null value.
Could you please tell me do I need to add some code in order to get the response(sResponse) value to get the out put in MsgBox.
what is the difference berween parameterisation and data driven testing
Fantastic.. can be used to know what responses you send and recieve..
Anybody tried to read a JMS queue using the QTP? I’m trying to create a script to read the XML that is in this queue, but I’m not having any success. Can anybody help me?
please help on how to configure xml check point.
Thanks
Hi ,
Your approach works for ASP.NET web services. Is there a way I can test WSDL web services.
Any pointers will be of great help.
I cannot see the sample script please repost it
Hi QA User,
Can you please elaborate this, where exactly you get this error?
Hi Prarthana,
what exactly you are looking for in QTP help, winhttprequest related document is not available with QTP. search for web services in help, you will get all the related things there.
I am planning to write a post on automating PDFs, hopefully I will be able to post it soon.
Not sure whether this is the right place question to ask this question or not.
Question: How can we verify the title of pdf file using QTP?
Please Provide me with what should i do when i got Error 401: Missing Authorization Header
Not able to find anything related to WinHTTPRequest in QTP help….Is it available in some specific ver of QTP?