Fix mullti-context active page issue for PlaywrightInstance
This commit is contained in:
parent
22cc3dccf4
commit
d213d2f8fd
|
|
@ -9,7 +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;
|
||||
Dictionary<string, IPage?> _activePage = new Dictionary<string, IPage?>();
|
||||
|
||||
/// <summary>
|
||||
/// ContextId and BrowserContext
|
||||
|
|
@ -30,14 +30,14 @@ public class PlaywrightInstance : IDisposable
|
|||
{
|
||||
if (string.IsNullOrEmpty(pattern))
|
||||
{
|
||||
return _activePage ?? _contexts[contextId].Pages.LastOrDefault();
|
||||
return _activePage.ContainsKey(contextId) ? _activePage[contextId] : _contexts[contextId].Pages.LastOrDefault();
|
||||
}
|
||||
|
||||
foreach (var page in _contexts[contextId].Pages)
|
||||
{
|
||||
if (page.Url.ToLower() == pattern.ToLower())
|
||||
{
|
||||
_activePage = page;
|
||||
_activePage[contextId] = page;
|
||||
page.BringToFrontAsync().Wait();
|
||||
return page;
|
||||
}
|
||||
|
|
@ -92,7 +92,7 @@ public class PlaywrightInstance : IDisposable
|
|||
|
||||
_contexts[ctxId].Page += async (sender, page) =>
|
||||
{
|
||||
_activePage = page;
|
||||
_activePage[ctxId] = page;
|
||||
_pages[ctxId].Add(page);
|
||||
page.Close += async (sender, e) =>
|
||||
{
|
||||
|
|
@ -240,7 +240,7 @@ public class PlaywrightInstance : IDisposable
|
|||
if (page != null)
|
||||
{
|
||||
await page.CloseAsync();
|
||||
_activePage = _pages[ctxId].LastOrDefault();
|
||||
_activePage[ctxId] = _pages[ctxId].LastOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue