From 98a02caf714e50a428c9a45cfad50ddacfe6c6b6 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Tue, 6 Aug 2024 19:18:55 -0500 Subject: [PATCH] _activePage --- .../Drivers/PlaywrightDriver/PlaywrightInstance.cs | 9 +++++++-- .../PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index 0a1eea3b..65b45013 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -9,6 +9,7 @@ public class PlaywrightInstance : IDisposable public IServiceProvider Services => _services; Dictionary _contexts = new Dictionary(); Dictionary> _pages = new Dictionary>(); + IPage? _activePage = null; /// /// ContextId and BrowserContext @@ -25,17 +26,19 @@ public class PlaywrightInstance : IDisposable _services = services; } - public IPage GetPage(string contextId, string? pattern = null) + public IPage? GetPage(string contextId, string? pattern = null) { if (string.IsNullOrEmpty(pattern)) { - return _contexts[contextId].Pages.LastOrDefault(); + return _activePage ?? _contexts[contextId].Pages.LastOrDefault(); } foreach (var page in _contexts[contextId].Pages) { if (page.Url.ToLower() == pattern.ToLower()) { + _activePage = page; + page.BringToFrontAsync().Wait(); return page; } } @@ -84,6 +87,7 @@ public class PlaywrightInstance : IDisposable _contexts[ctxId].Page += async (sender, page) => { + _activePage = page; _pages[ctxId].Add(page); page.Close += async (sender, e) => { @@ -200,6 +204,7 @@ public class PlaywrightInstance : IDisposable if (page != null) { await page.CloseAsync(); + _activePage = _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 c3121855..5ddbfcbd 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -12,7 +12,7 @@ public partial class PlaywrightWebDriver _instance.GetPage(message.ContextId, pattern: args.Url) : await _instance.NewPage(message, excludeResponseUrls: args.ExcludeResponseUrls); - if (args.UseExistingPage && page != null && page.Url != "about:blank") + if (args.UseExistingPage && page != null && page.Url == args.Url) { Serilog.Log.Information($"goto existing page: {args.Url}"); result.IsSuccess = true;