@@ -46,13 +46,13 @@ public enum MoveToElementOffsetOrigin
46
46
public class Actions : IAction
47
47
{
48
48
private readonly TimeSpan DefaultMouseMoveDuration = TimeSpan . FromMilliseconds ( 250 ) ;
49
- private IWebDriver driver ;
50
49
private ActionBuilder actionBuilder = new ActionBuilder ( ) ;
51
50
private PointerInputDevice defaultMouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" ) ;
52
51
private KeyInputDevice defaultKeyboard = new KeyInputDevice ( "default keyboard" ) ;
53
52
54
53
private IKeyboard keyboard ;
55
54
private IMouse mouse ;
55
+ private IActionExecutor actionExecutor ;
56
56
private CompositeAction action = new CompositeAction ( ) ;
57
57
58
58
/// <summary>
@@ -61,31 +61,22 @@ public class Actions : IAction
61
61
/// <param name="driver">The <see cref="IWebDriver"/> object on which the actions built will be performed.</param>
62
62
public Actions ( IWebDriver driver )
63
63
{
64
- this . driver = driver ;
65
- IHasInputDevices inputDevicesDriver = driver as IHasInputDevices ;
64
+ // this.driver = driver;
65
+ IHasInputDevices inputDevicesDriver = GetDriverAs < IHasInputDevices > ( driver ) ;
66
66
if ( inputDevicesDriver == null )
67
67
{
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" ) ;
80
69
}
81
70
82
- if ( inputDevicesDriver == null )
71
+ IActionExecutor actionExecutor = GetDriverAs < IActionExecutor > ( driver ) ;
72
+ if ( actionExecutor == null )
83
73
{
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" ) ;
85
75
}
86
76
87
77
this . keyboard = inputDevicesDriver . Keyboard ;
88
78
this . mouse = inputDevicesDriver . Mouse ;
79
+ this . actionExecutor = actionExecutor ;
89
80
}
90
81
91
82
/// <summary>
@@ -436,10 +427,9 @@ public IAction Build()
436
427
/// </summary>
437
428
public void Perform ( )
438
429
{
439
- IActionExecutor actionExecutor = this . driver as IActionExecutor ;
440
- if ( actionExecutor . IsActionExecutor )
430
+ if ( this . actionExecutor . IsActionExecutor )
441
431
{
442
- actionExecutor . PerformActions ( this . actionBuilder . ToActionSequenceList ( ) ) ;
432
+ this . actionExecutor . PerformActions ( this . actionBuilder . ToActionSequenceList ( ) ) ;
443
433
}
444
434
else
445
435
{
@@ -491,5 +481,27 @@ protected void AddAction(IAction actionToAdd)
491
481
{
492
482
this . action . AddAction ( actionToAdd ) ;
493
483
}
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
+ }
494
506
}
495
507
}
0 commit comments