add error handler for actiononelement util
This commit is contained in:
parent
dc0c2ca8ad
commit
74d38a5132
|
|
@ -19,44 +19,52 @@ public class UtilWebActionOnElementFn : IFunctionCallback
|
|||
{
|
||||
var locatorArgs = JsonSerializer.Deserialize<ElementLocatingArgs>(message.FunctionArgs);
|
||||
var actionArgs = JsonSerializer.Deserialize<ElementActionArgs>(message.FunctionArgs);
|
||||
if (actionArgs.Action == BroswerActionEnum.InputText)
|
||||
try
|
||||
{
|
||||
// Replace variable in input text
|
||||
if (actionArgs.Content.StartsWith("@"))
|
||||
if (actionArgs.Action == BroswerActionEnum.InputText)
|
||||
{
|
||||
var config = _services.GetRequiredService<IConfiguration>();
|
||||
var key = actionArgs.Content.Replace("@", string.Empty);
|
||||
actionArgs.Content = key.Replace(key, config[key]);
|
||||
// Replace variable in input text
|
||||
if (actionArgs.Content.StartsWith("@"))
|
||||
{
|
||||
var config = _services.GetRequiredService<IConfiguration>();
|
||||
var key = actionArgs.Content.Replace("@", string.Empty);
|
||||
actionArgs.Content = key.Replace(key, config[key]);
|
||||
}
|
||||
}
|
||||
|
||||
actionArgs.WaitTime = actionArgs.WaitTime > 0 ? actionArgs.WaitTime : 2;
|
||||
|
||||
var services = _services.CreateScope().ServiceProvider;
|
||||
var browser = services.GetRequiredService<IWebBrowser>();
|
||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||
var msg = new MessageInfo
|
||||
{
|
||||
AgentId = message.CurrentAgentId,
|
||||
MessageId = message.MessageId,
|
||||
ContextId = webDriverService.GetMessageContext(message),
|
||||
FunctionArgs = message.FunctionArgs
|
||||
};
|
||||
browser.SetServiceProvider(_services);
|
||||
var result = await browser.ActionOnElement(msg, locatorArgs, actionArgs);
|
||||
|
||||
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}.";
|
||||
|
||||
// Add Current Url info to the message
|
||||
if (actionArgs.ShowCurrentUrl)
|
||||
{
|
||||
message.Content += $" Current page url: '{result.UrlAfterAction}'.";
|
||||
}
|
||||
|
||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||
|
||||
message.Data = await browser.ScreenshotAsync(msg, path);
|
||||
|
||||
}
|
||||
|
||||
actionArgs.WaitTime = actionArgs.WaitTime > 0 ? actionArgs.WaitTime : 2;
|
||||
|
||||
var services = _services.CreateScope().ServiceProvider;
|
||||
var browser = services.GetRequiredService<IWebBrowser>();
|
||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||
var msg = new MessageInfo
|
||||
catch (Exception ex)
|
||||
{
|
||||
AgentId = message.CurrentAgentId,
|
||||
MessageId = message.MessageId,
|
||||
ContextId = webDriverService.GetMessageContext(message),
|
||||
FunctionArgs = message.FunctionArgs
|
||||
};
|
||||
browser.SetServiceProvider(_services);
|
||||
var result = await browser.ActionOnElement(msg, locatorArgs, actionArgs);
|
||||
|
||||
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}.";
|
||||
|
||||
// Add Current Url info to the message
|
||||
if (actionArgs.ShowCurrentUrl)
|
||||
{
|
||||
message.Content += $" Current page url: '{result.UrlAfterAction}'.";
|
||||
message.Data = $"{actionArgs.Action} execution failed.";
|
||||
_logger.LogError($"UtilWebActionOnElementFn exception: {ex.Message}. StackTrace: {ex.StackTrace}");
|
||||
}
|
||||
|
||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||
|
||||
message.Data = await browser.ScreenshotAsync(msg, path);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue