使用QTP進行非GUI的自動化測試

TIB發表於2010-03-14

QTP是非常優秀的面向GUI的自動化測試工具,由於採用的是VBS作為指令碼開發語言,因此可以利用VBS的優勢進行很多非GUI型別的自動化測試測試。就像Ian Fraser所說的“The true beauty of QTP is that the only limitation is your imagination and ability to code in VBScript.

只要VBS能做的東西,基本上就可以在QTP中應用上。

 

FTP

下面是一個通過Extern呼叫wininet.dll實現ftp上傳下載的例子:

 

Const DEFAULT_FTP_PORT =21

Const SERVICE_FTP = 1

Const OPEN_TYPE_DIRECT = 1

Const FTP_TRANSFER_TYPE_ASCII = 1

 

Extern.Declare micLong,"InternetOpen","wininet.dll","InternetOpenA",micString,micDWord,micString,micString,micDWord

Extern.Declare micLong,"InternetConnect","wininet.dll","InternetConnectA",micLong,micString,micInteger,micString,micString,micDWord,micDWord,micDWord

Extern.Declare micInteger,"FtpGetFile","wininet.dll","FtpGetFileA",micLong,micString,micString,micInteger,micDWord,micDWord,micDWord

Extern.Declare micInteger,"FtpPutFile","wininet.dll","FtpPutFileA",micLong,micString,micString,micDWord

Extern.Declare micInteger,"InternetCloseHandle","wininet.dll","InternetCloseHandle",micLong

 

'  開啟

hInternet = Extern.InternetOpen("QTP_FTP",OPEN_TYPE_DIRECT,vbNullChar,vbNullChar,0)

If hInternet=0 Then Print("QTP_FTP:Failed to setup FTP environment.")

 

' 連線

hConnection = Extern.InternetConnect(hInternet,"192.168.1.2",DEFAULT_FTP_PORT,"user1","user1",1,0,0)

If hConnection =0 Then Print("Failed to setup FTP environment")

 

' 上傳

' bRetval = Extern.FtpPutFile(hConnection,sLocalFile,sRemoteFile,0)

 

' 下載

bRetval = Extern.FtpGetFile(hConnection,"/Mercury/QuickTest/QTP crack.rar","D:/QTP crack.rar",0,0,1,0)

If Not CBool(bRetVal) Then

       Reporter.ReportEvent micFail,"FTP:FtpGetFile function","Failed to open download file."

else

       Print("FTP:File :QTP crack.rar downloaded successfully")

       'Reporter.ReportEvent micPass,"FTP:FtpPutFile function","FTP:File"&sLocalFile&"upload successfully."

End If

 

' 關閉

Extern.InternetCloseHandle(hConnection)

Extern.InternetCloseHandle(hInternet)

 

 

Linux命令列測試

利用W3Socket 這個dll所提供的API,可以在QTP中訪問linux命令列,執行linux命令並獲取命令列返回

 

W3Socket API參考:

http://www.dimac.net/Products/FreeProducts/w3Sockets/Reference/Refstart.htm

 

 

WebService測試

 

利用windowsWinHTTP所提供的介面,可以訪問HTTP頁面、WebService,獲取返回的頁面資料,例如:

 

' WinHttp.WinHttpRequest.5.1 Object

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

oWinHttpReq.Open "POST", sWebService_Url, False

oWinHttpReq.setRequestHeader "Content-Type", sContentType

oWinHttpReq.setRequestHeader "SOAPAction", sSOAPAction

 

'Send SOAP request

oWinHttpReq.Send  sSOAP_Request

 

'Get XML Response

sXMLResponse = oWinHttpReq.ResponseText

 

 

 

 

 

再結合Msxml.dll所提供的Msxml2.DOMDocument介面,可以對WebService返回的xml資料進行解析:

'Msxml2.DOMDocument

Set oInputXmlDoc = CreateObject("Msxml2.DOMDocument")

  'loadXML from file

oInputXmlDoc.load("C:/XML_Files_Demo/Weight_Conversion.xml")

 

 

 

 

參考:《QTP: Not just for GUI anymore

www.connectedtesting.com

 

相關文章