Project Description
Few WatiN extensions I wrote recently.

Now available on NuGet! Support for NUnit, MSTest and xUnit Testing Frameworks!

https://nuget.org/packages/WatiN.Extensions.NUnit

https://nuget.org/packages/WatiN.Extensions.MSTest

https://nuget.org/packages/WatiN.Extensions.Xunit

1. Extensions for Browser:

(new) public static void FireJSEventByParameters<TJSEvent>(this Browser browser, params string[] parameters) where TJSEvent : IJSEvent, new()

public static void FireJSEvent<TJSEvent>(this Browser browser, string elementId) where TJSEvent : IJSEvent, new()

public static void FireJSEventByClass<TJSEvent>(this Browser browser, string elementClass) where TJSEvent : IJSEvent, new()
Built-in events: JSBlur (new), JSClick, JSChange, JSDoubleClick (new), JSFocus (new), JSSelect (new), JSSubmit (new)
Implement IJSEvent interface if you want to implement custom events. Sample:
public string ById
{
     get
     {
          return "$('#{0}').blur();";
     }
}

2. Extensions for IElementContainer:  

public static string GetText(this IElementContainer container, Constraint constraint)

(new) public static string GetText(this IElementContainer container, Constraint constraint, bool assert)

public static string GetText(this IElementContainer container, string textFieldIdOrNameOrValue)

(new) public static string GetText(this IElementContainer container, string textFieldIdOrNameOrValue, bool assert)

public static TElement Get<TElement>(this IElementContainer container, Constraint constraint) where TElement : Element

(new) public static TElement Get<TElement>(this IElementContainer container, Constraint constraint, bool assert) where TElement : Element

public static TElement Get<TElement>(this IElementContainer container, string elementIdOrNameOrValue) where TElement : Element

(new) public static TElement Get<TElement>(this IElementContainer container, string elementIdOrNameOrValue, bool assert) where TElement : Element

public static TElement GetByAttribute<TElement>(this IElementContainer container, string elementAttributeName, string elementAttributeValue) where TElement : Element

(new) public static TElement GetByAttribute<TElement>(this IElementContainer container, string elementAttributeName,
  string elementAttributeValue, bool assert) where TElement : Element public static TElement GetByClass<TElement>(this IElementContainer container, string elementClass) where TElement : Element (new) public static TElement GetByClass<TElement>(this IElementContainer container, string elementClass, bool assert) where TElement : Element public static TElement GetByText<TElement>(this IElementContainer container, string elementText) where TElement : Element (new) public static TElement GetByText<TElement>(this IElementContainer container, string elementText, bool assert) where TElement : Element public static TElement GetByUrl<TElement>(this IElementContainer container, string elementUrl) where TElement : Element (new) public static TElement GetByUrl<TElement>(this IElementContainer container, string elementUrl, bool assert) where TElement : Element public static TElement GetByUrl<TElement>(this IElementContainer container, Uri uri) where TElement : Element (new) public static TElement GetByUrl<TElement>(this IElementContainer container, Uri uri, bool assert) where TElement : Element public static IList<TElement> GetAll<TElement>(this IElementContainer container, string elementIdOrNameOrValue) where TElement : Element public static IList<TElement> GetAll<TElement>(this IElementContainer container, Constraint constraint) where TElement : Element public static IList<TElement> GetAllByAttribute<TElement>(this IElementContainer container, string elementAttributeName,
string
elementAttributeValue) where TElement : Element public static IList<TElement> GetAllByClass<TElement>(this IElementContainer container, string elementClass) where TElement : Element public static IList<TElement> GetAllByText<TElement>(this IElementContainer container, string elementText) where TElement : Element public static IList<TElement> GetAllByUrl<TElement>(this IElementContainer container, Uri elementUri) where TElement : Element public static IList<TElement> GetAllByUrl<TElement>(this IElementContainer container, string elementUrl) where TElement : Element public static void Click<TElement>(this IElementContainer container, Constraint constraint) where TElement : Element (new) public static void Click<TElement>(this IElementContainer container, Constraint constraint, bool assert) where TElement : Element public static void Click<TElement>(this IElementContainer container, string elementIdOrNameOrValue) where TElement : Element (new) public static void Click<TElement>(this IElementContainer container, string elementIdOrNameOrValue, bool assert) where TElement : Element public static void ClickByClass<TElement>(this IElementContainer container, string elementClass) where TElement : Element (new) public static void ClickByClass<TElement>(this IElementContainer container, string elementClass, bool assert) where TElement : Element public static void ClickByText<TElement>(this IElementContainer container, string elementText) where TElement : Element (new) public static void ClickByText<TElement>(this IElementContainer container, string elementText, bool assert) where TElement : Element public static void ClickByUrl<TElement>(this IElementContainer container, string url) where TElement : Element (new) public static void ClickByUrl<TElement>(this IElementContainer container, string url, bool assert) where TElement : Element public static void ClickByUrl<TElement>(this IElementContainer container, Uri uri) where TElement : Element (new) public static void ClickByUrl<TElement>(this IElementContainer container, Uri uri, bool assert) where TElement : Element public static void SetText(this IElementContainer container, Constraint constraint, string text, bool append = false) public static void SetText(this IElementContainer container, string textFieldIdOrNameOrValue, string text, bool append = false) public static void SetTextByClass(this IElementContainer container, string textFieldClass, string text, bool append = false) public static void SetTextByText(this IElementContainer container, string textFieldText, string text, bool append = false) public static void SelectValue(this IElementContainer container, Constraint constraint, string value) public static void SelectValue(this IElementContainer container, string selectListIdOrNameOrValue, string value) public static void SelectValueByText(this IElementContainer container, Constraint constraint, string text) public static void SelectValueByText(this IElementContainer container, string selectListIdOrNameOrValue, string text) (new) public static void Check(this IElementContainer container, string checkBoxIdOrNameOrValue, bool check) (new) public static void Check(this IElementContainer container, Constraint constraint, bool check) (new) public static void CheckByClass(this IElementContainer container, string checkBoxClass, bool check)

