Web Services and QTP- Part4: Testing without Web Services Add-in
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 ofWinHTTPRequest 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
oWinHttpSet
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.
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.
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.
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
ExplicitDim
sWebServiceURL,
sContentType, sSOAPAction, sSOAPRequestDim
oWinHttpDim
sResponse'Web Service URL'Web Service Content TypesContentType ="text/XML"'Web Service SOAP Action'Request BodysSOAPRequest = "<?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>"
&
_"<Celsius>25</Celsius>"
&
_"</CelsiusToFahrenheit>"
&
_"</soap:Body>"
&
_"</soap:Envelope>"Set
oWinHttp
= CreateObject("WinHttp.WinHttpRequest.5.1")'Open HTTP connectionoWinHttp.Open "POST",
sWebServiceURL, False'Setting request headersoWinHttp.setRequestHeader
"Content-Type", sContentTypeoWinHttp.setRequestHeader
"SOAPAction", sSOAPAction'Send SOAP requestoWinHttp.Send sSOAPRequest'Get XML ResponsesResponse = oWinHttp.ResponseText' Close objectSet
oWinHttp
= Nothing' Extract resultDim
nPos1,
nPos2nPos1 = InStr(sResponse,
"Result>") + 7nPos2 = InStr(sResponse,
"</")If nPos1
> 7 And nPos2
> 0 Then sResponse =
Mid(sResponse, nPos1, nPos2 - nPos1)End
If' Return resultmsgbox 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
ExplicitDim
sWebServiceURL,
sContentType, sSOAPAction, sSOAPRequestDim
oDom,
oXmlHttpDim
sResponsesContentType ="text/XML"
'Web
Service Content Type'Request BodysSOAPRequest = "<?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>"
&
_"<Celsius>25</Celsius>"
&
_"</CelsiusToFahrenheit>"
&
_"</soap:Body>"
&
_"</soap:Envelope>"'Create objects to DOMDocument and
XMLHTTPSet
oDom
= CreateObject("MSXML2.DOMDocument")Set
oXmlHttp
= CreateObject("MSXML2.XMLHTTP")'Load XMLoDom.async = FalseoDom.loadXML sSOAPRequest'Open the webservice oXmlHttp.open "POST",
sWebServiceURL, False'Create headingsoXmlHttp.setRequestHeader
"Content-Type", sContentTypeoXmlHttp.setRequestHeader
"SOAPAction", sSOAPAction'Send XML commandoXmlHttp.send oDom.xml'Get XML ResponsesResponse = oXmlHttp.ResponseText'Close objectSet
oXmlHttp
= Nothing'Extract resultDim
nPos1,
nPos2nPos1 = InStr(sResponse,
"Result>") + 7nPos2 = InStr(sResponse,
"</")If nPos1
> 7 And nPos2
> 0 Then sResponse = Mid(sResponse, nPos1,
nPos2 - nPos1)End
If'Return resultmsgbox sResponse |
|
Option
ExplicitDim
sWebServiceURL,
sContentType, sSOAPAction, sSOAPRequestDim
oWinHttpDim
sResponsesContentType ="text/XML"
'Web
Service Content Type'Request BodysSOAPRequest = "<?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>"
&
_"<Celsius>25</Celsius>"
&
_"</CelsiusToFahrenheit>"
&
_"</soap:Body>"
&
_"</soap:Envelope>"Set
oWinHttp
= CreateObject("WinHttp.WinHttpRequest.5.1")'Open HTTP connectionoWinHttp.Open "POST",
sWebServiceURL, False'Setting request headersoWinHttp.setRequestHeader
"Content-Type", sContentTypeoWinHttp.setRequestHeader
"SOAPAction", sSOAPAction'Send SOAP requestoWinHttp.Send sSOAPRequest'Get XML ResponsesResponse = oWinHttp.ResponseText' Close objectSet
oWinHttp
= Nothing' Extract resultDim
nPos1,
nPos2nPos1 = InStr(sResponse,
"Result>") + 7nPos2 = InStr(sResponse,
"</")If nPos1
> 7 And nPos2
> 0 ThensResponse = Mid(sResponse, nPos1, nPos2
- nPos1)End
If' Return resultmsgbox sResponse |
|
|
|
No comments:
Post a Comment