Hi,
I have a combobox where I am trying to select an item by text, but it seems that I am not able to do so.
I have tried to use the following method
- Find the combobox by automationId (which works): combo = app.MainWindow.Find.
ByAutomationId<ArtOfTest.WebAii.Controls.Xaml.Wpf.ComboBox>(comboBoxAutoId);
- Use itemByText to select the item (which doesn´t work): combo.SelectItemByText(true, itemToSelect);
When I use combobox.selectItemByText("Text") I get a null reference exception.
The whole method:
public
void
selectItemFromComboBox(WpfApplication app,
string
comboBoxAutoId,
string
itemToSelect)
{
ArtOfTest.WebAii.Controls.Xaml.Wpf.ComboBox combo;
Thread.Sleep(350);
app.MainWindow.RefreshVisualTrees();
combo = app.MainWindow.Find.
ByAutomationId<ArtOfTest.WebAii.Controls.Xaml.Wpf.ComboBox>(comboBoxAutoId);
combo.OpenDropDown(
true
);
FrameworkElement indicator;
for
(
int
i = 0; i < 50; i++)
{
app.MainWindow.RefreshVisualTrees();
Thread.Sleep(350);
indicator = app.MainWindow.Find.ByTextContent(itemToSelect);
if
(indicator !=
null
)
{
for
(
int
j = 0; j < 50; j++)
{
Thread.Sleep(1000);
app.MainWindow.RefreshVisualTrees();
combo.Refresh();
combo.SelectItemByText(
true
, itemToSelect);
Debug.WriteLine(
"AAAAAA"
);
Thread.Sleep(500);
if
(combo.Text.Equals(itemToSelect))
{
Thread.Sleep(350);
break
;
}
}
}
}
Please note that I am using a framework element to ensure that the item is visible/not null.
The xaml of the combobox:
<ComboBox x:Name=
"cmbAttributeType"
Grid.Row=
"0"
Grid.Column=
"1"
materialDesign:HintAssist.Hint=
"Select Type"
Style=
"{StaticResource MaterialDesignFloatingHintComboBox}"
ItemsSource=
"{Binding Path=AttributeTypesAvailable, ElementName=LevelAttributes}"
SelectedItem=
"{Binding Path=SelectedAttributeType, ElementName=LevelAttributes}"
IsEditable=
"True"
IsReadOnly=
"True"
Margin=
"3,0,0,0"
AutomationProperties.AutomationId=
"Level_Att_Type_ComboBox"
/>
I tried doing this with a Framework element (opening the combobox and then clicking on the FWE), but every time I click on the FWE the comboboxlist closes and the comboboxitem is not selected. This is the same for clicking on a combobox item from an Ilist of the combobox.
Any help on this would be great.
Many thanks in advance,
David