From 97e0b687ce6609d7bdd1ae28291513e7ca8209d7 Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 25 Nov 2024 22:20:32 +0800 Subject: [PATCH 1/3] add AvatarBase64 --- .../Browsing/Models/ElementLocatingArgs.cs | 1 + .../PlaywrightWebDriver.LocateElement.cs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs index b50f2366..a8b5af51 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs @@ -36,4 +36,5 @@ public class ElementLocatingArgs /// public bool Highlight { get; set; } public string HighlightColor { get; set; } = "red"; + public bool IsImage { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs index 8695fc5c..e9b4004e 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs @@ -17,6 +17,18 @@ public partial class PlaywrightWebDriver ILocator locator = page.Locator("body"); int count = 0; + if (location.IsImage) + { + var handle = await page.QuerySelectorAsync("img"); + byte[]? imageBytes = handle != null ? await handle.ScreenshotAsync() : null; + string? body = null; + if (imageBytes?.Any() == true) + { + body = Convert.ToBase64String(imageBytes); + } + return new BrowserActionResult { IsSuccess = true, Body = body }; + } + // check if selector is specified if (location.Selector != null) { From 8cb29b147c642ed54d03cfc107e41c66e1c6410b Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 25 Nov 2024 23:05:56 +0800 Subject: [PATCH 2/3] use IHttpClientFactory --- .../Browsing/Models/ElementLocatingArgs.cs | 1 - .../PlaywrightWebDriver.LocateElement.cs | 12 ------------ 2 files changed, 13 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs index a8b5af51..b50f2366 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs @@ -36,5 +36,4 @@ public class ElementLocatingArgs /// public bool Highlight { get; set; } public string HighlightColor { get; set; } = "red"; - public bool IsImage { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs index e9b4004e..8695fc5c 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs @@ -17,18 +17,6 @@ public partial class PlaywrightWebDriver ILocator locator = page.Locator("body"); int count = 0; - if (location.IsImage) - { - var handle = await page.QuerySelectorAsync("img"); - byte[]? imageBytes = handle != null ? await handle.ScreenshotAsync() : null; - string? body = null; - if (imageBytes?.Any() == true) - { - body = Convert.ToBase64String(imageBytes); - } - return new BrowserActionResult { IsSuccess = true, Body = body }; - } - // check if selector is specified if (location.Selector != null) { From 85d4cf17bdcdee008f15adcbfcbcafc74d885066 Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 30 Dec 2024 19:15:30 +0800 Subject: [PATCH 3/3] fix content is null or '' --- .../Drivers/PlaywrightDriver/PlaywrightInstance.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index bf58a5ac..95b88eee 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -150,17 +150,20 @@ public class PlaywrightInstance : IDisposable ResponseInMemory = args.ResponseInMemory }; + var html = await response.TextAsync(); if (response.Headers["content-type"].Contains("application/json")) { if (response.Status == 200 && response.Ok) - { - var json = await response.JsonAsync(); - result.ResponseData = JsonSerializer.Serialize(json); + { + if (!string.IsNullOrWhiteSpace(html)) + { + var json = await response.JsonAsync(); + result.ResponseData = JsonSerializer.Serialize(json); + } } } else { - var html = await response.TextAsync(); result.ResponseData = html; }