_activePage

This commit is contained in:
Haiping Chen 2024-08-06 19:18:55 -05:00
parent 8d2c05e8e7
commit 98a02caf71
2 changed files with 8 additions and 3 deletions

View file

@ -9,6 +9,7 @@ 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>>();
IPage? _activePage = null;
/// <summary>
/// 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();
}
}
}

View file

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