Can't find text or select item by text in databound silverlight combobox

6 posts, 0 answers
  1. Peter
    Peter avatar
    13 posts
    Member since:
    Nov 2011

    Posted 13 Nov 2011 Link to this post

    Using: ArtOfTest.WebAii.dll 2011.2.1108.0.
    Windows 7 x64 / VS2010 / Mstest / Silverlight 4.

    Calls to ComboBox.SelectItemByText(...) fail on a databound combobox.  In fact, app.Find.ByTextContent(...) also fails.  I have attached a minimal test project that has two MsTest tests that demonstrate the issue. 

    This seems like very basic functionality, so I'm hoping I'm missing something here, otherwise can someone confirm if this is a bug?

    Thanks.
  2. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 16 Nov 2011 Link to this post

    Hello Peter,

    I'm sorry for the delay getting back to you.

    It looks like you hit a known issue where the TextContent property of the ComboBox returns an empty string instead of an actual text. This is already logged as a bug and should be resolved in near future.

    To workaround the issue you can change your code for the first test like this:
    public void FindByTextContent_ComboBox()
    {
        Manager.LaunchNewBrowser(BrowserType.InternetExplorer);
        ActiveBrowser.NavigateTo("http://localhost:16696/SilverlightApplication1TestPage.aspx");
        SilverlightApp app = ActiveBrowser.SilverlightApps()[0];   
        ComboBox cb = app.Find.ByType<ComboBox>();
        Assert.AreEqual("<Select Day>", cb.Text);
    }


    Here's the workaround for the second test:
    public void SelectByTextContent_ComboBox()
    {
        Manager.LaunchNewBrowser(BrowserType.InternetExplorer);
        ActiveBrowser.NavigateTo("http://localhost:16696/SilverlightApplication1TestPage.aspx");
        SilverlightApp app = ActiveBrowser.SilverlightApps()[0];
     
        app.RefreshVisualTrees();
     
        ComboBox combo = app.Find.ByType<ComboBox>();
        Assert.IsNotNull(combo);
     
        combo.OpenDropDown(true);
        combo.Refresh();
        var comboItem = combo.Items.Where(item => item.ItemText == "<Select Day>").FirstOrDefault();
    }
    Note that if you want to select an item from the ComboBox you need to open the dropdown first. 

    Now, to select the item by text you need to create an IEnumerable collection of all items with text "<Select Day>" and take the first one(second marked line in the code above).

    I have attached your sample project back with the neccessary corrections.

    Hope this helps!

    Best wishes,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Peter
    Peter avatar
    13 posts
    Member since:
    Nov 2011

    Posted 17 Nov 2011 Link to this post

    Thanks for confirming this is a bug, can I track this in PITS (I searched but couldn't find it listed...)?

    For the second test, you show how to retrieve the ComboBoxItem that we wish to select, however you don't actually select that item.  I couldn't see any method on ComboBox or ComboBoxItem classes to cause that item to be selected in the combobox.  How can that be done, since that is the purpose of the test?
  4. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 17 Nov 2011 Link to this post

    Hello Peter,

    Here's a link to track the PITS Item: Public URL.

    As for the second issue, once the ComboBoxItem is retrieved you just need to click on it to select it. Here's the code:
    comboItem.User.Click();


    Best wishes,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Peter
    Peter avatar
    13 posts
    Member since:
    Nov 2011

    Posted 17 Nov 2011 Link to this post

    Thanks, that works, however when I tried to verify the new item had successfully been selected I hit another issue.  ComboBox.Text returns the text from ALL TextBlocks, which will be a different value before and after the combobox has been opened.  So:

      var comboItem = combo.Items.Where(item => item.ItemText == "Monday").FirstOrDefault();
      comboItem.User.Click();
      Assert.AreEqual("Monday", combo.Text);

    Fails with:

      Assert.AreEqual failed. Expected:<Monday>. Actual:<Monday <Select Day> Monday Tuesday>.

    Since ComboBox.TextContent is not working, how I can get the selected item text, regardless of whether the combobox has been opened previously or not?
  6. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 22 Nov 2011 Link to this post

    Hi Peter,

    I understand the problem you are facing. It is a common problem with the ComboBox selected item text verification.

    To overcome this you need to find the element that contains the selected item text in the Visual Tree. In your case this is a TextBlock(see screenshot).

    Here's how to change your code:
    var comboItem = combo.Items.Where(item => item.ItemText == "Monday").FirstOrDefault();
    comboItem.User.Click();
     
    //Find the TextBlock that contains the selected item text
    TextBlock tb = combo.Find.ByType<TextBlock>();
    Assert.AreEqual("Monday", tb.Text);

    Hope this helps!

    Regards,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top