Friday, March 3, 2017

Selenium WebDriver - Exploring the Combo-Box element (C#)

Combo Box

Definition: A combo box is a commonly used graphical user interface widget (or control). Traditionally, it is a combination of a drop-down list or list box and a single-line editable textbox, allowing the user to either type a value directly or select a value from the list (Wiki).

Selenium WebDriver support this type of element with the WebElement class, but it also provides the "Select" class that we can use to execute a new type of methods and properties to control the Combo-Box element.

תוצאת תמונה עבור ‪test automation using selenium webdriver‬‏

How to work with single-Selection Combo-Box


HTML

<select class="goog-te-combo"><option value="">Select Language</option><option value="af">Afrikaans</option><option value="sq">Albanian</option><option value="eu">Basque</option><option value="be">Belarusian</option><option value="yo">Yoruba</option><option value="zu">Zulu</option></select>


Code
SelectElement ComboBoxValues = new SelectElement(Firefox.FindElement(By.XPath("//select")));

//Validate Combo-Box does not allow multiple selections (Unique Selection)
Assert.IsFalse(ComboBoxValues.IsMultiple);

//Validate the Combo-Box size (The Number of options that are available for selection)
Assert.AreEqual(104,ComboBoxValues.Options.Count);

//Select an option from the list by text
ComboBoxValues.SelectByText("Azerbaijani");



//validate the current selected item
Assert.AreEqual("Azerbaijani",ComboBoxValues.SelectedOption.Text);

//Select an option from the list by Index location
ComboBoxValues.SelectByIndex(20);//Ko

//Select an option from the list by value
ComboBoxValues.SelectByValue("ar");

How to work with Multi-Selection Combo-Box

The select class provides other great methods that will allow us to Select/Deselect all values of the element; the following code will show you how to do it. 

HTML
<select name="AnimalNames" size="5" multiple="multiple" style="width:100px">
 <option value="fo">Fox</option>
 <option value="du">Duck</option>
 <option value="sh">Shark</option>
 <option value="ti">Tigar</option>
 <option value="ea">Eagle</option>
</select>







Code
SelectElement AllListValues = new SelectElement(Firefox.FindElement(By.XPath("//select")));

foreach (var ListItem in Firefox.FindElements(By.XPath("//option")))
{
//Select all values
AllListValues.SelectByText(ListItem.Text);
}

//Deselect By index
AllListValues.DeselectByIndex(0);

//Deselect By text
AllListValues.DeselectByText("Eagle");

//Deselect By value
AllListValues.DeselectByValue("ti");

No comments:

Post a Comment

My Presentations