Skip to content

Commit 59d3a1e

Browse files
committed
Refactor Uia methods and update Simulator implementation
Simplify FindElement(s) in UiaDriver.cs and UiaElement.cs by using base class methods and wrapping results in UiaElement instances, removing manual JSON parsing and dictionary conversion. Update G4.WebDriver.Simulator.csproj to include a project reference to G4.WebDriver.Remote.Uia. Add using directive for G4.WebDriver.Remote.Uia in SimulatorDriver.cs and implement IUser32Driver interface with placeholder methods for sending inputs and keys. Implement IUser32Element interface in SimulatorElement.cs with placeholder methods for element interaction, and simplify Submit and GetScreenshot methods.
1 parent f27b1eb commit 59d3a1e

File tree

5 files changed

+100
-43
lines changed

5 files changed

+100
-43
lines changed

src/G4.WebDriver.Remote.Uia/UiaDriver.cs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -192,47 +192,23 @@ public UiaDriver(IWebDriverCommandInvoker invoker, SessionModel session)
192192
/// <inheritdoc />
193193
new public IWebElement FindElement(By by)
194194
{
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();
195+
// Find the element using the base class method
196+
var element = base.FindElement(by);
207197

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);
198+
// Return a new UiaElement instance with the found element ID
199+
return new UiaElement(driver: this, id: element.Id);
213200
}
214201

215202
/// <inheritdoc />
216203
new public IEnumerable<IWebElement> FindElements(By by)
217204
{
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)
205+
// Find the elements using the base class method
206+
var elements = base
207+
.FindElements(by)
208+
.Select(i => (IWebElement)new UiaElement(driver: this, id: i.Id))
233209
.ToList();
234210

235-
// Return a read-only collection of the found WebElement instances.
211+
// Return a read-only collection of the found elements
236212
return new ReadOnlyCollection<IWebElement>(elements);
237213
}
238214

src/G4.WebDriver.Remote.Uia/UiaElement.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using G4.WebDriver.Models;
33

44
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
6+
using System.Linq;
57
using System.Net.Http;
68
using System.Text.Json;
79

@@ -19,6 +21,29 @@ public class UiaElement(IWebDriver driver, string id) : WebElement(driver, id),
1921
PropertyNameCaseInsensitive = true
2022
};
2123

