I already looked through the previous discussions and issue reports hoping to find an answer to this question but I didn't. Does anyone know a reliable way to select an arbitrary item from a large list by its text?
ListBox.Select("some item name") only works if the item is currrently shown on the display. If the item is in the list but currently offscreen, it throws a UIActionException saying
that the item was not found.
If you know the position of the item in the list, ListBox.Select(index) works but that's not really a good option because the index of an item changes if there are dynamic additions or deletions to the list.
I tried using the built-in ScrollBars but could not find a way to detect that I tried to scroll when already at the end of the list. For example, with say 40 items in the list and a listbox large enough
to hold 6 or 7 items, the test can scroll 50 times with no problem... So, you can ListBox.Select("some item name") and catch the UIActionException and then scroll down
and try again but you can't tell when to give up searching.
I also tried a loop comparing ListBox.Items[loopIndex].Text to the search text. This didn't work because ListBox.Items[loopIndex].Text was an empty string for
any item that was off screen.
The only way I found to do this was to loop from 0 to ListBox.Items.Count; Select using the loop index; and comparing the selected item's text to the search text. The code looks something like this:
ListBox listBox = window.Get<ListBox>("theListBox");
string itemToSelect =
"some item name";
found = false;
i=0; ((i < listBox.Items.Count) && !found); i++)
if (listBox.Items.SelectedItemText == itemToSelect)
found = true;
of text " + itemToSelect +
" not found.");
Obviously, that code belongs in a method that the test calls but you can get the idea of how it works from this sample.
If there is a better way, I would be VERY glad to hear it!
Sep 21, 2008 at 9:11 AM
This feature is supposed to be already there. You shouldn't have to do anything more than ListBox.Select("some item name").
Can you provide me code example for this.
Sure. I don't see any way to attach a file here so I'll create a new issue and upload a zip file with the example.