1- using G4 . WebDriver . Remote ;
1+ using G4 . WebDriver . Models ;
2+ using G4 . WebDriver . Remote ;
23
34using System . Collections . Generic ;
45using System . Net . Http ;
5- using System . Reflection ;
66using System . Text . Json ;
77
8- #pragma warning disable S3011 // Necessary to interact with internal fields for sending native actions.
98namespace G4 . WebDriver . Extensions
109{
1110 /// <summary>
@@ -29,7 +28,7 @@ public static class UiaExtensions
2928 /// <param name="element">The web element to get the attribute from.</param>
3029 /// <param name="name">The name of the attribute.</param>
3130 /// <returns>The attribute value as a string.</returns>
32- public static string GetUiaAttribute ( this IWebElement element , string name )
31+ public static string GetUser32Attribute ( this IWebElement element , string name )
3332 {
3433 // Get the session ID and element ID from the web element
3534 var ( sessionId , elementId ) = GetRouteData ( element ) ;
@@ -63,11 +62,61 @@ public static string GetUiaAttribute(this IWebElement element, string name)
6362 return isValue ? $ "{ value } " : string . Empty ;
6463 }
6564
65+ /// <summary>
66+ /// Moves the mouse pointer over the specified web element using default mouse position data.
67+ /// </summary>
68+ /// <param name="element">The target web element over which the mouse will be moved.</param>
69+ /// <remarks>This method is designed for Windows environments only, utilizing the user32.dll for native mouse operations.</remarks>
70+ public static void MoveUser32Mouse ( this IWebElement element )
71+ {
72+ // Call the overload with a default MousePositionInputModel.
73+ MoveUser32Mouse ( element , new MousePositionInputModel ( ) ) ;
74+ }
75+
76+ /// <summary>
77+ /// Moves the mouse pointer over the specified web element using the provided mouse position data.
78+ /// </summary>
79+ /// <param name="element">The target web element over which the mouse will be moved.</param>
80+ /// <param name="positionData">The mouse position data to use when moving the mouse.</param>
81+ /// <remarks>This method is designed for Windows environments only, utilizing the user32.dll for native mouse operations.</remarks>
82+ public static void MoveUser32Mouse ( this IWebElement element , MousePositionInputModel positionData )
83+ {
84+ // Retrieve the session ID and element ID from the web element for routing.
85+ var ( sessionId , elementId ) = GetRouteData ( element ) ;
86+
87+ // Retrieve the WebDriver instance associated with the element.
88+ var driver = element . Driver ;
89+
90+ // Get the URI of the remote server from the command executor.
91+ var url = GetRemoteServerUri ( driver ) ;
92+
93+ // Construct the request URI for the native mouse move command.
94+ var requestUri = $ "{ url } /session/{ sessionId } /user32/element/{ elementId } /mouse/move";
95+
96+ // Serialize the mouse position input model to JSON.
97+ var jsonData = JsonSerializer . Serialize ( positionData , JsonSerializerOptions ) ;
98+
99+ // Create the HTTP content using the serialized JSON, specifying the content type.
100+ var content = new StringContent ( jsonData , System . Text . Encoding . UTF8 , "application/json" ) ;
101+
102+ // Construct the HTTP POST request with the target URI and content.
103+ var request = new HttpRequestMessage ( HttpMethod . Post , requestUri )
104+ {
105+ Content = content
106+ } ;
107+
108+ // Send the HTTP request to move the mouse.
109+ var response = HttpClient . Send ( request ) ;
110+
111+ // Ensure that the HTTP response indicates a successful request.
112+ response . EnsureSuccessStatusCode ( ) ;
113+ }
114+
66115 /// <summary>
67116 /// Sends a native click command to a web element.
68117 /// </summary>
69118 /// <param name="element">The web element to click.</param>
70- public static void SendNativeClick ( this IWebElement element )
119+ public static void SendUser32Click ( this IWebElement element )
71120 {
72121 // Get the session ID and element ID from the web element
73122 var ( sessionId , elementId ) = GetRouteData ( element ) ;
@@ -95,7 +144,7 @@ public static void SendNativeClick(this IWebElement element)
95144 /// Sends a native double-click command to a web element.
96145 /// </summary>
97146 /// <param name="element">The web element to double-click.</param>
98- public static void SendNativeDoubleClick ( this IWebElement element )
147+ public static void SendUser32DoubleClick ( this IWebElement element )
99148 {
100149 // Get the session ID and element ID from the web element
101150 var ( sessionId , elementId ) = GetRouteData ( element ) ;
@@ -123,7 +172,7 @@ public static void SendNativeDoubleClick(this IWebElement element)
123172 /// Sets focus on a web element.
124173 /// </summary>
125174 /// <param name="element">The web element to set focus on.</param>
126- public static void SetFocus ( this IWebElement element )
175+ public static void SetUser32Focus ( this IWebElement element )
127176 {
128177 // Get the session ID and element ID from the web element
129178 var ( sessionId , elementId ) = GetRouteData ( element ) ;
@@ -157,8 +206,6 @@ private static string GetRemoteServerUri(IWebDriver driver)
157206 // Gets the session ID and element ID from a web element.
158207 private static ( string SessionId , string ElementId ) GetRouteData ( IWebElement element )
159208 {
160- const BindingFlags bindingFlags = BindingFlags . Instance | BindingFlags . NonPublic ;
161-
162209 // Get the WebDriver instance from the web element
163210 var driver = element . Driver ;
164211
@@ -169,4 +216,4 @@ private static (string SessionId, string ElementId) GetRouteData(IWebElement ele
169216 return ( sessionId , element . Id ) ;
170217 }
171218 }
172- }
219+ }
0 commit comments