1- using FlaUI . WebDriver . Models ;
1+ using FlaUI . Core . Input ;
2+ using FlaUI . Core . WindowsAPI ;
3+ using FlaUI . WebDriver . Models ;
4+ using FlaUI . WebDriver . Services ;
5+ using Microsoft . AspNetCore . Authentication . OAuth . Claims ;
26using Microsoft . AspNetCore . Mvc ;
37using System . Diagnostics ;
8+ using System . Drawing ;
9+ using System . Text . Json ;
410
511namespace FlaUI . WebDriver . Controllers
612{
@@ -10,10 +16,12 @@ public class ExecuteController : ControllerBase
1016 {
1117 private readonly ILogger < ExecuteController > _logger ;
1218 private readonly ISessionRepository _sessionRepository ;
19+ private readonly IWindowsExtensionService _windowsExtensionService ;
1320
14- public ExecuteController ( ISessionRepository sessionRepository , ILogger < ExecuteController > logger )
21+ public ExecuteController ( ISessionRepository sessionRepository , IWindowsExtensionService windowsExtensionService , ILogger < ExecuteController > logger )
1522 {
1623 _sessionRepository = sessionRepository ;
24+ _windowsExtensionService = windowsExtensionService ;
1725 _logger = logger ;
1826 }
1927
@@ -25,21 +33,37 @@ public async Task<ActionResult> ExecuteScript([FromRoute] string sessionId, [Fro
2533 {
2634 case "powerShell" :
2735 return await ExecutePowerShellScript ( session , executeScriptRequest ) ;
36+ case "windows: keys" :
37+ return await ExecuteWindowsKeysScript ( session , executeScriptRequest ) ;
38+ case "windows: click" :
39+ return await ExecuteWindowsClickScript ( session , executeScriptRequest ) ;
40+ case "windows: hover" :
41+ return await ExecuteWindowsHoverScript ( session , executeScriptRequest ) ;
2842 default :
2943 throw WebDriverResponseException . UnsupportedOperation ( "Only 'powerShell' scripts are supported" ) ;
3044 }
3145 }
46+
3247 private async Task < ActionResult > ExecutePowerShellScript ( Session session , ExecuteScriptRequest executeScriptRequest )
3348 {
3449 if ( executeScriptRequest . Args . Count != 1 )
3550 {
3651 throw WebDriverResponseException . InvalidArgument ( $ "Expected an array of exactly 1 arguments for the PowerShell script, but got { executeScriptRequest . Args . Count } arguments") ;
3752 }
3853 var powerShellArgs = executeScriptRequest . Args [ 0 ] ;
39- if ( ! powerShellArgs . TryGetValue ( "command" , out var powerShellCommand ) )
54+ if ( ! powerShellArgs . TryGetProperty ( "command" , out var powerShellCommandJson ) )
4055 {
4156 throw WebDriverResponseException . InvalidArgument ( "Expected a \" command\" property of the first argument for the PowerShell script" ) ;
4257 }
58+ if ( powerShellCommandJson . ValueKind != JsonValueKind . String )
59+ {
60+ throw WebDriverResponseException . InvalidArgument ( $ "Powershell \" command\" property must be a string") ;
61+ }
62+ string ? powerShellCommand = powerShellCommandJson . GetString ( ) ;
63+ if ( string . IsNullOrEmpty ( powerShellCommand ) )
64+ {
65+ throw WebDriverResponseException . InvalidArgument ( $ "Powershell \" command\" property must be non-empty") ;
66+ }
4367
4468 _logger . LogInformation ( "Executing PowerShell command {Command} (session {SessionId})" , powerShellCommand , session . SessionId ) ;
4569
@@ -68,6 +92,72 @@ private async Task<ActionResult> ExecutePowerShellScript(Session session, Execut
6892 return WebDriverResult . Success ( result ) ;
6993 }
7094
95+ private async Task < ActionResult > ExecuteWindowsClickScript ( Session session , ExecuteScriptRequest executeScriptRequest )
96+ {
97+ if ( executeScriptRequest . Args . Count != 1 )
98+ {
99+ throw WebDriverResponseException . InvalidArgument ( $ "Expected an array of exactly 1 arguments for the windows: click script, but got { executeScriptRequest . Args . Count } arguments") ;
100+ }
101+ var action = JsonSerializer . Deserialize < WindowsClickScript > ( executeScriptRequest . Args [ 0 ] , new JsonSerializerOptions ( ) { PropertyNamingPolicy = JsonNamingPolicy . CamelCase } ) ;
102+ if ( action == null )
103+ {
104+ throw WebDriverResponseException . InvalidArgument ( "Action cannot be null" ) ;
105+ }
106+ await _windowsExtensionService . ExecuteClickScript ( session , action ) ;
107+ return WebDriverResult . Success ( ) ;
108+ }
109+
110+ private async Task < ActionResult > ExecuteWindowsHoverScript ( Session session , ExecuteScriptRequest executeScriptRequest )
111+ {
112+ if ( executeScriptRequest . Args . Count != 1 )
113+ {
114+ throw WebDriverResponseException . InvalidArgument ( $ "Expected an array of exactly 1 arguments for the windows: hover script, but got { executeScriptRequest . Args . Count } arguments") ;
115+ }
116+ var action = JsonSerializer . Deserialize < WindowsHoverScript > ( executeScriptRequest . Args [ 0 ] , new JsonSerializerOptions ( ) { PropertyNamingPolicy = JsonNamingPolicy . CamelCase } ) ;
117+ if ( action == null )
118+ {
119+ throw WebDriverResponseException . InvalidArgument ( "Action cannot be null" ) ;
120+ }
121+ await _windowsExtensionService . ExecuteHoverScript ( session , action ) ;
122+ return WebDriverResult . Success ( ) ;
123+ }
124+
125+ private async Task < ActionResult > ExecuteWindowsKeysScript ( Session session , ExecuteScriptRequest executeScriptRequest )
126+ {
127+ if ( executeScriptRequest . Args . Count != 1 )
128+ {
129+ throw WebDriverResponseException . InvalidArgument ( $ "Expected an array of exactly 1 arguments for the windows: keys script, but got { executeScriptRequest . Args . Count } arguments") ;
130+ }
131+ var windowsKeysArgs = executeScriptRequest . Args [ 0 ] ;
132+ if ( ! windowsKeysArgs . TryGetProperty ( "actions" , out var actionsJson ) )
133+ {
134+ throw WebDriverResponseException . InvalidArgument ( "Expected a \" actions\" property of the first argument for the windows: keys script" ) ;
135+ }
136+ session . CurrentWindow . FocusNative ( ) ;
137+ if ( actionsJson . ValueKind == JsonValueKind . Array )
138+ {
139+ var actions = JsonSerializer . Deserialize < List < WindowsKeyScript > > ( actionsJson , new JsonSerializerOptions ( ) { PropertyNamingPolicy = JsonNamingPolicy . CamelCase } ) ;
140+ if ( actions == null )
141+ {
142+ throw WebDriverResponseException . InvalidArgument ( "Argument \" actions\" cannot be null" ) ;
143+ }
144+ foreach ( var action in actions )
145+ {
146+ await _windowsExtensionService . ExecuteKeyScript ( session , action ) ;
147+ }
148+ }
149+ else
150+ {
151+ var action = JsonSerializer . Deserialize < WindowsKeyScript > ( actionsJson , new JsonSerializerOptions ( ) { PropertyNamingPolicy = JsonNamingPolicy . CamelCase } ) ;
152+ if ( action == null )
153+ {
154+ throw WebDriverResponseException . InvalidArgument ( "Action cannot be null" ) ;
155+ }
156+ await _windowsExtensionService . ExecuteKeyScript ( session , action ) ;
157+ }
158+ return WebDriverResult . Success ( ) ;
159+ }
160+
71161 private Session GetSession ( string sessionId )
72162 {
73163 var session = _sessionRepository . FindById ( sessionId ) ;
0 commit comments