From 9844d9bfa2dec57fcec0c25629698bb701a50d1f Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 6 Nov 2024 23:21:51 -0600 Subject: [PATCH] Remove UseExistingPage and _activePage --- .../Browsing/Models/PageActionArgs.cs | 2 -- .../PlaywrightDriver/PlaywrightInstance.cs | 25 +------------------ .../PlaywrightWebDriver.GoToPage.cs | 15 ++--------- 3 files changed, 3 insertions(+), 39 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs index 16232e6b..fe575462 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs @@ -38,8 +38,6 @@ public class PageActionArgs public bool ResponseInMemory { get; set; } = false; public List? ResponseContainer { get; set; } - public bool UseExistingPage { get; set; } = false; - public bool WaitForNetworkIdle { get; set; } = true; public float? Timeout { 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 1b0e79ac..b5155cd5 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -9,7 +9,6 @@ public class PlaywrightInstance : IDisposable public IServiceProvider Services => _services; Dictionary _contexts = new Dictionary(); Dictionary> _pages = new Dictionary>(); - Dictionary _activePage = new Dictionary(); /// /// ContextId and BrowserContext @@ -26,28 +25,8 @@ public class PlaywrightInstance : IDisposable _services = services; } - public IPage? GetPage(string contextId, string? pattern = null) + public IPage? GetPage(string contextId) { - if (string.IsNullOrEmpty(pattern)) - { - return _activePage.ContainsKey(contextId) ? _activePage[contextId] : _contexts[contextId].Pages.LastOrDefault(); - } - - foreach (var page in _contexts[contextId].Pages) - { - if (page.Url.ToLower() == pattern.ToLower()) - { - _activePage[contextId] = page; - page.BringToFrontAsync().Wait(); - return page; - } - } - - if (!string.IsNullOrEmpty(pattern)) - { - return null; - } - return _contexts[contextId].Pages.LastOrDefault(); } @@ -92,7 +71,6 @@ public class PlaywrightInstance : IDisposable _contexts[ctxId].Page += async (sender, page) => { - _activePage[ctxId] = page; _pages[ctxId].Add(page); page.Close += async (sender, e) => { @@ -240,7 +218,6 @@ public class PlaywrightInstance : IDisposable if (page != null) { await page.CloseAsync(); - _activePage[ctxId] = _pages[ctxId].LastOrDefault(); } } } 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 9b00b010..30bd6748 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -1,5 +1,3 @@ -using Microsoft.Playwright; - namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver; public partial class PlaywrightWebDriver @@ -10,24 +8,15 @@ public partial class PlaywrightWebDriver var context = await _instance.GetContext(message.ContextId); try { - var page = args.UseExistingPage ? - _instance.GetPage(message.ContextId, pattern: args.Url) : - await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback, + var page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback, responseInMemory: args.ResponseInMemory, responseContainer: args.ResponseContainer, excludeResponseUrls: args.ExcludeResponseUrls, includeResponseUrls: args.IncludeResponseUrls); - if (args.UseExistingPage && page != null && page.Url == args.Url) - { - Serilog.Log.Information($"goto existing page: {args.Url}"); - result.IsSuccess = true; - return result; - } - Serilog.Log.Information($"goto page: {args.Url}"); - if (args.UseExistingPage && args.OpenNewTab && page != null && page.Url == "about:blank") + if (args.OpenNewTab && page != null && page.Url == "about:blank") { page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,