Qtp allows simple monkey testing on Web Applications
For example, you can traverse every link on the home page, select a link each time, click the link, return to the home page, select the next link, and so on. During the test, each action is written to the test log.
The following code is taken from codesamplesplus of qtp and some necessary modifications are made:
'Start IE browser
Systemutil. Run "ipolice.exe"
'Save the report filter mode
Oldfilter = Reporter. Filter
Reporter. Filter = 2' enables errors only
'Link Description
Set DESC = description. Create ()
DESC ("HTML Tag"). value = ""
DESC ("href"). value = "http://blog.csdn.net/testing_is_believing/category /.*"
Set browserobj = browser ("creationtime: = 0 ")
'Navigate to the specified page
Browserobj. navigate "http://blog.csdn.net/Testing_is_believing/category/357781.aspx"
Set pageobj = browserobj. Page ("index: = 0 ")
'Start monkey testing"
Call enumerateapp (pageobj, DESC, "click", "reportpage", "browserback ")
Reporter. Filter = oldfilter 'returns the original Filter
'Traverse the entire program and perform the specified operation. For example, click each link.
Function enumerateapp (parentobj, DESC, operationmethod, postoperationmethod, restoremethod)
Dim objcol, currentobj, idx
Idx = 0
'Retrieve a collection of all the objects of the given descrition get all sub-objects according to the specified description
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 to perform the specified operation, such as click
Eval ("currentobj." & operationmethod)
'Form the post operations (after the object operation) the action to be performed after the specified operation is completed, such as writing test logs
Eval (postoperationmethod & "(parentobj, currentobj )")
'Return the application to the original state to bring the program back to the initial state, for example, let the browser navigate back to the home page
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
'Write test log
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
'Press the return key on the browser.
Function browserback (parentobj, currentobj)
On Error resume next
Browserobj. Back
End Function