Unable to find Silverlight child controls from ListBox when using ItemTemplate

4 posts, 0 answers
  1. Gary
    Gary avatar
    2 posts
    Member since:
    Mar 2011

    Posted 16 Mar 2011 Link to this post

    Hiya,

    We have a listbox we're trying to automate in WebAii.
    The ListBox is bound to a collection via its ItemsSource, and its items are templated to show an image and a checkbox. We want to be able to automate checking and unchecking the checkbox.

    It looks like I'm unable to do this - I'm able to find the ListBox, but its Items collection contains an ItemCollection of strings.
    listBox.Find.AllByType<ListBoxItem>();
    returns an empty list.
    The listbox's Children collection is empty.

    Am I able to do what I'm trying to do?
    I found the following link which hints that I'm unable to (but that post didn't say that they're using ItemTemplating): http://www.telerik.com/automated-testing-tools/community/forums/webui-test-studio-developer-edition/webaii-automation-framework/silverlight-obtaining-listbox-items.aspx

    If this isn't possible, will it be available in a future release?

    Thanks,

    Gary
  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 24 Mar 2011 Link to this post

    Hi Gary,
        yes, this can definitely be done. I tried it just to make sure. First I create a Silverlight application with the following xaml code:
    <ListBox x:Name="lstGames" Background="AliceBlue" Width="300" Height="150">
                <ListBoxItem Background="LightCyan" Foreground="Orange"  FontFamily="Verdana" FontSize="12" FontWeight="Bold">
                    <CheckBox x:Name="chkBaseball" Content="Baseball"></CheckBox>
                </ListBoxItem>
                <ListBoxItem Background="Lavender" Foreground="Blue"    FontFamily="Courier New" FontSize="14" FontWeight="Bold">
                    <CheckBox x:Name="chkSoccer" Content="Soccer"></CheckBox>
                </ListBoxItem>
                <ListBoxItem Background="LightCoral" Foreground="Pink"    FontFamily="Verdana" FontSize="13" FontWeight="Bold">
                    <CheckBox x:Name="chkHockey" Content="Hockey"></CheckBox>
                </ListBoxItem>
                <ListBoxItem Background="LightPink" Foreground="Red"    FontFamily="Verdana" FontSize="14" FontWeight="Bold">
                    <CheckBox x:Name="chkCricket" Content="Cricket"></CheckBox>
                </ListBoxItem>
                <ListBoxItem Background="lightGray" Foreground="Black"    FontFamily="Calibri" FontSize="12" FontWeight="Bold">
                    <CheckBox x:Name="chkTennis" Content="Tennis"></CheckBox>
                </ListBoxItem>
    </ListBox>
    I took this code from this tutorial:
    http://www.a2zdotnet.com/View.aspx?Id=118

    I deployed that to a VS Development server.

    Then I create WebAii Test that looks like this:
    Manager.Settings.EnableSilverlight = true;      
    Manager.LaunchNewBrowser();
    ActiveBrowser.NavigateTo("http://localhost:23260/SilverlightApplication1TestPage.aspx"); //This  is the URL of my application the Development server
     
    SilverlightApp app = ActiveBrowser.SilverlightApps()[0];
    ListBox lb =  app.FindName<ListBox>("lstGames");
    IList<ListBoxItem> l = lb.Find.AllByType<ListBoxItem>();
    CheckBox c = l[0].Find.ByAutomationId<CheckBox>("chkBaseball");
    c.User.Click(MouseClickType.LeftClick);
     
    System.Threading.Thread.Sleep(2000);

    This successfully clicks the first Checkbox in my application. Check out this example, I hope it will help you deal with your own Automation Task.

    Hope to hear from you soon!

    Best wishes,
    Stoich
    the Telerik team
  3. Gary
    Gary avatar
    2 posts
    Member since:
    Mar 2011

    Posted 24 Mar 2011 Link to this post

    Hi,

    Thanks for the same reply - unfortunately you haven't reproduced the scenario as I described.

    Our ListBox doesn't have the items inline, the ListBox items are populated via its ItemsSource attribute, and the listbox has an ItemTemplate to format the items.

    In this case, the Find.AllByType<ListBoxItem>(); returns an empty list.

    (It may or may not be relevant, but our ListBoxItem is in a Popup control).

    I don't have too much time to break this problem down to the minimum, but it would be good if you could at least reproduce it with the listbox items being bound, with an ItemTemplate.

    Thanks,

    Gary
  4. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 30 Mar 2011 Link to this post

    Hello Gary,
         it doesn't matter how your ListBox is populated. Test Studio only considers the visual tree (i.e. the DOM Explorer). The problem is probably that instead of using ListBoxItem, you're extending ListBoxItem. The extended ListBoxItem will show up under a different name in the visual tree. This can cause Find.AllByType<ListBoxItem>();
    to return an empty list. You're searching for type ListBoxItem but if you're extending ListBoxItem then that will show up as a different type. Hence Find.AllByType<ListBoxItem> returns no objects. If this is the case then you can remedy the situation by searching for XamlTagBase like this:
    IList l =  (IList) app.Find.AllByExpression(new XamlFindExpression("XamlTagBase=ListBoxItem"));

    If this doesn't seem to be the case then it might be best if you can provide your Xaml code or direct acess to a sample of your ListBox.

    Hope to hear from you soon!

    Best wishes,
    Stoich
    the Telerik team
Back to Top