ComboBox bug with item text

Mar 4, 2010 at 8:42 PM
Edited Mar 4, 2010 at 8:44 PM
I check out'ed white revision to 55 from google with turtoise svn. i'm using windows 7 ultimate 32bit, visual studio 2008 sp1 team system. i'm creating white test with c# for WPF application.
Problem.
When trying to get editable ComboBox item text it returns an emty string. comboBox.Items[i].Text returns ""
Older version.
With White 0.19.2 Binary version this works correctly. comboBox.Items[i].Text returns text.
How to get the text from comboBox item with newest white? only diference for me is just that newest white works faster, and i can save time on testing :)
Coordinator
Mar 8, 2010 at 4:52 PM

Thats puzzling, can you send me some sample code for AUT and the test

 

Mar 25, 2010 at 3:18 PM
Edited Mar 25, 2010 at 3:18 PM
protected string GetTextFromEditableComboBox(ComboBox comboBox) 
        {
            var element = comboBox.GetElement(SearchCriteria.ByAutomationId("AI_PART_EditableTextBox"));
            var textBox = new TextBox(element, comboBox.ActionListener);
            var text = textBox.Text;
            return text;
        }

With this method i get text from editable comboBox in WPF

How can i get text from not editable ComboBox?

if i simply get comboBox

 

var comboBox = something.Get<WPFComboBox>(SearchCriteria.ByAutomatioid("something"))

string text = comboBox.Name

it returns empty string

Mar 30, 2010 at 8:03 AM

Hi,

I wonder if there was a resolution to this problem with testing ComboBox controls (silverlight). They seem to be unreliable. No problem manual. I can see that the ComboBox is populated (while the test is running), but the select by item text often does not work. Run it again and it may work.

Rob Smyth

Mar 30, 2010 at 8:44 AM

selectedItemText should give you the value of the selected item in the combobox

 

The problem in many cases is, that before the selection becomes active the "popup" with the elements is closed and the selection, or click has no effect,

 

so I implement something like

ListItem item = comboBox.ListItems.Where(p=>p.Name=="name").First();

comboBox.Click();

item.Click(); or item.Select();

 

in some cases I build a little loop that check the selectedItemText and try again if not correct.

it might be that the method comboBox.Select("name") works not as expected, maybe it's a performance problem on fast machines or to slow machines, I don't know.

Throndorin

 

 

Mar 30, 2010 at 9:06 AM
Throndorin wrote:

so I implement something like

ListItem item = comboBox.ListItems.Where(p=>p.Name=="name").First();

comboBox.Click();

item.Click(); or item.Select();

Many thanks Throndorin,

I will give that a go tomorrow.

Rob

 

Mar 31, 2010 at 8:27 AM
Edited Mar 31, 2010 at 8:29 AM
Throndorin wrote:

so I implement something like

ListItem item = comboBox.ListItems.Where(p=>p.Name=="name").First();

comboBox.Click();

item.Click(); or item.Select();

 

Hi Throndorin,

You put me in the right direction. The implementation:

 

            var item = _comboBox.Items.Single(thisItem => thisItem.Text == itemText);

            if (!item.Visible)
            {
                _comboBox.Click();
            }

            item.Click();

 

Getting the item first and checking if it is visible is to detect if the ComboBox is already expanding. If not click on it.

FYI - implemented this by creating a wrapper class for the White ComboBox class. Used resharper to auto generate the code (delegating members). The resulting code:

    /// 
    ///   A White ComoBox wrapper class. Modified the behaviour of the Select(string) method.
    /// 
    public class ComboBoxWrapper : IUIItem
    {
        private readonly ComboBox _comboBox;

        public ComboBoxWrapper(ComboBox comboBox)
        {
            _comboBox = comboBox;
        }

          :

        public void Select(string itemText)
        {
            var item = _comboBox.Items.Single(thisItem => thisItem.Text == itemText);

            if (!item.Visible)
            {
                _comboBox.Click();
            }

            item.Click();
        }

         :
   }

Usage:

        public ComboBoxWrapper MyComboBox
        {
            get
            {
                if (_myComboBox == null)
                {
                    _myComboBox = new ComboBoxWrapper(_document.Get<ComboBox>("myComboBox"));
                }
                return _myComboBox;
            }
        }


Thanks

Rob Smyth
Jul 14, 2010 at 3:32 PM
Edited Jul 14, 2010 at 3:36 PM

Hi

 

I got this problem too. I think the issue here is that White wants to select something from a list that has not yet been populated. And if you select an element in an empty list you end up with nothing in your hands.

So after trying a lot of the solutions that are proposed to solve this problem I simply changed the White code like this:

 

White.Core.UIItems.ListBoxItems.ComboBox.cs

starting line 71

 

public override void Select(string itemText)
        {
            if (!Enabled)
            {
                WhiteLogger.Instance.WarnFormat("Could not select {0}in {1} since it is disabled", itemText, Name);
                return;
            }
            if (Equals(itemText, SelectedItemText)) return;
            ToggleDropDown();
            System.Threading.Thread.Sleep(200);
            base.Select(itemText);
        }

        public override void Select(int index)
        {
            if (!Enabled)
            {
                WhiteLogger.Instance.Warn("Could not select " + index + "in " + Name + " since it is disabled");
                return;
            }
            ToggleDropDown();
            System.Threading.Thread.Sleep(200);
            base.Select(index);
        }


I simply added a short sleep between toggling and selecting. 50ms didn't work for me, 200 do.
I guess that depends on your machine. you can also add a function Select(int index, int sleep) to not have to build White over and over again and still be able to find an appropriate value.
I know this is a dirty solution but I prefer something that works over pure beauty ;)
Maybe the developers come up with a solution for this in future releases. Maybe the IWaitHook interface is also a solution, but I found no example at all for this.

Jan 22, 2013 at 5:59 AM

Thanks to all. The posts are really helpful. I have used the below code and it works great-

White.Core.UIItems.ListBoxItems.ListItem item;
item = combobox.Items.Single(thisItem => thisItem.Text == valuetobesetincombobox);
               if (!item.Visible)
               {
                   combobox.Click();
               }
          item.Click();
              

Coordinator
Feb 9, 2013 at 7:45 AM
The underlying issue has been fixed in https://github.com/TestStack/White/pull/65

The next update I push to NuGet will have the fix included.

You can grab the nightly build from http://teamcity.ginnivan.net/viewType.html?buildTypeId=bt2