When I have tried to create some test that select item in ComboBox, I have found some issues
I can't :
select item from comboBox , as workaround
public void selectItemInComboBox( By by, String name ) {
WebElement element= getWebElement( by );
element.sendKeys( name );
element.sendKeys( Keys.ENTER );
}
get amount of item that combox contains
driver.findElements( By.xpath( "//*[@AutomationId='ID']//*[@ClassName='ComboBox']//*[ClassName='ListBoxItem']") )
result =0
even if i have made click for opening combobox
click( By.xpath( "//*[@AutomationId='ID']"))
driver.findElements( By.xpath( "//*[@AutomationId='ID']//*[@ClassName='ComboBox']//*[ClassName='ListBoxItem']") )
result =0
get selected item name in combobox,
First of all you'll need to open the combo box by clicking the open button, it is a child item of the combo. Then you'll have to find all children of the combo box and go through them in a loop to see if a specific item is in there or not.
I used the code below to select a specific value in a combo box named "comboBox1".
var combo = sessionAppWinForms.FindElementByAccessibilityId("comboBox1");
var open = combo.FindElementByName("Open");
combo.SendKeys(Keys.Down);
var listItems = combo.FindElementsByTagName("ListItem");
Debug.WriteLine($"Number of list items found: {listItems.Count}");
Assert.AreEqual(6, listItems.Count);// check the expected item count is equal to actual item count
foreach (var comboKid in listItems)
{
Debug.WriteLine(comboKid.Text);
if (comboKid.Text == "NJ")
{
// select a specific combo item, same logic can be extended to do more things
comboKid.Click();
}
}
This code example is taken from my course about WinAppDriver which you might see here to learn more.
The following code works for my case:
public void chooseValueFromCombobox(String control, String item) {
WebElement combobox = getActions.getElement(control);
try {
combobox.click();
WebElement comboboxItem = combobox.findElement(By.xpath(".//*[@ClassName='ListBoxItem' and @Name='" + item + "']"));
moveToElement(comboboxItem);
} catch (WebDriverException exception) {
exception.printStackTrace();
}
}
private void moveToElement(WebElement element) {
actions.moveToElement(element).click().perform();
}
protected WebElement getElement(String elementID) {
return winDriver.findElementByAccessibilityId(elementID);
}
Most helpful comment
First of all you'll need to open the combo box by clicking the open button, it is a child item of the combo. Then you'll have to find all children of the combo box and go through them in a loop to see if a specific item is in there or not.
I used the code below to select a specific value in a combo box named "comboBox1".
This code example is taken from my course about WinAppDriver which you might see here to learn more.