Skip to content
Browse files

refactor InputSetter into individual classes - one per control type

  • Loading branch information...
1 parent ae186e6 commit e4db37048b497ec1e2182d4e57bcc4b22baba1cd @handcraftsman committed Mar 3, 2010
Showing with 47 additions and 47 deletions.
  1. +31 −43 decuit/InputSetter.cs
  2. +16 −4 decuit/SetExpression.cs
  3. BIN decuit/bin/Debug/decuit.zip
View
74 decuit/InputSetter.cs
@@ -13,69 +13,57 @@
// limitations under the License.
using System;
-using System.Collections.Generic;
-using System.Linq;
using FluentWebUITesting.Extensions;
-using JetBrains.Annotations;
-
using NUnit.Framework;
using WatiN.Core;
namespace gar3t.decuit
{
- public class InputSetter
+ public class TextBoxSetter : IInputSetter
{
- private static readonly List<InputSetter> _setters = new List<InputSetter>();
-
- static InputSetter()
+ public bool IsMatch(Browser browser, string id)
{
- new InputSetter(
- (b, id) => b.TextBoxWithId(id).Exists().Passed,
- (b, id, textToSet) => b.TextBoxWithId(id).Text().SetValueTo(textToSet));
- new InputSetter(
- (b, id) => b.DropDownListWithId(id).Exists().Passed,
- (b, id, textToSet) =>
- {
- var dropDown = b.DropDownListWithId(id);
- var option = dropDown.OptionWithText(textToSet);
- if (option.Exists().Passed)
- {
- option.Select();
- return;
- }
- option = dropDown.OptionWithValue(textToSet);
- if (option.Exists().Passed)
- {
- option.Select();
- return;
- }
- Assert.Fail(String.Format("The drop down with id '{0}' does not have option '{1}'", id, textToSet));
- });
+ return browser.TextBoxWithId(id).Exists().Passed;
}
- private InputSetter(Func<Browser, string, bool> isMatch, Action<Browser, string, string> setText)
+ public void SetText(Browser browser, string id, string textToSet)
{
- IsMatch = isMatch;
- SetText = setText;
- _setters.Add(this);
+ browser.TextBoxWithId(id).Text().SetValueTo(textToSet);
}
+ }
- private Func<Browser, string, bool> IsMatch { get; set; }
- public Action<Browser, string, string> SetText { get; private set; }
+ public interface IInputSetter
+ {
+ bool IsMatch(Browser browser, string id);
+ void SetText(Browser browser, string id, string textToSet);
+ }
- [NotNull]
- public static InputSetter GetFor([NotNull] Browser browser, [NotNull] string id)
+ public class DropDownListSetter : IInputSetter
+ {
+ public bool IsMatch(Browser browser, string id)
{
- var setter = _setters.FirstOrDefault(x => x.IsMatch(browser, id));
- if (setter == null)
+ return browser.DropDownListWithId(id).Exists().Passed;
+ }
+
+ public void SetText(Browser browser, string id, string textToSet)
+ {
+ var dropDown = browser.DropDownListWithId(id);
+ var option = dropDown.OptionWithText(textToSet);
+ if (option.Exists().Passed)
+ {
+ option.Select();
+ return;
+ }
+ option = dropDown.OptionWithValue(textToSet);
+ if (option.Exists().Passed)
{
- throw new ArgumentOutOfRangeException("id",
- String.Format("There is no configured InputSetter for type of control with id '{0}'", id));
+ option.Select();
+ return;
}
- return setter;
+ Assert.Fail(String.Format("The drop down with id '{0}' does not have option '{1}'", id, textToSet));
}
}
}
View
20 decuit/SetExpression.cs
@@ -13,6 +13,8 @@
// limitations under the License.
using System;
+using System.Collections.Generic;
+using System.Linq;
using FluentAssert;
@@ -22,6 +24,12 @@ namespace gar3t.decuit
{
public class SetExpression
{
+ private static readonly List<IInputSetter> _setters = new List<IInputSetter>
+ {
+ new TextBoxSetter(),
+ new DropDownListSetter()
+ };
+
private readonly Browser _browser;
public SetExpression(Browser browser, string labelText)
@@ -42,10 +50,14 @@ public void To(string text)
var control = _browser.Element(Find.ById(itsLinkedControlId));
control.Exists.ShouldBeTrue(String.Format("Could not find a control with id '{0}' as referenced in For atribute of Label with text {1}", itsLinkedControlId, LabelText));
- control.Enabled.ShouldBeTrue(String.Format("Cannot set the value of control with id '{0}' because it is disabled.", itsLinkedControlId, LabelText));
-
- var inputSetter = InputSetter.GetFor(_browser, itsLinkedControlId);
- inputSetter.SetText(_browser, itsLinkedControlId, text);
+ control.Enabled.ShouldBeTrue(String.Format("Cannot set the value of control with id '{0}' because it is disabled.", itsLinkedControlId));
+
+ var setter = _setters.FirstOrDefault(x => x.IsMatch(_browser, itsLinkedControlId));
+ if (setter == null)
+ {
+ throw new ArgumentOutOfRangeException(String.Format("There is no configured InputSetter for control type with label '{0}'", LabelText));
+ }
+ setter.SetText(_browser, itsLinkedControlId, text);
}
}
}
View
BIN decuit/bin/Debug/decuit.zip
Binary file not shown.

0 comments on commit e4db370

Please sign in to comment.
Something went wrong with that request. Please try again.