Verify All Text in a Silverlight ComboBox
PROBLEM
I would like to verify all the text at once in a Silverlight ComboBox.
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: TextContent is deprecated 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 TextContent.
C#
//Put the ComboBox into a variable
ComboBox cb = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox;
//Get all CombBoxItems within the ComboBox
IList<ComboBoxItem> list = cb.Find.AllByType<ComboBoxItem>();
string full = null;
//Get the text content from the TextBlock in each ComboBoxItem
//and add it to a string
foreach (ComboBoxItem cbi in list)
{
string text = cbi.Find.ByType<TextBlock>().TextContent.ToString();
full += text;
}
Log.WriteLine(full);
//Verify whether the concatenated string equals the expected string
Assert.AreEqual(full, "AllAddDeleteEditNavigationNone");
Visual Basic
'Put the ComboBox into a variable
Dim cb As ComboBox = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox
'Get all CombBoxItems within the ComboBox
Dim list As IList(Of ComboBoxItem) = cb.Find.AllByType(Of ComboBoxItem)()
Dim full As String = Nothing
'Get the text content from the TextBlock in each ComboBoxItem
'and add it to a string
For Each cbi As ComboBoxItem In list
Dim text As String = cbi.Find.ByType(Of TextBlock)().TextContent.ToString()
full += text
Next
Log.WriteLine(full)
'Verify whether the concatenated string equals the expected string
Assert.AreEqual(full, "AllAddDeleteEditNavigationNone")
C#
ComboBox cb = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox;
string full = cb.TextBlockContent.ToString();
full = full.Trim();
Log.WriteLine(full);
Assert.AreEqual(full, "All Add Delete Edit Navigation None");
Visual Basic
Dim cb As ComboBox = Pages.SilverlightToolkitSamples.SilverlightApp.Item0Combobox
Dim full As String = cb.TextBlockContent.ToString()
full = full.Trim()
Log.WriteLine(full)
Assert.AreEqual(full, "All Add Delete Edit Navigation None")