24+
/// <inheritdoc />
25+
new public IWebElement FindElement(By by)
26+
{
27+
// Find the element using the base class method
28+
var webElement = base.FindElement(by);
29+
30+
// Return a new UiaElement instance with the found element ID
31+
return new UiaElement(Driver, webElement.Id);
32+
}
33+
34+
/// <inheritdoc />
35+
new public IEnumerable<IWebElement> FindElements(By by)
36+
{
37+
// Find the elements using the base class method
38+
var elements = base
39+
.FindElements(by)
40+
.Select(i => (IWebElement)new UiaElement(Driver, i.Id))
41+
.ToList();
42+
43+
// Return a read-only collection of the found elements
44+
return new ReadOnlyCollection<IWebElement>(elements);
45+
}
46+
2247
/// <inheritdoc />
2348
new public string GetAttribute(string name)
2449
{

src/G4.WebDriver.Simulator/G4.WebDriver.Simulator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
<ItemGroup>
2828
<ProjectReference Include="..\G4.WebDriver.Remote.Appium\G4.WebDriver.Remote.Appium.csproj" />
29+
<ProjectReference Include="..\G4.WebDriver.Remote.Uia\G4.WebDriver.Remote.Uia.csproj" />
2930
<ProjectReference Include="..\G4.WebDriver\G4.WebDriver.csproj" />
3031
</ItemGroup>
3132

src/G4.WebDriver.Simulator/SimulatorDriver.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using G4.WebDriver.Remote.Appium;
66
using G4.WebDriver.Remote.Appium.Models;
77
using G4.WebDriver.Remote.Interactions;
8+
using G4.WebDriver.Remote.Uia;
89

910
using System;
1011
using System.Collections.Generic;
@@ -27,7 +28,8 @@ public partial class SimulatorDriver :
2728
ITakesScreenshot,
2829
IWebDriver,
2930
IGeolocation,
30-
IMobileDeviceKeyboard
31+
IMobileDeviceKeyboard,
32+
IUser32Driver
3133
{
3234
#region *** Fields ***
3335
[GeneratedRegex("(?<=top:(\\s+)?)\\d+|\\d+(?=\\))")]
@@ -475,6 +477,36 @@ public void HideKeyboard(HideKeyboardModel model)
475477
// No actual action is performed.
476478
}
477479

480+
/// <inheritdoc />
481+
public void SendInputs(params string[] codes)
482+
{
483+
// No actual action is performed.
484+
}
485+
486+
/// <inheritdoc />
487+
public void SendInputs(int repeat, params string[] codes)
488+
{
489+
// No actual action is performed.
490+
}
491+
492+
/// <inheritdoc />
493+
public void SendKeys(string text)
494+
{
495+
// No actual action is performed.
496+
}
497+
498+
/// <inheritdoc />
499+
public void SendKeys(string text, TimeSpan delay)
500+
{
501+
// No actual action is performed.
502+
}
503+
504+
/// <inheritdoc />
505+
public void SendKeys(int repeat, string text)
506+
{
507+
// No actual action is performed.
508+
}
509+
478510
/// <inheritdoc />
479511
public void ShowKeyboard()
480512
{

src/G4.WebDriver.Simulator/SimulatorElement.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using G4.WebDriver.Extensions;
33
using G4.WebDriver.Models;
44
using G4.WebDriver.Remote;
5+
using G4.WebDriver.Remote.Uia;
56

67
using System;
78
using System.Collections.Generic;
@@ -17,7 +18,7 @@ namespace G4.WebDriver.Simulator
1718
/// <summary>
1819
/// Represents a simulated WebElement for testing or no browser purposes.
1920
/// </summary>
20-
public class SimulatorElement : IWebElement, ILocatable
21+
public class SimulatorElement : IWebElement, ILocatable, IUser32Element
2122
{
2223
#region *** Fields ***
2324
private static readonly Random s_random = new();
@@ -263,20 +264,46 @@ public string GetProperty(string propertyName)
263264
return "mockJavaScriptProperty";
264265
}
265266

267+
/// <inheritdoc />
268+
public void MoveToElement()
269+
{
270+
// No actual action is performed.
271+
}
272+
273+
/// <inheritdoc />
274+
public void MoveToElement(MousePositionInputModel positionData)
275+
{
276+
// No actual action is performed.
277+
}
278+
279+
/// <inheritdoc />
280+
public void SendClick()
281+
{
282+
// No actual action is performed.
283+
}
284+
285+
/// <inheritdoc />
286+
public void SendDoubleClick()
287+
{
288+
// No actual action is performed.
289+
}
290+
266291
/// <inheritdoc />
267292
public void SendKeys(string text)
268293
{
269294
ConfirmActionOnElement(element: this);
270295
Attributes["value"] = text;
271296
}
272297

298+
/// <inheritdoc />
299+
public void SetFocus()
300+
{
301+
// No actual action is performed.
302+
}
303+
273304
/// <summary>
274305
/// Submits the WebElement (mock method - does not perform any action).
275306
/// </summary>
276-
/// <remarks>
277-
/// This method is designed to be an instance method, and it is intentionally left as a mock method
278-
/// without performing any action. Consider the specific requirements before modifying its behavior.
279-
/// </remarks>
280307
[SuppressMessage(
281308
category: "Performance",
282309
checkId: "CA1822:Mark members as static",
@@ -292,10 +319,6 @@ public void Submit()
292319
/// Captures a screenshot of the current WebElement.
293320
/// </summary>
294321
/// <returns>A <see cref="ScreenshotModel"/> containing the screenshot in Base64 format and its raw byte representation.</returns>
295-
/// <remarks>
296-
/// This method is designed to be an instance method, and it is intentionally left as a mock method
297-
/// without using instance members. Consider the specific requirements before modifying its behavior.
298-
/// </remarks>
299322
public ScreenshotModel GetScreenshot() => new SimulatorScreenshot().GetScreenshot();
300323

301324
// Confirms whether an action can be performed on the WebElement.

0 commit comments

Comments
 (0)