Skip to content

Commit

Permalink
Streamlining .NET Actions class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimevans committed Mar 12, 2018
1 parent 4844292 commit 4defda8
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions dotnet/src/webdriver/Interactions/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 4defda8

Please sign in to comment.