您的位置:首页 > 编程语言 > Qt开发

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

2010-01-07 22:22 405 查看
利用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息