Skip to content

Instantly share code, notes, and snippets.

@stuartf7
Created July 4, 2012 16:23
Show Gist options
  • Save stuartf7/3048139 to your computer and use it in GitHub Desktop.
Save stuartf7/3048139 to your computer and use it in GitHub Desktop.
Selenium Select Logic
using System.Collections.Generic;
namespace OpenQA.Selenium.Support.UI
{
public class Select
{
private readonly IWebElement element;
public bool Multiple { get; private set; }
/// <summary>
/// Create a new instance of a Select object.
/// </summary>
/// <param name="element">The element to be wrapped</param>
public Select(IWebElement element)
{
if (element.TagName ==null || element.TagName.ToLower() != "select")
throw new UnexpectedTagNameException("select", element.TagName);
this.element = element;
//let check if it's a multiple
string attribute = element.GetAttribute("multiple");
Multiple = attribute != null && attribute.ToLower().Equals("multiple");
}
/// <summary>
/// Fetch the lsit of options for the Check.
/// </summary>
/// <returns>List of <see cref="IWebElement"/>.</returns>
public IList<IWebElement> GetOptions()
{
return element.FindElements(By.TagName("option"));
}
/// <summary>
/// Select the option by the text displayed.
/// </summary>
/// <param name="text">The text of the option to be selected.</param>
public void SelectByText(string text)
{
foreach (IWebElement option in GetOptions())
{
if (option.Text == text)
{
option.Select();
if(!Multiple) return;
}
}
}
/// <summary>
/// Select an option by the value.
/// </summary>
/// <param name="value">The value of the option to be selected.</param>
public void SelectByValue(string value)
{
foreach (IWebElement option in GetOptions())
{
if (option.Value == value)
{
option.Select();
if (!Multiple) return;
}
}
}
/// <summary>
/// Select the option by the index.
/// </summary>
/// <param name="index">The index of the option to be selected.</param>
public void SelectByIndex(int index)
{
foreach (IWebElement option in GetOptions())
{
if (option.GetAttribute("index").Equals(index.ToString()))
{
option.Select();
if (!Multiple) return;
}
}
}
/// <summary>
/// Deselect the option by the text displayed.
/// </summary>
/// <param name="text">The text of the option to be deselected.</param>
public void DeselectByText(string text)
{
foreach (IWebElement option in GetOptions())
{
if (option.Text.Equals(text))
{
if (option.Selected) option.Toggle();
if (!Multiple) return;
}
}
}
/// <summary>
/// Deselect the option by the value.
/// </summary>
/// <param name="value">The value of the option to deselect.</param>
public void DeselectByValue(string value)
{
foreach (IWebElement option in GetOptions())
{
if (option.Value.Equals(value))
{
if (option.Selected) option.Toggle();
if (!Multiple) return;
}
}
}
/// <summary>
/// Deselect the option by the index.
/// </summary>
/// <param name="index">The index of the option to deselect.</param>
public void DeselectByIndex(int index)
{
foreach (IWebElement option in GetOptions())
{
if (option.GetAttribute("index").Equals(index.ToString()))
{
if (option.Selected) option.Toggle();
if (!Multiple) return;
}
}
}
/// <summary>
/// Get the selected item.
/// </summary>
/// <returns>The <see cref="IWebElement"/> that is selected.</returns>
/// <remarks>If more than one item is selected this will return the first item.</remarks>
public IWebElement GetSelected()
{
foreach (IWebElement option in GetOptions())
{
if (option.Selected) return option;
}
throw new NoSuchElementException("No options are selected");
}
/// <summary>
/// Get the all the selected options within the select element.
/// </summary>
/// <returns>A list of <see cref="IWebElement"/>.</returns>
public IList<IWebElement> GetAllSelectedOption()
{
List<IWebElement> returnValue = new List<IWebElement>();
foreach (IWebElement option in GetOptions())
{
if (option.Selected) returnValue.Add(option);
}
return returnValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment