Verify Silverlight ComboBox Contains Specific Text
PROBLEM
I would like to iterate through a Silverlight ComboBox and verify that it contains specific text strings.
SOLUTION
This is possible with a coded solution. The example below is against this Silverlight demo site.
After navigating there, click DataForm in the left-hand menu. Then open the CommandButtonsVisibility ComboBox. Finally, add a coded step:
Note: Text only gives that element's text. As of version 2011.2.1229, use TextBlockContent on the parent element to recursively search for the text within its children.
Click here to show code for an earlier version using Text.
C#
//Put the ComboBox into a variable
ComboBox cb = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox;
//Check whether any TextBlock in the ComboBox contains the data driven string.
bool found = cb.Find.AllByType<TextBlock>().Any((tb) => tb.Text.Contains(Data["Col1"].ToString()));
//Log the results.
Log.WriteLine("Match found: " + found.ToString());
//Perform the assert
Assert.IsTrue(found);
Visual Basic
'Put the ComboBox into a variable
Dim cb As ComboBox = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox
'Check whether any TextBlock in the ComboBox contains the data driven string.
Dim found As Boolean = cb.Find.AllByType(Of TextBlock)().Any(Function(tb) tb.Text.Contains(Data("Col1").ToString()))
'Log the results.
Log.WriteLine("Match found: " + found.ToString())
'Perform the assert
Assert.IsTrue(found)
C#
ComboBox cb = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox;
bool found = cb.TextBlockContent.Contains(Data["Col1"].ToString());
Log.WriteLine("Match found: " + found.ToString());
Assert.IsTrue(found);
Visual Basic
Dim cb As ComboBox = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox
Dim found As Boolean = cb.TextBlockContent.Contains(Data("Col1").ToString())
Log.WriteLine("Match found: " + found.ToString())
Assert.IsTrue(found)