Skip to content

Instantly share code, notes, and snippets.

@kungfux
Created December 11, 2016 00:19
Show Gist options
  • Select an option

  • Save kungfux/b62e77ababe41e529d1bd8c871a80486 to your computer and use it in GitHub Desktop.

Select an option

Save kungfux/b62e77ababe41e529d1bd8c871a80486 to your computer and use it in GitHub Desktop.
How to write extension in .NET
// Example of extensions for IWebElement
using System.Collections.ObjectModel;
namespace OpenQA.Selenium.Extensions
{
public static class WebElement
{
public static void ClearAndSendKeys(this IWebElement webElement, string keys)
{
webElement.Clear();
webElement.SendKeys(keys);
}
public static IWebElement FindElementByCss(this IWebElement we, string css)
=> we.FindElement(By.CssSelector(css));
public static ReadOnlyCollection<IWebElement> FindElementsByCss(this IWebElement we, string css)
=> we.FindElements(By.CssSelector(css));
public static void Mark(this IWebElement we) => MarkElement(we, true);
public static void Unmark(this IWebElement we) => MarkElement(we, false);
private static void MarkElement(IWebElement we, bool isChecked)
{
if (we.Selected != isChecked)
we.Click();
}
public static void Mark(this ReadOnlyCollection<IWebElement> e) => MarkElements(e, true);
public static void Unmark(this ReadOnlyCollection<IWebElement> e) => MarkElements(e, false);
private static void MarkElements(ReadOnlyCollection<IWebElement> elements, bool isChecked)
{
foreach (var element in elements)
{
MarkElement(element, isChecked);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment