Remove UseExistingPage and _activePage

This commit is contained in:
Haiping Chen 2024-11-06 23:21:51 -06:00
parent 68d444a541
commit 9844d9bfa2
3 changed files with 3 additions and 39 deletions

View file

@ -38,8 +38,6 @@ public class PageActionArgs
public bool ResponseInMemory { get; set; } = false;
public List<WebPageResponseData>? ResponseContainer { get; set; }
public bool UseExistingPage { get; set; } = false;
public bool WaitForNetworkIdle { get; set; } = true;
public float? Timeout { get; set; }

View file

@ -9,7 +9,6 @@ public class PlaywrightInstance : IDisposable
public IServiceProvider Services => _services;
Dictionary<string, IBrowserContext> _contexts = new Dictionary<string, IBrowserContext>();
Dictionary<string, List<IPage>> _pages = new Dictionary<string, List<IPage>>();
Dictionary<string, IPage?> _activePage = new Dictionary<string, IPage?>();
/// <summary>
/// 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();
}
}
}

View file

@ -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,