BringToFrontAsync
This commit is contained in:
parent
cbf54f23a0
commit
efb8d9f5c1
|
|
@ -1,3 +1,4 @@
|
|||
using Microsoft.Playwright;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
||||
|
|
@ -6,31 +7,41 @@ public class PlaywrightInstance : IDisposable
|
|||
{
|
||||
IPlaywright _playwright;
|
||||
Dictionary<string, IBrowserContext> _contexts = new Dictionary<string, IBrowserContext>();
|
||||
Dictionary<string, List<IPage>> _pages = new Dictionary<string, List<IPage>>();
|
||||
|
||||
/// <summary>
|
||||
/// ContextId and BrowserContext
|
||||
/// </summary>
|
||||
public Dictionary<string, IBrowserContext> Contexts => _contexts;
|
||||
|
||||
public IPage GetPage(string id)
|
||||
/// <summary>
|
||||
/// ContextId and Pages
|
||||
/// </summary>
|
||||
public Dictionary<string, List<IPage>> Pages => _pages;
|
||||
|
||||
public IPage GetPage(string id, string? pattern = null)
|
||||
{
|
||||
InitInstance(id).Wait();
|
||||
return _contexts[id].Pages.LastOrDefault();
|
||||
}
|
||||
|
||||
public async Task<IBrowserContext> InitInstance(string id)
|
||||
public async Task<IBrowserContext> InitInstance(string ctxId)
|
||||
{
|
||||
if (_playwright == null)
|
||||
{
|
||||
_playwright = await Playwright.CreateAsync();
|
||||
}
|
||||
return await InitContext(id);
|
||||
return await InitContext(ctxId);
|
||||
}
|
||||
|
||||
public async Task<IBrowserContext> InitContext(string id)
|
||||
public async Task<IBrowserContext> InitContext(string ctxId)
|
||||
{
|
||||
if (_contexts.ContainsKey(id))
|
||||
return _contexts[id];
|
||||
if (_contexts.ContainsKey(ctxId))
|
||||
return _contexts[ctxId];
|
||||
|
||||
string tempFolderPath = $"{Path.GetTempPath()}\\playwright\\{id}";
|
||||
string tempFolderPath = $"{Path.GetTempPath()}\\playwright\\{ctxId}";
|
||||
|
||||
_contexts[id] = await _playwright.Chromium.LaunchPersistentContextAsync(tempFolderPath, new BrowserTypeLaunchPersistentContextOptions
|
||||
_contexts[ctxId] = await _playwright.Chromium.LaunchPersistentContextAsync(tempFolderPath, new BrowserTypeLaunchPersistentContextOptions
|
||||
{
|
||||
#if DEBUG
|
||||
Headless = false,
|
||||
|
|
@ -42,63 +53,77 @@ public class PlaywrightInstance : IDisposable
|
|||
[
|
||||
"--enable-automation",
|
||||
],
|
||||
Args =
|
||||
Args =
|
||||
[
|
||||
"--disable-infobars",
|
||||
"--test-type"
|
||||
// "--start-maximized"
|
||||
]
|
||||
});
|
||||
_pages[ctxId] = new List<IPage>();
|
||||
|
||||
_contexts[id].Page += async (sender, e) =>
|
||||
_contexts[ctxId].Page += async (sender, page) =>
|
||||
{
|
||||
e.Close += async (sender, e) =>
|
||||
_pages[ctxId].Add(page);
|
||||
page.Close += async (sender, e) =>
|
||||
{
|
||||
_pages[ctxId].Remove(e);
|
||||
Serilog.Log.Information($"Page is closed: {e.Url}");
|
||||
};
|
||||
Serilog.Log.Information($"New page is created: {e.Url}");
|
||||
await e.SetViewportSizeAsync(1280, 800);
|
||||
Serilog.Log.Information($"New page is created: {page.Url}");
|
||||
await page.SetViewportSizeAsync(1280, 800);
|
||||
};
|
||||
|
||||
_contexts[id].Close += async (sender, e) =>
|
||||
_contexts[ctxId].Close += async (sender, e) =>
|
||||
{
|
||||
Serilog.Log.Warning($"Playwright browser context is closed");
|
||||
_contexts.Remove(id);
|
||||
_pages.Remove(ctxId);
|
||||
_contexts.Remove(ctxId);
|
||||
};
|
||||
|
||||
return _contexts[id];
|
||||
return _contexts[ctxId];
|
||||
}
|
||||
|
||||
public async Task<IPage> NewPage(string id)
|
||||
public async Task<IPage> NewPage(string ctxId)
|
||||
{
|
||||
await InitContext(id);
|
||||
return await _contexts[id].NewPageAsync();
|
||||
await InitContext(ctxId);
|
||||
var page = await _contexts[ctxId].NewPageAsync();
|
||||
return page;
|
||||
}
|
||||
|
||||
public async Task Wait(string id)
|
||||
/// <summary>
|
||||
/// Wait page and network until timeout in seconds
|
||||
/// </summary>
|
||||
/// <param name="ctxId"></param>
|
||||
/// <param name="timeout">seconds</param>
|
||||
/// <returns></returns>
|
||||
public async Task Wait(string ctxId, int timeout = 60)
|
||||
{
|
||||
if (_contexts.ContainsKey(id))
|
||||
foreach (var page in _pages[ctxId])
|
||||
{
|
||||
var page = _contexts[id].Pages.Last();
|
||||
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
|
||||
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
|
||||
await page.WaitForLoadStateAsync(LoadState.NetworkIdle, new PageWaitForLoadStateOptions
|
||||
{
|
||||
Timeout = 1000 * timeout
|
||||
});
|
||||
}
|
||||
await Task.Delay(100);
|
||||
}
|
||||
|
||||
public async Task Close(string id)
|
||||
public async Task Close(string ctxId)
|
||||
{
|
||||
if (_contexts.ContainsKey(id))
|
||||
if (_contexts.ContainsKey(ctxId))
|
||||
{
|
||||
await _contexts[id].CloseAsync();
|
||||
await _contexts[ctxId].CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CloseCurrentPage(string id)
|
||||
public async Task CloseCurrentPage(string ctxId)
|
||||
{
|
||||
if (_contexts.ContainsKey(id))
|
||||
var pages = _pages[ctxId].ToArray();
|
||||
for (var i = 0; i < pages.Length; i++)
|
||||
{
|
||||
await GetPage(id).CloseAsync();
|
||||
await _pages[ctxId][i].CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ public partial class PlaywrightWebDriver
|
|||
// Disable this due to performance issue, some page is too large
|
||||
// result.Body = await p.ContentAsync();
|
||||
result.IsSuccess = true;
|
||||
await p.BringToFrontAsync();
|
||||
// await p.BringToFrontAsync();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue