tags:

views:

42

answers:

2

Hi,

I would extremely appreciate if anyone can suggest a solution for this.

I have a simple function that is is expecting for a browser to be opened on a page containing a web list that each value of it represents an account. When an account is selected it's products (if any) are displayed.

The functions goal is to retrieve an index of an account with products (the first to be found) or -1 if there are none.

The problem, which I can't figure out what is causing it, is that the function will return the correct result when I'm debugging it - meaning running the code step by step using F10, but will return a wrong result if I'll run regularly (F5). This behavior is consistent and the function retrieves the same result each time for each type of runs, meaning it's not a bug that just makes the function return a random answer.

This is the function:

' @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

I'm running on Pentium 4, 3.2 GHZ, 2 GB RAM, Win XP, SP 3,IE 7, QTP 10.0 Build 513

Thanks!

A: 

Have you considered using the all items property?

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
Motti
Thanks for your response.I'm sorry but it seems I wasn't totally clear to what the problem seems to be. Actually, the "items count" property is working fine and I get the correct number. The problematic part seems to be:' Return account if has products If Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) Then hasProducts = true End If
Alon Mos
The question is what makes the difference?As you can see, I added a wait for the exist but with no improvement.Thanks,Alon
Alon Mos
@Alon, The thing that could make the difference is that when you're in debug mode the focus moves to QTP at each step and doesn't remain on the browser.
Motti
A: 

Solution was found thanks to Jonty,

The problem was in the following section:

  ' 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

The first time entered to the loop, the account really didn't have any products, so obviously none was recognized. So accountIndex was increased by one and the corresponding account was selected in the web list.

No here lies the problem. The select method caused a refresh in the page and the condition Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) was evaluated before the web list was loaded thus, returning false.

I considered that option, but I thought (wrongly apparently) that the Exist(5) should do the trick, but it seems that it works differently than expected.

Thanks,

Alon

Alon Mos