From e8d182494acd662b05c06b56e947d2047b9dcd76 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 23 Dec 2024 19:53:04 +0000 Subject: [PATCH] test webview2 --- .../Browsing/Models/BrowserActionArgs.cs | 2 + .../BotSharp.Core/BotSharp.Core.csproj | 2 +- .../PlaywrightDriver/PlaywrightInstance.cs | 48 +++++++++++-------- .../PlaywrightWebDriver.GoToPage.cs | 30 +++++++----- 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/BrowserActionArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/BrowserActionArgs.cs index 21bdf6ab..7987b3a5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/BrowserActionArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/BrowserActionArgs.cs @@ -3,4 +3,6 @@ namespace BotSharp.Abstraction.Browsing.Models; public class BrowserActionArgs { public bool Headless { get; set; } + public string? UserDataDir { get; set; } + public string? RemoteHostUrl { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index d7f6cdde..23351fb5 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -190,7 +190,7 @@ - + diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index b5155cd5..f14e4bdc 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -45,28 +45,36 @@ public class PlaywrightInstance : IDisposable _playwright = await Playwright.CreateAsync(); } - string tempFolderPath = $"{Path.GetTempPath()}\\playwright\\{ctxId}"; - - _contexts[ctxId] = await _playwright.Chromium.LaunchPersistentContextAsync(tempFolderPath, new BrowserTypeLaunchPersistentContextOptions + if (!string.IsNullOrEmpty(args.RemoteHostUrl)) { - Headless = args.Headless, - Channel = "chrome", - ViewportSize = new ViewportSize + var browser = await _playwright.Chromium.ConnectOverCDPAsync(args.RemoteHostUrl); + _contexts[ctxId] = browser.Contexts[0]; + } + else + { + string userDataDir = args.UserDataDir ?? $"{Path.GetTempPath()}\\playwright\\{ctxId}"; + _contexts[ctxId] = await _playwright.Chromium.LaunchPersistentContextAsync(userDataDir, new BrowserTypeLaunchPersistentContextOptions { - Width = 1600, - Height = 900 - }, - IgnoreDefaultArgs = - [ - "--enable-automation", - ], - Args = - [ - "--disable-infobars", - "--test-type" - // "--start-maximized" - ] - }); + Headless = args.Headless, + Channel = "chrome", + ViewportSize = new ViewportSize + { + Width = 1600, + Height = 900 + }, + IgnoreDefaultArgs = + [ + "--enable-automation", + ], + Args = + [ + "--disable-infobars", + "--test-type" + // "--start-maximized" + ] + }); + } + _pages[ctxId] = new List(); _contexts[ctxId].Page += async (sender, page) => 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 2f339ca6..0a0a5425 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -8,22 +8,30 @@ public partial class PlaywrightWebDriver var context = await _instance.GetContext(message.ContextId); try { - var page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback, - responseInMemory: args.ResponseInMemory, - responseContainer: args.ResponseContainer, - excludeResponseUrls: args.ExcludeResponseUrls, - includeResponseUrls: args.IncludeResponseUrls); - - Serilog.Log.Information($"goto page: {args.Url}"); - - if (args.OpenNewTab && page != null && page.Url == "about:blank") + IPage? page = null; + if (!args.OpenNewTab && !args.EnableResponseCallback) { - page = await _instance.NewPage(message, - enableResponseCallback: args.EnableResponseCallback, + page = _instance.Contexts[message.ContextId].Pages.LastOrDefault(); + } + else + { + page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback, responseInMemory: args.ResponseInMemory, responseContainer: args.ResponseContainer, excludeResponseUrls: args.ExcludeResponseUrls, includeResponseUrls: args.IncludeResponseUrls); + + Serilog.Log.Information($"goto page: {args.Url}"); + + if (args.OpenNewTab && page != null && page.Url == "about:blank") + { + page = await _instance.NewPage(message, + enableResponseCallback: args.EnableResponseCallback, + responseInMemory: args.ResponseInMemory, + responseContainer: args.ResponseContainer, + excludeResponseUrls: args.ExcludeResponseUrls, + includeResponseUrls: args.IncludeResponseUrls); + } } if (page == null)