Change SendHttpRequest args

This commit is contained in:
Haiping Chen 2024-05-17 18:45:18 -05:00
parent 151ec1986c
commit 8eee1fd164
9 changed files with 43 additions and 11 deletions

View file

@ -24,6 +24,6 @@ public interface IWebBrowser
Task<T> EvaluateScript<T>(string contextId, string script);
Task CloseBrowser(string contextId);
Task CloseCurrentPage(string contextId);
Task<BrowserActionResult> SendHttpRequest(string contextId, HttpRequestParams actionParams);
Task<BrowserActionResult> SendHttpRequest(MessageInfo message, HttpRequestParams actionParams);
Task<BrowserActionResult> GetAttributeValue(MessageInfo message, ElementLocatingArgs location);
}

View file

@ -10,6 +10,8 @@ public class ElementActionArgs
public ElementPosition? Position { get; set; }
public string? PressKey { get; set; }
/// <summary>
/// Required for deserialization
/// </summary>

View file

@ -18,7 +18,7 @@ public class HttpRequestParams
public HttpRequestParams(string url, HttpMethod method, string? payload = null)
{
Method = HttpMethod.Get;
Method = method;
Url = url;
Payload = payload;
}

View file

@ -40,11 +40,12 @@ public class PlaywrightInstance : IDisposable
Channel = "chrome",
IgnoreDefaultArgs = new[]
{
"--disable-infobars"
"--enable-automation",
},
Args = new[]
{
"--disable-infobars",
"--no-sandbox",
// "--start-maximized"
}
});
@ -104,6 +105,6 @@ public class PlaywrightInstance : IDisposable
public void Dispose()
{
_contexts.Clear();
_playwright.Dispose();
_playwright?.Dispose();
}
}

View file

@ -28,6 +28,11 @@ public partial class PlaywrightWebDriver
else if (action.Action == BroswerActionEnum.InputText)
{
await locator.FillAsync(action.Content);
if (action.PressKey != null)
{
await locator.PressAsync(action.PressKey);
}
}
}
}

View file

@ -4,7 +4,7 @@ namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
public async Task<BrowserActionResult> SendHttpRequest(string contextId, HttpRequestParams args)
public async Task<BrowserActionResult> SendHttpRequest(MessageInfo message, HttpRequestParams args)
{
var result = new BrowserActionResult();
@ -27,7 +27,7 @@ public partial class PlaywrightWebDriver
try
{
var response = await EvaluateScript<object>(contextId, script);
var response = await EvaluateScript<object>(message.ContextId, script);
result.IsSuccess = true;
result.Body = JsonSerializer.Serialize(response);
}

View file

@ -1,3 +1,5 @@
using System.Xml.Linq;
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
@ -18,12 +20,12 @@ public partial class PlaywrightWebDriver
// check if selector is specified
if (location.Selector != null)
{
locator = page.Locator(location.Selector);
locator = locator.Locator(location.Selector);
count = await locator.CountAsync();
}
// try attribute
if (count == 0 && !string.IsNullOrEmpty(location.AttributeName))
if (!string.IsNullOrEmpty(location.AttributeName))
{
locator = locator.Locator($"[{location.AttributeName}='{location.AttributeValue}']");
count = await locator.CountAsync();
@ -65,12 +67,29 @@ public partial class PlaywrightWebDriver
else if (count == 1)
{
result.Selector = locator.ToString().Split('@').Last();
// Make sure the element is visible
await locator.EvaluateAsync("element => element.style.height = ''");
await locator.EvaluateAsync("element => element.style.width = ''");
await locator.EvaluateAsync("element => element.style.opacity = ''");
var text = await locator.InnerTextAsync();
result.Body = text;
result.IsSuccess = true;
}
else if (count > 1)
{
// Make sure the element is visible
foreach (var element in await locator.AllAsync())
{
if (!await element.IsVisibleAsync())
{
await element.EvaluateAsync("element => element.style.height = '10px'");
await element.EvaluateAsync("element => element.style.width = '10px'");
await element.EvaluateAsync("element => element.style.opacity = '1.0'");
}
}
if (location.FailIfMultiple)
{
result.Message = $"Multiple elements are found by {locator}";

View file

@ -4,7 +4,7 @@ namespace BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver;
public partial class SeleniumWebDriver
{
public async Task<BrowserActionResult> SendHttpRequest(string contextId, HttpRequestParams args)
public async Task<BrowserActionResult> SendHttpRequest(MessageInfo message, HttpRequestParams args)
{
var result = new BrowserActionResult();
@ -27,7 +27,7 @@ public partial class SeleniumWebDriver
try
{
var response = await EvaluateScript<object>(contextId, script);
var response = await EvaluateScript<object>(message.ContextId, script);
result.IsSuccess = true;
result.Body = JsonSerializer.Serialize(response);
}

View file

@ -21,7 +21,12 @@ public class HttpRequestFn : IFunctionCallback
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
var result = await _browser.SendHttpRequest(convService.ConversationId, args);
var result = await _browser.SendHttpRequest(new MessageInfo
{
AgentId = agent.Id,
MessageId = message.MessageId,
ContextId = convService.ConversationId
}, args);
message.Content = result.IsSuccess ?
result.Body :