利用QTP在WEB应用程序上进行简单的猴子测试

发表于:2010-1-08 13:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:陈能技(CSDNBlog)    来源:51Testing软件测试网采编

  利用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

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号