I really like Selenium 2.0!
I've been working a lot on a side project involving WebDriver, and thus I have been creating a few extension methods for working with form data that I thought others might find helpful:
- bool IsSelected(this IWebElement element)
- bool IsChecked(this IWebElement element)
- void SetChecked(this IWebElement element)
- void SetUnchecked(this IWebElement element)
- IWebElement GetOptionByValue(this IWebElement element, string value)
- IWebElement GetOptionByText(this IWebElement element, string text)
Extension Methods
public static class WebElementExtensions
{
private static readonly StringComparer DefaultComparer =
StringComparer.InvariantCultureIgnoreCase;
private static readonly string[] Selected = new[] { "true", "selected" };
public static bool IsSelected(this IWebElement element)
{
var attribute = element.GetAttribute("selected");
return Selected.Contains(attribute, DefaultComparer);
}
private static readonly string[] Checked = new[] { "true", "checked" };
public static bool IsChecked(this IWebElement element)
{
var attribute = element.GetAttribute("checked");
return Checked.Contains(attribute, DefaultComparer);
}
public static void SetChecked(this IWebElement element)
{
if (!element.IsChecked())
element.Click();
}
public static void SetUnchecked(this IWebElement element)
{
if (element.IsChecked())
element.Click();
}
private const StringComparison DefaultComparison =
StringComparison.InvariantCultureIgnoreCase;
public static IWebElement GetOptionByValue(
this IWebElement element,
string value)
{
return element.GetOption(
o => value.Equals(o.GetAttribute("value"), DefaultComparison));
}
public static IWebElement GetOptionByText(
this IWebElement element,
string text)
{
return element.GetOption(o => text.Equals(o.Text, DefaultComparison));
}
private static IWebElement GetOption(
this IWebElement element,
Func<IWebElement, bool> predicate)
{
return element
.FindElements(By.CssSelector("option"))
.FirstOrDefault(predicate);
}
}
Unit Tests
public class WebElementExtensionTests : IDisposable
{
public IWebDriver Driver { get; private set; }
public WebElementExtensionTests()
{
Driver = new ChromeDriver("C:/Code/Drivers");
Driver.Url = "file:///C:/Code/WebDriverTests/TestPage.htm";
}
public void Dispose()
{
Driver.Dispose();
}
[Fact]
public void RadioTests()
{
var e1 = Driver.FindElement(By.Id("r1"));
var e2 = Driver.FindElement(By.Id("r2"));
IsNotSelected(e1);
IsSelected(e2);
e1.Click();
IsSelected(e1);
e2.Click();
IsNotSelected(e1);
}
protected void IsSelected(IWebElement element)
{
Assert.True(element.IsSelected());
}
protected void IsNotSelected(IWebElement element)
{
Assert.False(element.IsSelected());
}
[Fact]
public void CheckboxTests()
{
var e1 = Driver.FindElement(By.Id("c1"));
var e2 = Driver.FindElement(By.Id("c2"));
IsNotChecked(e1);
IsChecked(e2);
e1.Click();
IsChecked(e1);
e1.Click();
IsNotChecked(e1);
}
protected void IsChecked(IWebElement element)
{
Assert.True(element.IsChecked());
}
protected void IsNotChecked(IWebElement element)
{
Assert.False(element.IsChecked());
}
[Fact]
public void OptionTests()
{
var e1 = Driver.FindElement(By.Id("s1"));
var option1 = e1.GetOptionByValue("a");
var option2 = e1.GetOptionByValue("b");
IsNotSelected(option1);
IsSelected(option2);
option1.Click();
IsSelected(option1);
IsNotSelected(option2);
}
[Fact]
public void ToggleCheckedTests()
{
var e1 = Driver.FindElement(By.Id("c1"));
IsNotChecked(e1);
e1.SetChecked();
IsChecked(e1);
e1.SetChecked();
IsChecked(e1);
e1.SetUnchecked();
IsNotChecked(e1);
e1.SetUnchecked();
IsNotChecked(e1);
}
}
TestPage.htm
<html> <body> <p> <select id="s1"> <option value="a">Ant</option> <option value="b" selected="selected">Bird</option> </select> </p> <p> <input id="r1" type="radio" name="r" value="c" />Cat<br> <input id="r2" type="radio" name="r" value="d" checked="checked" />Dog </p> <p> <input id="c1" type="checkbox" name="vehicle" value="e" />Elephant<br> <input id="c2" type="checkbox" name="vehicle" value="f" checked="checked" />Fox </p> </body> </html>
Enjoy,
Tom