From efb8d9f5c105155f2ca44ae7ea9e1db05538e440 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Fri, 14 Jun 2024 23:38:38 -0500 Subject: [PATCH] BringToFrontAsync --- .../PlaywrightDriver/PlaywrightInstance.cs | 83 ++++++++++++------- .../PlaywrightWebDriver.GoToPage.cs | 2 +- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index 3f7d4d3a..2b0847d5 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -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 _contexts = new Dictionary(); + Dictionary> _pages = new Dictionary>(); + + /// + /// ContextId and BrowserContext + /// public Dictionary Contexts => _contexts; - public IPage GetPage(string id) + /// + /// ContextId and Pages + /// + public Dictionary> Pages => _pages; + + public IPage GetPage(string id, string? pattern = null) { InitInstance(id).Wait(); return _contexts[id].Pages.LastOrDefault(); } - public async Task InitInstance(string id) + public async Task InitInstance(string ctxId) { if (_playwright == null) { _playwright = await Playwright.CreateAsync(); } - return await InitContext(id); + return await InitContext(ctxId); } - public async Task InitContext(string id) + public async Task 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(); - _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 NewPage(string id) + public async Task 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) + /// + /// Wait page and network until timeout in seconds + /// + /// + /// seconds + /// + 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(); } } 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 4e505997..3a80d07d 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -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; } }