本文主要是介绍使用QTP进行非GUI的自动化测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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测试
利用windows的WinHTTP所提供的接口,可以访问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
这篇关于使用QTP进行非GUI的自动化测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!