From a16602a4fdaff2af7a73aaee1f19aa68837e7c32 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sun, 4 Aug 2024 22:46:41 -0500 Subject: [PATCH] Add ExcludeResponseUrls to PageActionArgs --- .../BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs | 4 ++++ .../Drivers/PlaywrightDriver/PlaywrightInstance.cs | 5 +++-- .../Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs index 661b0da4..54707cec 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs @@ -15,6 +15,10 @@ public class PageActionArgs /// This value has to be set to true if you want to get the page XHR/ Fetch responses /// public bool OpenNewTab { get; set; } = false; + /// + /// Exclude urls for XHR/ Fetch responses + /// + public string[]? ExcludeResponseUrls { get; set; } public bool UseExistingPage { get; set; } = false; public bool WaitForNetworkIdle { get; set; } = true; diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index 79eec038..0a1eea3b 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -113,7 +113,7 @@ public class PlaywrightInstance : IDisposable return _contexts[ctxId]; } - public async Task NewPage(MessageInfo message) + public async Task NewPage(MessageInfo message, string[]? excludeResponseUrls = null) { var context = await GetContext(message.ContextId); var page = await context.NewPageAsync(); @@ -128,7 +128,8 @@ public class PlaywrightInstance : IDisposable if (e.Status != 204 && e.Headers.ContainsKey("content-type") && e.Headers["content-type"].Contains("application/json") && - (e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr")) + (e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr") && + (excludeResponseUrls == null || !excludeResponseUrls.Any(url => e.Url.ToLower().Contains(url)))) { Serilog.Log.Information($"{e.Request.Method}: {e.Url}"); JsonElement? json = null; 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 76e63171..c3121855 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -10,7 +10,7 @@ public partial class PlaywrightWebDriver { var page = args.UseExistingPage ? _instance.GetPage(message.ContextId, pattern: args.Url) : - await _instance.NewPage(message); + await _instance.NewPage(message, excludeResponseUrls: args.ExcludeResponseUrls); if (args.UseExistingPage && page != null && page.Url != "about:blank") { @@ -23,7 +23,7 @@ public partial class PlaywrightWebDriver if (args.UseExistingPage && args.OpenNewTab && page != null && page.Url == "about:blank") { - page = await _instance.NewPage(message); + page = await _instance.NewPage(message, excludeResponseUrls: args.ExcludeResponseUrls); } var response = await page.GotoAsync(args.Url, new PageGotoOptions