Skip to content

Commit 4defda8

Browse files
committed
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.
1 parent 4844292 commit 4defda8

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

dotnet/src/webdriver/Interactions/Actions.cs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public enum MoveToElementOffsetOrigin
4646
public class Actions : IAction
4747
{
4848
private readonly TimeSpan DefaultMouseMoveDuration = TimeSpan.FromMilliseconds(250);
49-
private IWebDriver driver;
5049
private ActionBuilder actionBuilder = new ActionBuilder();
5150
private PointerInputDevice defaultMouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
5251
private KeyInputDevice defaultKeyboard = new KeyInputDevice("default keyboard");
5352

5453
private IKeyboard keyboard;
5554
private IMouse mouse;
55+
private IActionExecutor actionExecutor;
5656
private CompositeAction action = new CompositeAction();
5757

5858
/// <summary>
@@ -61,31 +61,22 @@ public class Actions : IAction
6161
/// <param name="driver">The <see cref="IWebDriver"/> object on which the actions built will be performed.</param>
6262
public Actions(IWebDriver driver)
6363
{
64-
this.driver = driver;
65-
IHasInputDevices inputDevicesDriver = driver as IHasInputDevices;
64+
//this.driver = driver;
65+
IHasInputDevices inputDevicesDriver = GetDriverAs<IHasInputDevices>(driver);
6666
if (inputDevicesDriver == null)
6767
{
68-
IWrapsDriver wrapper = driver as IWrapsDriver;
69-
while (wrapper != null)
70-
{
71-
inputDevicesDriver = wrapper.WrappedDriver as IHasInputDevices;
72-
if (inputDevicesDriver != null)
73-
{
74-
this.driver = wrapper.WrappedDriver;
75-
break;
76-
}
77-
78-
wrapper = wrapper.WrappedDriver as IWrapsDriver;
79-
}
68+
throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasInputDevices.", "driver");
8069
}
8170

82-
if (inputDevicesDriver == null)
71+
IActionExecutor actionExecutor = GetDriverAs<IActionExecutor>(driver);
72+
if (actionExecutor == null)
8373
{
84-
throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasInputDevices.", "driver");
74+
throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IActionExecutor.", "driver");
8575
}
8676

8777
this.keyboard = inputDevicesDriver.Keyboard;
8878
this.mouse = inputDevicesDriver.Mouse;
79+
this.actionExecutor = actionExecutor;
8980
}
9081

9182
/// <summary>
@@ -436,10 +427,9 @@ public IAction Build()
436427
/// </summary>
437428
public void Perform()
438429
{
439-
IActionExecutor actionExecutor = this.driver as IActionExecutor;
440-
if (actionExecutor.IsActionExecutor)
430+
if (this.actionExecutor.IsActionExecutor)
441431
{
442-
actionExecutor.PerformActions(this.actionBuilder.ToActionSequenceList());
432+
this.actionExecutor.PerformActions(this.actionBuilder.ToActionSequenceList());
443433
}
444434
else
445435
{
@@ -491,5 +481,27 @@ protected void AddAction(IAction actionToAdd)
491481
{
492482
this.action.AddAction(actionToAdd);
493483
}
484+
485+
private T GetDriverAs<T>(IWebDriver driver) where T : class
486+
{
487+
T driverAsType = driver as T;
488+
if (driverAsType == null)
489+
{
490+
IWrapsDriver wrapper = driver as IWrapsDriver;
491+
while (wrapper != null)
492+
{
493+
driverAsType = wrapper.WrappedDriver as T;
494+
if (driverAsType != null)
495+
{
496+
driver = wrapper.WrappedDriver;
497+
break;
498+
}
499+
500+
wrapper = wrapper.WrappedDriver as IWrapsDriver;
501+
}
502+
}
503+
504+
return driverAsType;
505+
}
494506
}
495507
}

0 commit comments

Comments
 (0)