From 5604273d1dd57f50d40ad858104b7a23f7b93978 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 29 Apr 2024 09:13:17 -0500 Subject: [PATCH] WebBrowsingSettings --- .../Browsing/Settings/WebBrowsingSettings.cs | 6 +++ .../BotSharp.Plugin.WebDriver.csproj | 12 ++++-- .../SeleniumDriver/SeleniumInstance.cs | 8 +++- .../SeleniumWebDriver.EvaluateScript.cs | 13 ++++++ .../SeleniumWebDriver.HttpRequest.cs | 43 +++++++++++++++++++ .../SeleniumDriver/SeleniumWebDriver.cs | 10 ----- .../WebDriverPlugin.cs | 24 +++++++++-- src/WebStarter/appsettings.json | 6 ++- 8 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Browsing/Settings/WebBrowsingSettings.cs create mode 100644 src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.EvaluateScript.cs create mode 100644 src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.HttpRequest.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Settings/WebBrowsingSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Settings/WebBrowsingSettings.cs new file mode 100644 index 00000000..25149fe0 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Settings/WebBrowsingSettings.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Abstraction.Browsing.Settings; + +public class WebBrowsingSettings +{ + public string Driver { get; set; } = "Playwright"; +} diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj index 234e898b..b03f585d 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 @@ -11,8 +11,14 @@ - - + + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumInstance.cs index 43586be4..5e1f5eaf 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumInstance.cs @@ -33,11 +33,15 @@ public class SeleniumInstance : IDisposable string tempFolderPath = $"{Path.GetTempPath()}\\_selenium\\{id}"; - var options = new ChromeOptions(); + var options = new ChromeOptions + { + // DebuggerAddress = "localhost:9222", + // BrowserVersion = "123.0.6312.46" + }; options.AddArgument("disable-infobars"); options.AddArgument($"--user-data-dir={tempFolderPath}"); var selenium = new ChromeDriver(options); - selenium.Manage().Window.Maximize(); + // selenium.Manage().Window.Maximize(); selenium.Navigate().GoToUrl("about:blank"); _contexts[id] = selenium; diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.EvaluateScript.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.EvaluateScript.cs new file mode 100644 index 00000000..9a59cb81 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.EvaluateScript.cs @@ -0,0 +1,13 @@ +namespace BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver; + +public partial class SeleniumWebDriver +{ + public async Task EvaluateScript(string contextId, string script) + { + await _instance.Wait(contextId); + var driver = await _instance.InitContext(contextId); + var jsExecutor = (IJavaScriptExecutor)driver; + var result = jsExecutor.ExecuteAsyncScript(script); + return (T)result; + } +} diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.HttpRequest.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.HttpRequest.cs new file mode 100644 index 00000000..b88a5d96 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.HttpRequest.cs @@ -0,0 +1,43 @@ +using System.Net.Http; + +namespace BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver; + +public partial class SeleniumWebDriver +{ + public async Task SendHttpRequest(string contextId, HttpRequestParams args) + { + var result = new BrowserActionResult(); + + var body = args.Method == HttpMethod.Post ? + $"body: '{args.Payload}'" : string.Empty; + + // Send AJAX request + string script = $@" + (async () => {{ + const response = await fetch('{args.Url}', {{ + method: '{args.Method}', + headers: {{ + 'Content-Type': 'application/json' + }}, + {body} + }}); + return await response.json(); + }})(); + "; + + try + { + var response = await EvaluateScript(contextId, script); + result.IsSuccess = true; + result.Body = JsonSerializer.Serialize(response); + } + catch (Exception ex) + { + result.Message = ex.Message; + result.StackTrace = ex.StackTrace; + _logger.LogError(ex.Message); + } + + return result; + } +} diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs index 26ce8ea7..40eb70d7 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/SeleniumDriver/SeleniumWebDriver.cs @@ -57,11 +57,6 @@ public partial class SeleniumWebDriver : IWebBrowser throw new NotImplementedException(); } - public Task EvaluateScript(string contextId, string script) - { - throw new NotImplementedException(); - } - public Task ExtractData(BrowserActionParams actionParams) { throw new NotImplementedException(); @@ -86,9 +81,4 @@ public partial class SeleniumWebDriver : IWebBrowser { throw new NotImplementedException(); } - - public Task SendHttpRequest(string contextId, HttpRequestParams actionParams) - { - throw new NotImplementedException(); - } } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs b/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs index e767104b..12dd0090 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs @@ -1,3 +1,5 @@ +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; @@ -14,12 +16,28 @@ public class WebDriverPlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { - // services.AddScoped(); - // services.AddSingleton(); + var settings = new WebBrowsingSettings(); + config.Bind("WebBrowsing", settings); - services.AddScoped(); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settings; + }); + + services.AddScoped(); + services.AddSingleton(); + + services.AddScoped(); services.AddSingleton(); + services.AddScoped(provider => settings.Driver switch + { + "Playwright" => provider.GetRequiredService(), + "Selenium" => provider.GetRequiredService(), + _ => provider.GetRequiredService(), + }); + services.AddScoped(); services.AddScoped(); } diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 83b870b7..1abd6ce3 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -133,6 +133,10 @@ } }, + "WebBrowsing": { + "Driver": "Playwright" + }, + "Statistics": { "DataDir": "stats" }, @@ -224,7 +228,7 @@ "ApiKey": "", "Map": { "Endpoint": "https://maps.googleapis.com/maps/api/geocode/json", - "Components": "country=US|country=CA", + "Components": "country=US|country=CA" }, "Youtube": { "Endpoint": "https://www.googleapis.com/youtube/v3/search",