8
8
using System . Text . Json ;
9
9
using System . Text ;
10
10
using System . Threading ;
11
+ using System . Collections . ObjectModel ;
12
+ using System . Linq ;
11
13
12
14
namespace G4 . WebDriver . Remote . Uia
13
15
{
14
16
/// <summary>
15
17
/// Represents a WebDriver implementation for UI Automation (UIA), extending the <see cref="RemoteWebDriver"/>.
16
18
/// </summary>
17
- public class UiaDriver : RemoteWebDriver
19
+ public class UiaDriver : RemoteWebDriver , IUser32Driver
18
20
{
19
21
#region *** Constructors ***
20
22
/// <summary>
@@ -187,20 +189,60 @@ public UiaDriver(IWebDriverCommandInvoker invoker, SessionModel session)
187
189
PropertyNameCaseInsensitive = true
188
190
} ;
189
191
190
- /// <summary>
191
- /// Sends input scan codes to the WebDriver server.
192
- /// </summary>
193
- /// <param name="codes">The scan codes to send.</param>
192
+ /// <inheritdoc />
193
+ new public IWebElement FindElement ( By by )
194
+ {
195
+ // Send a command to the WebDriver to find an element using the specified locator strategy and value.
196
+ var response = Invoker . Invoke ( nameof ( WebDriverCommands . FindElement ) , new
197
+ {
198
+ by . Using , // The method used to locate the element (e.g., CSS selector, XPath).
199
+ by . Value // The actual value of the locator (e.g., ".button-class", "//input[@id='submit']").
200
+ } ) ;
201
+
202
+ // Extract the JSON response from the WebDriver command.
203
+ var value = ( JsonElement ) response . Value ;
204
+
205
+ // Convert the JSON response to a dictionary to access the element's details.
206
+ var element = value . ConvertToDictionary ( ) ;
207
+
208
+ // Get the element ID from the dictionary, which uniquely identifies the element within the current session.
209
+ var id = element . First ( ) . Value . ToString ( ) ;
210
+
211
+ // Return a new WebElement instance, initializing it with the WebDriver instance and the extracted element ID.
212
+ return new UiaElement ( driver : this , id ) ;
213
+ }
214
+
215
+ /// <inheritdoc />
216
+ new public IEnumerable < IWebElement > FindElements ( By by )
217
+ {
218
+ // Send a command to the WebDriver to find all elements that match the specified locator strategy and value.
219
+ var response = Invoker . Invoke ( nameof ( WebDriverCommands . FindElements ) , new
220
+ {
221
+ by . Using , // The method used to locate the elements (e.g., CSS selector, XPath).
222
+ by . Value // The actual value of the locator (e.g., ".button-class", "//input[@id='submit']").
223
+ } ) ;
224
+
225
+ // Parse the JSON response to an array of elements.
226
+ var value = ( ( JsonElement ) response . Value ) . EnumerateArray ( ) . ToArray ( ) ;
227
+
228
+ // Convert each element in the array to a dictionary to access its details,
229
+ // and create a WebElement instance for each found element.
230
+ var elements = value
231
+ . Select ( i => i . ConvertToDictionary ( ) )
232
+ . Select ( i => new UiaElement ( driver : this , id : i . First ( ) . Value . ToString ( ) ) as IWebElement )
233
+ . ToList ( ) ;
234
+
235
+ // Return a read-only collection of the found WebElement instances.
236
+ return new ReadOnlyCollection < IWebElement > ( elements ) ;
237
+ }
238
+
239
+ /// <inheritdoc />
194
240
public void SendInputs ( params string [ ] codes )
195
241
{
196
242
SendInputs ( 1 , codes ) ;
197
243
}
198
244
199
- /// <summary>
200
- /// Sends input scan codes to the WebDriver server multiple times.
201
- /// </summary>
202
- /// <param name="repeat">The number of times to repeat the input.</param>
203
- /// <param name="codes">The scan codes to send.</param>
245
+ /// <inheritdoc />
204
246
public void SendInputs ( int repeat , params string [ ] codes )
205
247
{
206
248
// Get the session ID from the WebDriver
@@ -244,20 +286,13 @@ public void SendInputs(int repeat, params string[] codes)
244
286
}
245
287
}
246
288
247
- /// <summary>
248
- /// Sends text input to the WebDriver server.
249
- /// </summary>
250
- /// <param name="text">The text to send.</param>
289
+ /// <inheritdoc />
251
290
public void SendKeys ( string text )
252
291
{
253
292
SendKeys ( repeat : 1 , text ) ;
254
293
}
255
294
256
- /// <summary>
257
- /// Sends each character of the specified text as a key input with a delay between each keystroke.
258
- /// </summary>
259
- /// <param name="text">The text to be sent as key inputs.</param>
260
- /// <param name="delay">The delay to wait between sending each key.</param>
295
+ /// <inheritdoc />
261
296
public void SendKeys ( string text , TimeSpan delay )
262
297
{
263
298
// Iterate through each character in the provided text.
@@ -272,11 +307,7 @@ public void SendKeys(string text, TimeSpan delay)
272
307
}
273
308
}
274
309
275
- /// <summary>
276
- /// Sends text input to the WebDriver server multiple times.
277
- /// </summary>
278
- /// <param name="repeat">The number of times to repeat the input.</param>
279
- /// <param name="text">The text to send.</param>
310
+ /// <inheritdoc />
280
311
public void SendKeys ( int repeat , string text )
281
312
{
282
313
// Get the session ID from the WebDriver
0 commit comments