如果有人可以为此提出解决方案,我将不胜感激。
我有一个简单的函数,期望浏览器在包含 Web 列表的页面上打开,该列表的每个值都代表一个帐户。选择帐户后,将显示其产品(如果有)。
该函数的目标是检索包含产品的帐户的索引(第一个找到的),如果没有,则为 -1。
问题,我不知道是什么原因造成的,当我调试它时,该函数将返回正确的结果——这意味着使用 F10 逐步运行代码,但如果我会返回错误的结果将定期运行 (F5)。这种行为是一致的,并且该函数每次为每种类型的运行检索相同的结果,这意味着它不是一个错误,它只是使函数返回一个随机答案。
这是功能:
' @return: a random account index with products if one exists
' otherwise returns -1
Public Function getRandomAccountWithProducts()
On Error Resume Next
Set Page1 = Browser("micclass:=browser").Page("micclass:=Page")
Set br = Browser("micclass:=Browser")
originalURL = br.GetROProperty("URL")
br.Navigate Environment.Value("SOME URL") & "REST OF URL"
br.Sync
Page1.WebList("name:=accountId").Select "#1"
br.Sync
' Display only products
Page1.WebRadioGroup("name:=name0").Click
Page1.WebList("name:=name1").Select "Display None"
Page1.WebList("name:=name2").Select "Display None"
Page1.WebButton("value:=Apply","visible:=True").Click
' Init
numOfAccounts = Page1.WebList("name:=accountId").GetROProperty("items count") - 1
If numOfAccounts < 1 Then
getRandomAccountWithProducts = -1
Reporter.ReportEvent micFail, "Number of accounts","There are no accounts. No account with products exists"
Exit Function
End If
hasProducts = false
accountIndex = 1
' Get account with products
While ((Not hasProducts) AND (accountIndex =< numOfAccounts))
' Return account if has products
If Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) Then
hasProducts = true
End If
If (Not hasProducts) Then
accountIndex = accountIndex + 1
Page1.WebList("name:=accountId").Select "#" & accountIndex
End If
Wend
br.Navigate originalURL
Set Page1= Nothing
Set br = Nothing
' If no account has products, report and exit, else return selected account index
If Not hasProducts Then
Reporter.ReportEvent micFail,"Accounts","No account has products."
getRandomAccountWithProducts = -1
Else
getRandomAccountWithProducts = accountIndex
End If
If Err<>0 Then
errorMessage = "Error number: " & Err.Number & vbNewLine & "Error description: " & Err.Description & vbNewLine & "Error source: " & Err.Source
Reporter.ReportEvent micFail,"Run Time Error",errorMessage
Err.Clear
End If
On Error GoTo 0
End Function
我在 Pentium 4、3.2 GHZ、2 GB RAM、Win XP、SP 3、IE 7、QTP 10.0 Build 513 上运行
谢谢!