@@ -19,44 +19,52 @@ public async Task<bool> Execute(RoleDialogModel message)
1919 {
2020 var locatorArgs = JsonSerializer . Deserialize < ElementLocatingArgs > ( message . FunctionArgs ) ;
2121 var actionArgs = JsonSerializer . Deserialize < ElementActionArgs > ( message . FunctionArgs ) ;
22- if ( actionArgs . Action == BroswerActionEnum . InputText )
22+ try
2323 {
24- // Replace variable in input text
25- if ( actionArgs . Content . StartsWith ( "@" ) )
24+ if ( actionArgs . Action == BroswerActionEnum . InputText )
2625 {
27- var config = _services . GetRequiredService < IConfiguration > ( ) ;
28- var key = actionArgs . Content . Replace ( "@" , string . Empty ) ;
29- actionArgs . Content = key . Replace ( key , config [ key ] ) ;
26+ // Replace variable in input text
27+ if ( actionArgs . Content . StartsWith ( "@" ) )
28+ {
29+ var config = _services . GetRequiredService < IConfiguration > ( ) ;
30+ var key = actionArgs . Content . Replace ( "@" , string . Empty ) ;
31+ actionArgs . Content = key . Replace ( key , config [ key ] ) ;
32+ }
3033 }
31- }
3234
33- actionArgs . WaitTime = actionArgs . WaitTime > 0 ? actionArgs . WaitTime : 2 ;
35+ actionArgs . WaitTime = actionArgs . WaitTime > 0 ? actionArgs . WaitTime : 2 ;
3436
35- var services = _services . CreateScope ( ) . ServiceProvider ;
36- var browser = services . GetRequiredService < IWebBrowser > ( ) ;
37- var webDriverService = _services . GetRequiredService < WebDriverService > ( ) ;
38- var msg = new MessageInfo
39- {
40- AgentId = message . CurrentAgentId ,
41- MessageId = message . MessageId ,
42- ContextId = webDriverService . GetMessageContext ( message ) ,
43- FunctionArgs = message . FunctionArgs
44- } ;
45- browser . SetServiceProvider ( _services ) ;
46- var result = await browser . ActionOnElement ( msg , locatorArgs , actionArgs ) ;
47-
48- message . Content = $ "{ actionArgs . Action } executed { ( result . IsSuccess ? "success" : "failed" ) } .";
49-
50- // Add Current Url info to the message
51- if ( actionArgs . ShowCurrentUrl )
52- {
53- message . Content += $ " Current page url: '{ result . UrlAfterAction } '.";
54- }
37+ var services = _services . CreateScope ( ) . ServiceProvider ;
38+ var browser = services . GetRequiredService < IWebBrowser > ( ) ;
39+ var webDriverService = _services . GetRequiredService < WebDriverService > ( ) ;
40+ var msg = new MessageInfo
41+ {
42+ AgentId = message . CurrentAgentId ,
43+ MessageId = message . MessageId ,
44+ ContextId = webDriverService . GetMessageContext ( message ) ,
45+ FunctionArgs = message . FunctionArgs
46+ } ;
47+ browser . SetServiceProvider ( _services ) ;
48+ var result = await browser . ActionOnElement ( msg , locatorArgs , actionArgs ) ;
49+
50+ message . Content = $ "{ actionArgs . Action } executed { ( result . IsSuccess ? "success" : "failed" ) } .";
5551
56- var path = webDriverService . GetScreenshotFilePath ( message . MessageId ) ;
52+ // Add Current Url info to the message
53+ if ( actionArgs . ShowCurrentUrl )
54+ {
55+ message . Content += $ " Current page url: '{ result . UrlAfterAction } '.";
56+ }
57+
58+ var path = webDriverService . GetScreenshotFilePath ( message . MessageId ) ;
5759
58- message . Data = await browser . ScreenshotAsync ( msg , path ) ;
60+ message . Data = await browser . ScreenshotAsync ( msg , path ) ;
5961
62+ }
63+ catch ( Exception ex )
64+ {
65+ message . Data = $ "{ actionArgs . Action } execution failed.";
66+ _logger . LogError ( $ "UtilWebActionOnElementFn exception: { ex . Message } . StackTrace: { ex . StackTrace } ") ;
67+ }
6068 return true ;
6169 }
6270}
0 commit comments