Streamlining .NET Actions class
The Actions class will now throw an exception on instantiation if the
IWebDriver instance passed into the constructor does not also implement
IActionExecutor. This will help folks who wrap the standard WebDriver
driver classes to be able to continue to work with the Actions class.
@@ -46,13 +46,13 @@ public enum MoveToElementOffsetOrigin
public class Actions : IAction
{
private readonly TimeSpan DefaultMouseMoveDuration = TimeSpan.FromMilliseconds(250);
- private IWebDriver driver;
private ActionBuilder actionBuilder = new ActionBuilder();
private PointerInputDevice defaultMouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
private KeyInputDevice defaultKeyboard = new KeyInputDevice("default keyboard");
private IKeyboard keyboard;
private IMouse mouse;
+ private IActionExecutor actionExecutor;
private CompositeAction action = new CompositeAction();
/// <summary>
@@ -61,31 +61,22 @@ public class Actions : IAction
/// <param name="driver">The <see cref="IWebDriver"/> object on which the actions built will be performed.</param>
public Actions(IWebDriver driver)
{
- this.driver = driver;
- IHasInputDevices inputDevicesDriver = driver as IHasInputDevices;
+ //this.driver = driver;
+ IHasInputDevices inputDevicesDriver = GetDriverAs<IHasInputDevices>(driver);
if (inputDevicesDriver == null)
{
- IWrapsDriver wrapper = driver as IWrapsDriver;
- while (wrapper != null)
- {
- inputDevicesDriver = wrapper.WrappedDriver as IHasInputDevices;
- if (inputDevicesDriver != null)
- {
- this.driver = wrapper.WrappedDriver;
- break;
- }
-
- wrapper = wrapper.WrappedDriver as IWrapsDriver;
- }
+ throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasInputDevices.", "driver");
}
- if (inputDevicesDriver == null)
+ IActionExecutor actionExecutor = GetDriverAs<IActionExecutor>(driver);
+ if (actionExecutor == null)
{
- throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasInputDevices.", "driver");
+ throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IActionExecutor.", "driver");
}
this.keyboard = inputDevicesDriver.Keyboard;
this.mouse = inputDevicesDriver.Mouse;
+ this.actionExecutor = actionExecutor;
}
/// <summary>
@@ -436,10 +427,9 @@ public IAction Build()
/// </summary>
public void Perform()
{
- IActionExecutor actionExecutor = this.driver as IActionExecutor;
- if (actionExecutor.IsActionExecutor)
+ if (this.actionExecutor.IsActionExecutor)
{
- actionExecutor.PerformActions(this.actionBuilder.ToActionSequenceList());
+ this.actionExecutor.PerformActions(this.actionBuilder.ToActionSequenceList());
}
else
{
@@ -491,5 +481,27 @@ protected void AddAction(IAction actionToAdd)
{
this.action.AddAction(actionToAdd);
}
+
+ private T GetDriverAs<T>(IWebDriver driver) where T : class
+ {
+ T driverAsType = driver as T;
+ if (driverAsType == null)
+ {
+ IWrapsDriver wrapper = driver as IWrapsDriver;
+ while (wrapper != null)
+ {
+ driverAsType = wrapper.WrappedDriver as T;
+ if (driverAsType != null)
+ {
+ driver = wrapper.WrappedDriver;
+ break;
+ }
+
+ wrapper = wrapper.WrappedDriver as IWrapsDriver;
+ }
+ }
+
+ return driverAsType;
+ }
}
}
