@@ -31,23 +31,23 @@ public class Actions : IAction
3131 private PointerInputDevice activePointer ;
3232 private KeyInputDevice activeKeyboard ;
3333 private WheelInputDevice activeWheel ;
34- private IActionExecutor actionExecutor ;
3534
3635 /// <summary>
3736 /// Initializes a new instance of the <see cref="Actions"/> class.
3837 /// </summary>
3938 /// <param name="driver">The <see cref="IWebDriver"/> object on which the actions built will be performed.</param>
39+ /// <exception cref="ArgumentException">If <paramref name="driver"/> does not implement <see cref="IActionExecutor"/>.</exception>
4040 public Actions ( IWebDriver driver )
4141 : this ( driver , TimeSpan . FromMilliseconds ( 250 ) )
4242 {
43-
4443 }
4544
4645 /// <summary>
4746 /// Initializes a new instance of the <see cref="Actions"/> class.
4847 /// </summary>
4948 /// <param name="driver">The <see cref="IWebDriver"/> object on which the actions built will be performed.</param>
5049 /// <param name="duration">How long durable action is expected to take.</param>
50+ /// <exception cref="ArgumentException">If <paramref name="driver"/> does not implement <see cref="IActionExecutor"/>.</exception>
5151 public Actions ( IWebDriver driver , TimeSpan duration )
5252 {
5353 IActionExecutor actionExecutor = GetDriverAs < IActionExecutor > ( driver ) ;
@@ -56,52 +56,33 @@ public Actions(IWebDriver driver, TimeSpan duration)
5656 throw new ArgumentException ( "The IWebDriver object must implement or wrap a driver that implements IActionExecutor." , nameof ( driver ) ) ;
5757 }
5858
59- this . actionExecutor = actionExecutor ;
59+ this . ActionExecutor = actionExecutor ;
6060
6161 this . duration = duration ;
6262 }
6363
6464 /// <summary>
6565 /// Returns the <see cref="IActionExecutor"/> for the driver.
6666 /// </summary>
67- protected IActionExecutor ActionExecutor
68- {
69- get { return this . actionExecutor ; }
70- }
67+ protected IActionExecutor ActionExecutor { get ; }
7168
7269 /// <summary>
7370 /// Sets the active pointer device for this Actions class.
7471 /// </summary>
7572 /// <param name="kind">The kind of pointer device to set as active.</param>
7673 /// <param name="name">The name of the pointer device to set as active.</param>
7774 /// <returns>A self-reference to this Actions class.</returns>
75+ /// <exception cref="InvalidOperationException">If a device with this name exists but is not a pointer.</exception>
7876 public Actions SetActivePointer ( PointerKind kind , string name )
7977 {
80- IList < ActionSequence > sequences = this . actionBuilder . ToActionSequenceList ( ) ;
78+ InputDevice device = FindDeviceById ( name ) ;
8179
82- InputDevice device = null ;
83-
84- foreach ( var sequence in sequences )
80+ this . activePointer = device switch
8581 {
86- Dictionary < string , object > actions = sequence . ToDictionary ( ) ;
87-
88- string id = ( string ) actions [ "id" ] ;
89-
90- if ( id == name )
91- {
92- device = sequence . inputDevice ;
93- break ;
94- }
95- }
96-
97- if ( device == null )
98- {
99- this . activePointer = new PointerInputDevice ( kind , name ) ;
100- }
101- else
102- {
103- this . activePointer = ( PointerInputDevice ) device ;
104- }
82+ null => new PointerInputDevice ( kind , name ) ,
83+ PointerInputDevice pointerDevice => pointerDevice ,
84+ _ => throw new InvalidOperationException ( $ "Device under the name \" { name } \" is not a pointer. Actual input type: { device . DeviceKind } ") ,
85+ } ;
10586
10687 return this ;
10788 }
@@ -111,33 +92,17 @@ public Actions SetActivePointer(PointerKind kind, string name)
11192 /// </summary>
11293 /// <param name="name">The name of the keyboard device to set as active.</param>
11394 /// <returns>A self-reference to this Actions class.</returns>
95+ /// <exception cref="InvalidOperationException">If a device with this name exists but is not a keyboard.</exception>
11496 public Actions SetActiveKeyboard ( string name )
11597 {
116- IList < ActionSequence > sequences = this . actionBuilder . ToActionSequenceList ( ) ;
98+ InputDevice device = FindDeviceById ( name ) ;
11799
118- InputDevice device = null ;
119-
120- foreach ( var sequence in sequences )
100+ this . activeKeyboard = device switch
121101 {
122- Dictionary < string , object > actions = sequence . ToDictionary ( ) ;
123-
124- string id = ( string ) actions [ "id" ] ;
125-
126- if ( id == name )
127- {
128- device = sequence . inputDevice ;
129- break ;
130- }
131- }
132-
133- if ( device == null )
134- {
135- this . activeKeyboard = new KeyInputDevice ( name ) ;
136- }
137- else
138- {
139- this . activeKeyboard = ( KeyInputDevice ) device ;
140- }
102+ null => new KeyInputDevice ( name ) ,
103+ KeyInputDevice keyDevice => keyDevice ,
104+ _ => throw new InvalidOperationException ( $ "Device under the name \" { name } \" is not a keyboard. Actual input type: { device . DeviceKind } ") ,
105+ } ;
141106
142107 return this ;
143108 }
@@ -147,35 +112,36 @@ public Actions SetActiveKeyboard(string name)
147112 /// </summary>
148113 /// <param name="name">The name of the wheel device to set as active.</param>
149114 /// <returns>A self-reference to this Actions class.</returns>
115+ /// <exception cref="InvalidOperationException">If a device with this name exists but is not a wheel.</exception>
150116 public Actions SetActiveWheel ( string name )
151117 {
152- IList < ActionSequence > sequences = this . actionBuilder . ToActionSequenceList ( ) ;
118+ InputDevice device = FindDeviceById ( name ) ;
119+
120+ this . activeWheel = device switch
121+ {
122+ null => new WheelInputDevice ( name ) ,
123+ WheelInputDevice wheelDevice => wheelDevice ,
124+ _ => throw new InvalidOperationException ( $ "Device under the name \" { name } \" is not a wheel. Actual input type: { device . DeviceKind } ") ,
125+ } ;
153126
154- InputDevice device = null ;
127+ return this ;
128+ }
155129
156- foreach ( var sequence in sequences )
130+ private InputDevice FindDeviceById ( string name )
131+ {
132+ foreach ( var sequence in this . actionBuilder . ToActionSequenceList ( ) )
157133 {
158134 Dictionary < string , object > actions = sequence . ToDictionary ( ) ;
159135
160136 string id = ( string ) actions [ "id" ] ;
161137
162138 if ( id == name )
163139 {
164- device = sequence . inputDevice ;
165- break ;
140+ return sequence . inputDevice ;
166141 }
167142 }
168143
169- if ( device == null )
170- {
171- this . activeWheel = new WheelInputDevice ( name ) ;
172- }
173- else
174- {
175- this . activeWheel = ( WheelInputDevice ) device ;
176- }
177-
178- return this ;
144+ return null ;
179145 }
180146
181147 /// <summary>
@@ -619,7 +585,7 @@ public IAction Build()
619585 /// </summary>
620586 public void Perform ( )
621587 {
622- this . actionExecutor . PerformActions ( this . actionBuilder . ToActionSequenceList ( ) ) ;
588+ this . ActionExecutor . PerformActions ( this . actionBuilder . ToActionSequenceList ( ) ) ;
623589 this . actionBuilder . ClearSequences ( ) ;
624590 }
625591
0 commit comments