3. Extensions for Element:

(updated) public static bool IsVisible<TElement>(this TElement element, bool assert = false, bool assertExpectedValue = true) where TElement : Element

public static bool IsEnabled<TElement>(this TElement element, bool assert = false, bool assertExpectedValue = true) where TElement : Element

(experimental) public static void Action<TElement>(this TElement element, Action<TElement> action, Expression<Func<TElement, bool>> condition) where TElement : Element

(new) public static bool WaitUntilExists<TElement>(this TElement element, bool throwExceptionAfterTimeout) where TElement : Element

(new) public static bool WaitUntilExists<TElement>(this TElement element, int timeoutInMilliseconds = 1000, bool throwExceptionAfterTimeout = true) where TElement : Element

(new) public static bool WaitUntilEnabled<TElement>(this TElement element) where TElement : Element

(new) public static bool WaitUntilEnabled<TElement>(this TElement element, bool throwExceptionAfterTimeout) where TElement : Element

(new) public static bool WaitUntilEnabled<TElement>(this TElement element, int timeoutInMilliseconds = 1000, bool throwExceptionAfterTimeout = true) where TElement : Element

(new) public static bool Wait<TElement>(this TElement element, int timeoutInMilliseconds) where TElement : Element

(new) public static bool Wait<TElement>(this TElement element, Expression<Func<TElement, bool>> condition) where TElement : Element

(new) public static bool Wait<TElement>(this TElement element, Expression<Func<TElement, bool>> condition, bool throwExceptionAfterTimeout) where TElement : Element

(new) public static bool Wait<TElement>(this TElement element, Expression<Func<TElement, bool>> condition, 
int timeoutInMilliseconds = 1000, bool throwExceptionAfterTimeout = true) where TElement : Element (new) public static bool WaitUntilVisible<TElement>(this TElement element) where TElement : Element (new) public static bool WaitUntilVisible<TElement>(this TElement element, bool throwExceptionAfterTimeout) where TElement : Element (new) public static bool WaitUntilVisible<TElement>(this TElement element, int timeoutInMilliseconds = 1000, bool throwExceptionAfterTimeout = true) where TElement : Element
IsVisible extension will now check also if parents are visible or not. Note: max visibility search scope can be adjusted in Settings if needed.
Action is an experimental function that will fire action if the condition is met. Can be useful is some situations (async?), but I had no time to test it properly. Sample usage: link.Action(x => x.Click(), x => x.WaitUntilEnabled());

4. Extensions for TextField:

public static void SetText(this TextField textField, string text, bool append = false)

5. WatiNTestBase is a base class class that contains methods for test SetUp (TestInitialize in MSTest and Parameterless constructor for xUnit), test TearDown (TestCleanup in MSTest and Dispose method for xUnit), all methods from IElementContainer section where CurrentBrowser is used as a container.

6. Settings: User can define default behaviors here. For example: when you will not specify parameter assert in Get method, the assert will be the same as in Settings. Things that can be configured/set here:

  • Assert - value indicating whether extension methods should assert element existence or not. Default: true
  • DefaultBrowser (Default: IE)
  • WaitUntilEnabledTimeout (Default: 2500ms)
  • WaitUntilVisibleTimeout (Default: 2500ms)
  • WaitTimeout (Default: 2500ms)
  • WaitUntilExistsTimeout (Default: 2500ms)
  • CreateIEInNewProcess (Default: false)
  • MaxVisibilitySearchScope (Default: 5)

Have fun using them!

P.S. If you will have some ideas about new features or existing ones, do not hesitate to inform me about that :)

public static void FireJSEvent<TJSEvent>(this Browser browser, string elementId) where TJSEvent : IJSEvent

Last edited Jul 1, 2012 at 5:43 PM by ppekrol, version 16