【问题标题】:QTP 10 - A function return deifferent results for same data in run and debug modesQTP 10 - 一个函数在运行和调试模式下为相同的数据返回不同的结果
【发布时间】:2010-06-02 09:13:10
【问题描述】:

如果有人能提出解决方案,我将不胜感激。

我有一个简单的功能,它期望浏览器在包含 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 上运行

谢谢!

【问题讨论】:

    标签: vbscript qtp


    【解决方案1】:

    您是否考虑过使用 all items 属性?

    AllItems = Page1.WebList("name:=accountId").GetROProperty("all items")
    SplitItems = Split(AllItems, ";")
    Found = False
    For i = 0 To UBound(AllItems)
        If AllItems(i) = "<product>" Then
            Found = True 
            Exit For
        End If
    Next
    

    【讨论】:

    • 感谢您的回复。对不起,但似乎我并不完全清楚问题似乎是什么。实际上,“项目计数”属性工作正常,我得到了正确的数字。有问题的部分似乎是:'如果有产品,则返回帐户 If Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5 ) 然后 hasProducts = true End If
    • 问题是有什么不同?如您所见,我添加了等待存在但没有任何改进。谢谢,阿隆
    • @Alon,可能会有所不同的是,当您处于调试模式时,焦点会在每一步移至 QTP,并且不会保留在浏览器上。
    • 即由于时间差异,调试和正常运行模式可能会产生不同的结果。
    • @TheBlastOne,不仅仅是时间上的差异,还有焦点问题。
    【解决方案2】:

    感谢 Jonty,找到了解决方案,

    问题出在以下部分:

      ' 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
    

    第一次进入循环,账号确实没有任何产品,所以显然没有被识别出来。于是accountIndex加一,在web列表中选择了对应的账号。

    不,这就是问题所在。 select方法导致页面刷新,条件Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) 因此在加载 web 列表之前进行了评估,返回 false。

    我考虑过这个选项,但我认为(显然是错误的)Exist(5) 应该可以解决问题,但它的工作方式似乎与预期不同。

    谢谢,

    阿隆

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多