diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs index 0368bfdd..d81dae39 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs @@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Browsing; public interface IWebBrowser { + void SetServiceProvider(IServiceProvider services); Task LaunchBrowser(MessageInfo message, BrowserActionArgs args); Task ScreenshotAsync(MessageInfo message, string path); Task ScrollPage(MessageInfo message, PageActionArgs args); diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs index dcbe66f7..0819520d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs @@ -14,4 +14,9 @@ public class PageActionArgs public bool WaitForNetworkIdle { get; set; } = true; public float? Timeout { get; set; } + + /// + /// Wait time in seconds after page is opened + /// + public int WaitTime { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 667e7e71..1b58a984 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -170,7 +170,7 @@ - + diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj index fcaac28e..ce32616a 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj @@ -11,15 +11,17 @@ + + + - - + diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index ebf1e524..53466f1a 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -18,9 +18,22 @@ public class PlaywrightInstance : IDisposable /// public Dictionary> Pages => _pages; - public IPage GetPage(string id, string? pattern = null) + public IPage GetPage(string contextId, string? pattern = null) { - return _contexts[id].Pages.LastOrDefault(); + if (string.IsNullOrEmpty(pattern)) + { + return _contexts[contextId].Pages.LastOrDefault(); + } + + foreach (var page in _contexts[contextId].Pages) + { + if (page.Url.ToLower() == pattern.ToLower()) + { + return page; + } + } + + return _contexts[contextId].Pages.LastOrDefault(); } public async Task GetContext(string ctxId) @@ -103,38 +116,6 @@ public class PlaywrightInstance : IDisposable var js = @"Object.defineProperties(navigator, {webdriver:{get:()=>false}});"; await page.AddInitScriptAsync(js); - page.Response += async (sender, e) => - { - if (e.Headers.ContainsKey("content-type") && - e.Headers["content-type"].Contains("application/json") && - (e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr")) - { - Serilog.Log.Information($"{e.Request.Method}: {e.Url}"); - JsonElement? json = null; - try - { - if (e.Status == 200 && e.Ok) - { - json = await e.JsonAsync(); - } - else - { - Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}"); - } - - var webPageResponseHooks = services.GetServices(); - foreach (var hook in webPageResponseHooks) - { - hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json)); - } - } - catch(Exception ex) - { - Serilog.Log.Error(ex.ToString()); - } - } - }; - return 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 458889f9..4b8cc5b1 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -25,9 +25,49 @@ public partial class PlaywrightWebDriver }*/ var page = args.OpenNewTab ? await _instance.NewPage(message, _services) : - _instance.GetPage(message.ContextId); + _instance.GetPage(message.ContextId, pattern: args.Url); + + page.Response += async (sender, e) => + { + if (e.Headers.ContainsKey("content-type") && + e.Headers["content-type"].Contains("application/json") && + (e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr")) + { + Serilog.Log.Information($"{e.Request.Method}: {e.Url}"); + JsonElement? json = null; + try + { + if (e.Status == 200 && e.Ok) + { + json = await e.JsonAsync(); + } + else + { + Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}"); + } + + var webPageResponseHooks = _services.GetServices(); + foreach (var hook in webPageResponseHooks) + { + hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json)); + } + } + catch (Exception ex) + { + Serilog.Log.Error(ex.ToString()); + } + } + }; + + if (!args.OpenNewTab && page != null && page.Url != "about:blank") + { + Serilog.Log.Information($"goto existing page: {args.Url}"); + result.IsSuccess = true; + return result; + } Serilog.Log.Information($"goto page: {args.Url}"); + var response = await page.GotoAsync(args.Url, new PageGotoOptions { Timeout = args.Timeout @@ -42,6 +82,11 @@ public partial class PlaywrightWebDriver }); } + if (args.WaitTime > 0) + { + await Task.Delay(args.WaitTime * 1000); + } + if (response.Status == 200) { // Disable this due to performance issue, some page is too large diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs index dab45548..cd0b7769 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs @@ -2,7 +2,7 @@ namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver; public partial class PlaywrightWebDriver : IWebBrowser { - private readonly IServiceProvider _services; + private IServiceProvider _services; private readonly PlaywrightInstance _instance; private readonly ILogger _logger; public PlaywrightInstance Instance => _instance; @@ -63,4 +63,9 @@ public partial class PlaywrightWebDriver : IWebBrowser return element; } + + public void SetServiceProvider(IServiceProvider services) + { + _services = services; + } } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs index 34f62754..15dd0bc1 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs @@ -81,4 +81,9 @@ public partial class SeleniumWebDriver : IWebBrowser { throw new NotImplementedException(); } + + public void SetServiceProvider(IServiceProvider services) + { + throw new NotImplementedException(); + } } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Using.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Using.cs index 619f0e23..161e150a 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Using.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Using.cs @@ -9,7 +9,6 @@ global using Microsoft.Playwright; global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Logging; -global using OpenQA.Selenium; global using BotSharp.Abstraction.Browsing.Enums; global using BotSharp.Abstraction.Conversations; global using BotSharp.Abstraction.Plugins; diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs b/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs index 12dd0090..6fe2d8dd 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs @@ -1,7 +1,6 @@ using BotSharp.Abstraction.Browsing.Settings; using BotSharp.Abstraction.Settings; using BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver; -using BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver; using BotSharp.Plugin.WebDriver.Hooks; namespace BotSharp.Plugin.Playwrights; @@ -12,7 +11,7 @@ public class WebDriverPlugin : IBotSharpPlugin public string Name => "Web Driver"; public string Description => "Empower agent to manipulate web browser in automation tools."; public string IconUrl => "https://cdn-icons-png.flaticon.com/512/8576/8576378.png"; - public string[] AgentIds => new[] { "f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b" }; + public string[] AgentIds => ["f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b"]; public void RegisterDI(IServiceCollection services, IConfiguration config) { @@ -28,13 +27,13 @@ public class WebDriverPlugin : IBotSharpPlugin services.AddScoped(); services.AddSingleton(); - services.AddScoped(); - services.AddSingleton(); + // services.AddScoped(); + // services.AddSingleton(); services.AddScoped(provider => settings.Driver switch { "Playwright" => provider.GetRequiredService(), - "Selenium" => provider.GetRequiredService(), + // "Selenium" => provider.GetRequiredService(), _ => provider.GetRequiredService(), });