Script help for finding a value in Listbox

13 posts, 0 answers
  1. Akbar
    Akbar avatar
    19 posts
    Member since:
    Mar 2011

    Posted 30 May 2011 Link to this post

    Hi Telerik/All,


    I have a situation where I need to automate test in which automation test should searches for a value in the list box and if value is present in the list box then it needs to highlight it. I have developed a customize code which is as follows:


    pages.BORWL08D63186Wmservice.SilverlightApp.LstProcessOrdersListbox.Find.ByTextContent(Data("Process Order").ToString()).User.Click()


    This code works fine when the value is visible in the listbox without scrolling down. But if the value is at the bottom of the listbox (with vertical scroll bar enabled) then the above codes does not work.


    I need your suggestion/help to develop a script, where it looks for the value in the listbox if it’s not present then scroll down and search for the value till the end of listbox. Your help would be a great help.


    Currently the alternative way which I am using is as follows:

    1)  Look for value in listbox with above customize code. (Mark the step as continue on failure)

    2)  Press the vertical scroll bar. (Mark the step as continue on failure)

    3)  Look for value in listbox with above customize code. (Mark the step as continue on failure)

    4)  Press the vertical scroll bar. (Mark the step as continue on failure)

    5)  Look for value in listbox with above customize code.


    This codes work fine but its not fully automate, and it has following issues:

    1)  If the value is at the bottom of a very large amount data in the list box then this will not work (stops after two vertical scroll down).

    2)  If the value is in the middle of the list box after one vertical scroll down pressed, then we will have few failed steps and the overall result of the test will be failed. Even though it had successfully passed the test.


    Your help in this regards will be be much appreciated. Also please note that i have scripts written in VB


    Cheers


    Akbar

  2. Akbar
    Akbar avatar
    19 posts
    Member since:
    Mar 2011

    Posted 01 Jun 2011 Link to this post

    Hi,

    Help with regards to my above query will be much appreciated. This will help us use telerik tool more often in our project.

    Regards

    Akbar
  3. Akbar
    Akbar avatar
    19 posts
    Member since:
    Mar 2011

    Posted 01 Jun 2011 Link to this post

    Hi All,

    Any help with my above query.

    Cheers

    Akbar
  4. Madhu
    Madhu avatar
    26 posts
    Member since:
    Dec 2010

    Posted 02 Jun 2011 Link to this post

    Hello Support,

    I'm also waiting for Akbar query solution?

    Also, "Press Return" is not working (attached screenshot), when I ran script all steps are showing passed including "Press Return" , but data is not filtered after "pressing return" (Enter Key). If I hit Enter Key manually it is working fine.

    I need help on Passing Enter Key via coding?

    Thanks,
    Madhu.
  5. Cody
    Admin
    Cody avatar
    3354 posts

    Posted 02 Jun 2011 Link to this post

    Hello Akbar,

    I created this KB article to demonstrate how to select a list box item by text.

    Best wishes,
    Cody
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  6. Cody
    Admin
    Cody avatar
    3354 posts

    Posted 02 Jun 2011 Link to this post

    Hi Madhu,

    It would be worthwhile trying to understand why the Press Enter test step isn't working as expected. To answer your last question, the equivalent in code is this:

    Manager.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.Enter);
     


    All the best,
    Cody
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  7. Akbar
    Akbar avatar
    19 posts
    Member since:
    Mar 2011

    Posted 05 Jun 2011 Link to this post

    Hi Cody,

    Thanks for the KB.

    All my customize code is written in VB. Is it possible to provide the KB in VB language.

    Thanks

    Akbar
  8. Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 06 Jun 2011 Link to this post

    Hello Akbar,

    There are several free code converters online, including this one by Telerik. Simply copy and paste the code from the KB and convert from C# to VB.

    Regards,
    Anthony
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  9. Akbar
    Akbar avatar
    19 posts
    Member since:
    Mar 2011

    Posted 07 Jun 2011 Link to this post

    Hi,

    Thanks for the code converter link.

    Now i have issue, where i am getting complilation error which is as below:
    C:\CIMSharp\Tools\Telerik Automation Test\Original Code\Main Step.tstest.vb: Line 129: (BC30652) Reference required to assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' containing the type 'System.Windows.Forms.Keys'. Add one to your project.
    C:\CIMSharp\Tools\Telerik Automation Test\Original Code\Main Step.tstest.vb: Line 130: (BC30456) 'Windows' is not a member of 'System'.


    And my customize code is :

    Imports Telerik.WebAii.Controls.Html
    Imports Telerik.WebAii.Controls.Xaml
    Imports System
    Imports System.Collections.Generic
    Imports System.Text

    Imports ArtOfTest.Common.UnitTesting
    Imports ArtOfTest.WebAii.Core
    Imports ArtOfTest.WebAii.Controls.HtmlControls
    Imports ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts
    Imports ArtOfTest.WebAii.Design
    Imports ArtOfTest.WebAii.Design.Execution
    Imports ArtOfTest.WebAii.ObjectModel
    Imports ArtOfTest.WebAii.Silverlight
    Imports ArtOfTest.WebAii.Silverlight.UI


        '
        ' You can add custom execution steps by simply
        ' adding a 'Sub' routine and decorating it with the <CodedStep("Description")>
        ' attribute to the test method.
        ' Those steps will automatically show up in the test steps on save.
        '
        ' The BaseWebAiiTest exposes all key objects that you can use
        ' to access the current testcase context. [i.e. ActiveBrowser, Find ..etc]
        '
        ' Data driven tests can use the Data(columnIndex) or Data("columnName")
        ' to access data for a specific data iteration.
        '
        ' Example:
        '
        ' <CodedStep("MyCustom Step Description")> Public Sub MyTestMethod()
        '
        '       // Custom code goes here
        '      ActiveBrowser.NavigateTo("http://www.google.com")
        '
        '       // Or
        '       ActiveBrowser.NavigateTo(Data("url"))
        '
        ' End Sub
        '
        Public Class Main_Step
            Inherits BaseWebAiiTest

    #Region "[ Dynamic Pages Reference ]"

            Private _pages As Pages
            
            '''<summary>
            ''' Gets the Pages object that has references
            ''' to all the elements, frames or regions
            ''' in this project.
            '''</summary>
            Public ReadOnly Property Pages() As Pages
                Get
                    If (_pages Is Nothing) Then
                        _pages = New Pages(Manager.Current)
                    End If
                    Return _pages
                End Get
            End Property
            
    #End Region
            
            ' Add your test methods here...


           
            
            Private Shared Function FindListboxItemByText(itemToFind As String, myLB As ListBox) As ListBoxItem
                ' We may have to scroll the list box to find the item we    
                ' want, so get the scroll viewer attached to the listbox
                Console.WriteLine("in function")
                Dim scroller As ScrollViewer = myLB.Find.ByType(Of ScrollViewer)()
                Dim items As IList(Of ListBoxItem)
                Dim scrollPos As Double = 0
                Do
                    ' Get all list box items currently contained in the        
                    ' visual tree        
                    items = myLB.Find.AllByType(Of ListBoxItem)()         
                    ' Iterate through each list box item looking for the one        
                    ' we want        
                    For i As Integer = 0 To items.Count - 1
                        Console.WriteLine(items(i).Text.ToString())
                        If items(i).Text.Contains(itemToFind) Then
                            ' We found the one we want. Scroll it to the top               
                            ' (in case it's outside the listbox's viewport)               
                            ' then return it to the caller.
                            Console.WriteLine("In  if condition")
                            scroller.InvokeMethod("ScrollToVerticalOffset", i)
                            Return items(i)                        
                        End If                    
                    Next
                    ' We didn't find it, scroll down by a page worth of data        
                    ' to populate the VisualTree with the next page of data        
                    ' and try again.        
                    scrollPos += scroller.ViewportHeight
                    scroller.InvokeMethod("ScrollToVerticalOffset", scrollPos)
                        ' Refresh our cached copy of the listbox.    
                    myLB.Refresh()
                Loop While scrollPos <= scroller.ExtentHeight
                ' We scanned the entire list and didn't find the right item    
                ' to select.    
                Return Nothing
            End Function

            <CodedStep("New Coded Step")> _
            Public Sub Main_Step_CodedStep()
                Dim itemToFind As String = "PO_SMK_40"
                Dim app As SilverlightApp = ActiveBrowser.SilverlightApps()(0)
                Dim myLB As ListBox = app.Find.ByAutomationId(Of ListBox)("lstProcessOrders")
                Dim item As ListBoxItem = FindListboxItemByText(itemToFind, myLB)
                Assert.IsNotNull(item, """" + itemToFind + """ not found in the listbox.")
                item.Click(MouseClickType.LeftClick, False, False, 0, 0, ArtOfTest.Common.OffsetReference.AbsoluteCenter, _
        ArtOfTest.Common.ActionPointUnitType.Pixel, System.Windows.Forms.Keys.None)      
                              
            End Sub



           
        End Class


    It seems i need to add System.Windows.Forms.Keys to the project. But i am unable to do it.

    Cheers

    Akbar
  10. Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 07 Jun 2011 Link to this post

    Hello Akbar,

    Add the System.Windows.Forms reference to the Project:

    1. Click the Project tab.
    2. Click the Show button in the Setting ribbon.
    3. Click Script Options in the left column of User Settings.
    4. Click "Add Reference."
    5. Browse to the following folder:

    32-bit machine:
    C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client

    64-bit machine:
    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client

    6. Choose System.Windows.Forms.dll and click Open.
    7. Click OK.

    Please see this KB article for more information.

    Kind regards,
    Anthony
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  11. Akbar
    Akbar avatar
    19 posts
    Member since:
    Mar 2011

    Posted 10 Jun 2011 Link to this post

    Thanks for your help.

    It works fine.
  12. Madhu
    Madhu avatar
    26 posts
    Member since:
    Dec 2010

    Posted 01 Jul 2011 Link to this post

    Hello Support/Akbar,

    Please can you help me out, I'm getting below exception:

    System.InvalidOperationException: Point (224, 1975) outside bounds of browser window: (0, 90, 1280, 881)

    Here I'm searching for ComboBox item. My VB code as follows:

    Private Shared Function FindComboBoxItemByText(itemToFind As String, myCB As ComboBox) As ComboBoxItem
                Dim scroller As ScrollViewer = myCB.Find.ByType(Of ScrollViewer)()
                Dim items As IList(Of ComboBoxItem)
                Dim scrollPos As Double = 0

                Do
                  items = myCB.Find.AllByType(Of ComboBoxItem)()
                
                    For i As Integer = 0 To items.Count - 1
                        If items(i).Text.Equals(itemToFind) Then
                          scroller.InvokeMethod("ScrollToVerticalOffset", i)
                            Return items(i)
                        End If
                    Next
                
                    scrollPos += scroller.ViewportHeight
                    scroller.InvokeMethod("ScrollToVerticalOffset", scrollPos)
                    myCB.Refresh()
                
                Loop While scrollPos <= scroller.ExtentHeight
                Return Nothing
    End Function

            <CodedStep("New Coded Step")> _
            Public Sub LoanTradeEntry_QuickTrades_CodedStep()
             Dim itemToFind As String = "AutoPortfolio21731"
            Dim app As SilverlightApp = ActiveBrowser.SilverlightApps()(0)
       Dim myCB As ComboBox = app.Find.ByAutomationId(Of ComboBox)("PageScrollViewer")
       Dim item As ComboBoxItem = FindComboBoxItemByText(itemToFind, myCB)
         Assert.IsNotNull(item, """" + itemToFind + """ not found in the ComboBox.")
        item.Click(MouseClickType.LeftClick, False, False, 0, 0, ArtOfTest.Common.OffsetReference.AbsoluteCenter, _
    ArtOfTest.Common.ActionPointUnitType.Pixel, System.Windows.Forms.Keys.None)
            End Sub

    If I'm passing search string which is not in ComboBox list, it is scrolling down until last record and throws error, that is fine and I expected.
    Also, if the string is visible (after opening CoboBox) works fine.

    But, if I'm passing string which is in list (but not visible, needs to scroll down to see), throwing outside bounds of browser window exception!

    What I'm doing wrong in this one?

    Thanks,
    Madhu.
  13. Cody
    Admin
    Cody avatar
    3354 posts

    Posted 05 Jul 2011 Link to this post

    Hello Madhu,

    Rather than try and figure out why your code doesn't work as is, let me ask this... why not use our own SelectItemByText function like this?


    Pages.SilverlightAppTesting.SilverlightApp.ComboBox1Combobox.SelectItemByText(false, "Combobox Item 16");

    Best wishes,
    Cody
    the Telerik team
    Register today for a live 'What's New in Test Studio R1 2011 SP2' event on Tuesday, July 19 at 2pm EST!

    Have you looked at the new Online User Guide for Telerik Test Studio?
Back to Top