想必大家都知道QTP Addin的成本之高,所以如何避免使用付费Addin来测试需求对象是解决成本的最佳方案。
面对WebService,我们可以用到 WinHTTP协议(微软windows HTTP服务),这是一个非常高级别的HTTP协议接口。
以下会具体介绍下这个COM对象。
1.创建WinHttp对象
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.
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
运行以上脚本,最终会返回 77