From f6651b5d335240eac9537755afe1a8cac61c78d1 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sat, 31 Aug 2024 07:03:25 -0500 Subject: [PATCH] IgnoreIfNotFound --- .../Browsing/Models/ElementLocatingArgs.cs | 1 + .../Browsing/Models/MessageInfo.cs | 2 +- .../PlaywrightDriver/PlaywrightInstance.cs | 26 +++++++++---------- .../PlaywrightWebDriver.LocateElement.cs | 13 ++++++++-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs index 9dd5334a..53d72520 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs @@ -26,6 +26,7 @@ public class ElementLocatingArgs public bool Parent { get; set; } public bool FailIfMultiple { get; set; } + public bool IgnoreIfNotFound { get; set; } /// /// Draw outline around the element diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/MessageInfo.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/MessageInfo.cs index 48c1253a..baa404d5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/MessageInfo.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/MessageInfo.cs @@ -12,5 +12,5 @@ public class MessageInfo : ICacheKey public string StepId { get; set; } = Guid.NewGuid().ToString(); public string GetCacheKey() - => $"{nameof(MessageInfo)}-{AgentId}-{UserId}-{ContextId}-{MessageId}-{TaskId}"; + => $"{nameof(MessageInfo)}-{ContextId}"; } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index 6fb84817..035c1f4e 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -164,22 +164,22 @@ public class PlaywrightInstance : IDisposable Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}"); } + var result = new WebPageResponseData + { + Url = e.Url.ToLower(), + PostData = e.Request?.PostData ?? string.Empty, + ResponseData = JsonSerializer.Serialize(json), + ResponseInMemory = responseInMemory + }; + + if (responseContainer != null && responseInMemory) + { + responseContainer.Add(result); + } + var webPageResponseHooks = _services.GetServices(); foreach (var hook in webPageResponseHooks) { - var result = new WebPageResponseData - { - Url = e.Url.ToLower(), - PostData = e.Request?.PostData ?? string.Empty, - ResponseData = JsonSerializer.Serialize(json), - ResponseInMemory = responseInMemory - }; - - if (responseContainer != null && responseInMemory) - { - responseContainer.Add(result); - } - hook.OnDataFetched(message, result); } } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs index f62c230a..a39da9c0 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs @@ -68,8 +68,17 @@ public partial class PlaywrightWebDriver if (count == 0) { - result.Message = $"Can't locate element by keyword {location.Text}"; - _logger.LogError(result.Message); + if (location.IgnoreIfNotFound) + { + result.Message = $"Can't locate element by keyword {location.Text} and Ignored"; + _logger.LogWarning(result.Message); + } + else + { + result.Message = $"Can't locate element by keyword {location.Text}"; + _logger.LogError(result.Message); + } + } else if (count == 1) {