@@ -31,23 +31,23 @@ public class Actions : IAction
31
31
private PointerInputDevice activePointer ;
32
32
private KeyInputDevice activeKeyboard ;
33
33
private WheelInputDevice activeWheel ;
34
- private IActionExecutor actionExecutor ;
35
34
36
35
/// <summary>
37
36
/// Initializes a new instance of the <see cref="Actions"/> class.
38
37
/// </summary>
39
38
/// <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>
40
40
public Actions ( IWebDriver driver )
41
41
: this ( driver , TimeSpan . FromMilliseconds ( 250 ) )
42
42
{
43
-
44
43
}
45
44
46
45
/// <summary>
47
46
/// Initializes a new instance of the <see cref="Actions"/> class.
48
47
/// </summary>
49
48
/// <param name="driver">The <see cref="IWebDriver"/> object on which the actions built will be performed.</param>
50
49
/// <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>
51
51
public Actions ( IWebDriver driver , TimeSpan duration )
52
52
{
53
53
IActionExecutor actionExecutor = GetDriverAs < IActionExecutor > ( driver ) ;
@@ -56,52 +56,33 @@ public Actions(IWebDriver driver, TimeSpan duration)
56
56
throw new ArgumentException ( "The IWebDriver object must implement or wrap a driver that implements IActionExecutor." , nameof ( driver ) ) ;
57
57
}
58
58
59
- this . actionExecutor = actionExecutor ;
59
+ this . ActionExecutor = actionExecutor ;
60
60
61
61
this . duration = duration ;
62
62
}
63
63
64
64
/// <summary>
65
65
/// Returns the <see cref="IActionExecutor"/> for the driver.
66
66
/// </summary>
67
- protected IActionExecutor ActionExecutor
68
- {
69
- get { return this . actionExecutor ; }
70
- }
67
+ protected IActionExecutor ActionExecutor { get ; }
71
68
72
69
/// <summary>
73
70
/// Sets the active pointer device for this Actions class.
74
71
/// </summary>
75
72
/// <param name="kind">The kind of pointer device to set as active.</param>
76
73
/// <param name="name">The name of the pointer device to set as active.</param>
77
74
/// <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>
78
76
public Actions SetActivePointer ( PointerKind kind , string name )
79
77
{
80
- IList < ActionSequence > sequences = this . actionBuilder . ToActionSequenceList ( ) ;
78
+ InputDevice device = FindDeviceById ( name ) ;
81
79
82
- InputDevice device = null ;
83
-
84
- foreach ( var sequence in sequences )
80
+ this . activePointer = device switch
85
81
{
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
+ } ;
105
86
106
87
return this ;
107
88
}
@@ -111,33 +92,17 @@ public Actions SetActivePointer(PointerKind kind, string name)
111
92
/// </summary>
112
93
/// <param name="name">The name of the keyboard device to set as active.</param>
113
94
/// <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>
114
96
public Actions SetActiveKeyboard ( string name )
115
97
{
116
- IList < ActionSequence > sequences = this . actionBuilder . ToActionSequenceList ( ) ;
98
+ InputDevice device = FindDeviceById ( name ) ;
117
99
118
- InputDevice device = null ;
119
-
120
- foreach ( var sequence in sequences )
100
+ this . activeKeyboard = device switch
121
101
{
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
+ } ;
141
106
142
107
return this ;
143
108
}
@@ -147,35 +112,36 @@ public Actions SetActiveKeyboard(string name)
147
112
/// </summary>
148
113
/// <param name="name">The name of the wheel device to set as active.</param>
149
114
/// <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>
150
116
public Actions SetActiveWheel ( string name )
151
117
{
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
+ } ;
153
126
154
- InputDevice device = null ;
127
+ return this ;
128
+ }
155
129
156
- foreach ( var sequence in sequences )
130
+ private InputDevice FindDeviceById ( string name )
131
+ {
132
+ foreach ( var sequence in this . actionBuilder . ToActionSequenceList ( ) )
157
133
{
158
134
Dictionary < string , object > actions = sequence . ToDictionary ( ) ;
159
135
160
136
string id = ( string ) actions [ "id" ] ;
161
137
162
138
if ( id == name )
163
139
{
164
- device = sequence . inputDevice ;
165
- break ;
140
+ return sequence . inputDevice ;
166
141
}
167
142
}
168
143
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 ;
179
145
}
180
146
181
147
/// <summary>
@@ -619,7 +585,7 @@ public IAction Build()
619
585
/// </summary>
620
586
public void Perform ( )
621
587
{
622
- this . actionExecutor . PerformActions ( this . actionBuilder . ToActionSequenceList ( ) ) ;
588
+ this . ActionExecutor . PerformActions ( this . actionBuilder . ToActionSequenceList ( ) ) ;
623
589
this . actionBuilder . ClearSequences ( ) ;
624
590
}
625
591
0 commit comments