利用QTP在WEB应用程序上进行简单的猴子测试
更新时间:2022-05-23 09:33:54 作者:多测师 浏览:174
利用QTP可以在WEB应用程序上进行简单的猴子测试 ,例如遍历主页面中的每一个链接,每次选择一个链接进行点击操作,然后回退到主页面,再选择下一个链接进行点击,如此类推。在测试过程中,把每个动作写入测试日志中。
下面代码摘自QTP的CodeSamplesPlus并做了一些必要的修改:
' 启动IE浏览器
SystemUtil.Run "iexplore.exe"
' save the Report Filter mode
OldFilter = Reporter.Filter
Reporter.Filter = 2 ' Enables Errors Only
' 链接描述
Set Desc = Description.Create()
Desc("html tag").Value = "A"
Desc("href").Value = "http://blog.csdn.net/Testing_is_believing/category/.*"
Set BrowserObj = Browser("creationtime:=0")
' 导航到指定页面
BrowserObj.Navigate "http://blog.csdn.net/Testing_is_believing/category/357781.aspx"
Set PageObj = BrowserObj.Page("index:=0")
' 开始“猴子测试”
call EnumerateApp(PageObj, Desc, "Click", "ReportPage", "BrowserBack")
Reporter.Filter = OldFilter ' returns the original filter
' 遍历整个程序,执行指定的操作,例如点击每个链接
Function EnumerateApp(ParentObj, Desc, OperationMethod, PostOperationMethod, RestoreMethod)
dim ObjCol, CurrentObj, idx
idx = 0
' retrieve a collection of all the objects of the given descrition 按指定的描述取得所有子对象
Set ObjCol = ParentObj.ChildObjects(Desc)
Do While (idx < ObjCol.Count)
' get the current object
set CurrentObj = ObjCol.item(idx)
' perform the desired operation on the object 执行指定的操作,例如Click
eval("CurrentObj." & OperationMethod)
' perform the post operations (after the object operation) 执行完指定的操作后需要做的动作,例如写入测试日志
eval(PostOperationMethod & "(ParentObj, CurrentObj)")
' Return the application to the original state 让程序回到初始状态,例如让浏览器导航回退到主页面
eval(RestoreMethod & "(ParentObj, CurrentObj)")
idx = idx + 1
' reretrieve the collection of objects
' (as the application might have changed)
Set ObjCol = ParentObj.ChildObjects(Desc)
Loop
End Function
' 写测试日志
Function ReportPage(ParentObj, CurrentObj)
dim FuncFilter, PageTitle
PageTitle = ParentObj.GetROProperty("title")
FuncFilter = Reporter.Filter
Reporter.Filter = 0
Reporter.ReportEvent 0, "Page Information", "page title " & PageTitle
Reporter.Filter = FuncFilter
End Function
' 让浏览器按回退键
Function BrowserBack(ParentObj, CurrentObj)
On Error Resume Next
BrowserObj.Back
End Function
以上内容为大家介绍了自动化测试中的利用QTP在WEB应用程序上进行简单的猴子测试,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/