From b7dadd3159c9f71586e91ec5b6b4355fb467ccc5 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Tue, 13 Aug 2024 22:44:53 -0500 Subject: [PATCH] ResponseInMemory --- .../Browsing/IWebPageResponseHook.cs | 4 ++-- .../Browsing/Models/PageActionArgs.cs | 6 +++++ .../Browsing/Models/WebPageResponseData.cs | 10 +++++++++ .../Browsing/Models/WebPageResponseFilter.cs | 7 ++++++ .../PlaywrightDriver/PlaywrightInstance.cs | 22 +++++++++++++++++-- .../PlaywrightWebDriver.GoToPage.cs | 12 ++++++++-- 6 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseData.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseFilter.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebPageResponseHook.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebPageResponseHook.cs index a7e33b22..4e32117b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebPageResponseHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebPageResponseHook.cs @@ -4,6 +4,6 @@ namespace BotSharp.Abstraction.Browsing; public interface IWebPageResponseHook { - void OnDataFetched(MessageInfo message, string url, string postData, string responsData); - T? GetResponse(MessageInfo message, string url, string? queryParameter = null); + void OnDataFetched(MessageInfo message, WebPageResponseData response); + T? GetResponse(MessageInfo message, WebPageResponseFilter filter); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs index 098b9538..d260a09d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs @@ -28,6 +28,12 @@ public class PageActionArgs /// public string[]? IncludeResponseUrls { get; set; } + /// + /// If set to true, the response will be stored in memory + /// + public bool ResponseInMemory { get; set; } = false; + public List? ResponseContainer { get; set; } + public bool UseExistingPage { get; set; } = false; public bool WaitForNetworkIdle { get; set; } = true; diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseData.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseData.cs new file mode 100644 index 00000000..1d03b0b7 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseData.cs @@ -0,0 +1,10 @@ +namespace BotSharp.Abstraction.Browsing.Models; + +public class WebPageResponseData +{ + public string Url { get; set; } = null!; + public string PostData { get; set; } = null!; + public string ResponseData { get; set; } = null!; + public bool ResponseInMemory { get; set; } + public DateTime CreatedAt { get; set; } = DateTime.UtcNow; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseFilter.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseFilter.cs new file mode 100644 index 00000000..cd97a01d --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/WebPageResponseFilter.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Abstraction.Browsing.Models; + +public class WebPageResponseFilter +{ + public string Url { get; set; } = null!; + public string[]? QueryParameters { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index 13b52b3e..6fb84817 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -122,7 +122,12 @@ public class PlaywrightInstance : IDisposable return _contexts[ctxId]; } - public async Task NewPage(MessageInfo message, bool enableResponseCallback = false, string[]? excludeResponseUrls = null, string[]? includeResponseUrls = null) + public async Task NewPage(MessageInfo message, + bool enableResponseCallback = false, + bool responseInMemory = false, + List? responseContainer = null, + string[]? excludeResponseUrls = null, + string[]? includeResponseUrls = null) { var context = await GetContext(message.ContextId); var page = await context.NewPageAsync(); @@ -162,7 +167,20 @@ public class PlaywrightInstance : IDisposable var webPageResponseHooks = _services.GetServices(); foreach (var hook in webPageResponseHooks) { - hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json)); + 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); } } catch (ObjectDisposedException ex) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs index 7b03b57a..a7e5f27f 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -11,6 +11,8 @@ public partial class PlaywrightWebDriver var page = args.UseExistingPage ? _instance.GetPage(message.ContextId, pattern: args.Url) : await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback, + responseInMemory: args.ResponseInMemory, + responseContainer: args.ResponseContainer, excludeResponseUrls: args.ExcludeResponseUrls, includeResponseUrls: args.IncludeResponseUrls); @@ -25,14 +27,20 @@ public partial class PlaywrightWebDriver if (args.UseExistingPage && args.OpenNewTab && page != null && page.Url == "about:blank") { - page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback, + page = await _instance.NewPage(message, + enableResponseCallback: args.EnableResponseCallback, + responseInMemory: args.ResponseInMemory, + responseContainer: args.ResponseContainer, excludeResponseUrls: args.ExcludeResponseUrls, includeResponseUrls: args.IncludeResponseUrls); } if (page == null) { - page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback, + page = await _instance.NewPage(message, + enableResponseCallback: args.EnableResponseCallback, + responseInMemory: args.ResponseInMemory, + responseContainer: args.ResponseContainer, excludeResponseUrls: args.ExcludeResponseUrls, includeResponseUrls: args.IncludeResponseUrls); }