Add ScrollPage to WebDriver.

This commit is contained in:
Haiping Chen 2024-02-15 14:37:42 -06:00
parent e2b7e2b070
commit 46a63e0f16
8 changed files with 79 additions and 4 deletions

View file

@ -4,6 +4,7 @@ public interface IWebBrowser
{
Task<bool> LaunchBrowser(string conversationId, string? url);
Task<string> ScreenshotAsync(string conversationId, string path);
Task<bool> ScrollPageAsync(BrowserActionParams actionParams);
Task<bool> InputUserText(BrowserActionParams actionParams);
Task<bool> InputUserPassword(BrowserActionParams actionParams);
Task<bool> ClickButton(BrowserActionParams actionParams);

View file

@ -30,7 +30,7 @@ public class PlaywrightInstance : IDisposable
string tempFolderPath = $"{Path.GetTempPath()}\\playwright\\{id}";
_contexts[id] = await _playwright.Chromium.LaunchPersistentContextAsync(tempFolderPath, new BrowserTypeLaunchPersistentContextOptions
{
Headless = true,
Headless = false,
Channel = "chrome",
IgnoreDefaultArgs = new[]
{

View file

@ -48,8 +48,7 @@ public partial class PlaywrightWebDriver
}
else if (count == 1)
{
// var tagName = await elements.EvaluateAsync<string>("el => el.tagName");
// var tagName = await locator.EvaluateAsync<string>("el => el.tagName");
await locator.ClickAsync();
// Triggered ajax

View file

@ -0,0 +1,23 @@
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
public async Task<bool> ScrollPageAsync(BrowserActionParams actionParams)
{
await _instance.Wait(actionParams.ConversationId);
var page = _instance.GetPage(actionParams.ConversationId);
if(actionParams.Context.Direction == "down")
await page.EvaluateAsync("window.scrollBy(0, window.innerHeight - 200)");
else if (actionParams.Context.Direction == "up")
await page.EvaluateAsync("window.scrollBy(0, -window.innerHeight + 200)");
else if (actionParams.Context.Direction == "left")
await page.EvaluateAsync("window.scrollBy(-400, 0)");
else if (actionParams.Context.Direction == "right")
await page.EvaluateAsync("window.scrollBy(400, 0)");
return true;
}
}

View file

@ -22,7 +22,7 @@ public class ScreenshotFn : IFunctionCallback
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
message.Data = await _browser.ScreenshotAsync(convService.ConversationId, path);
message.Content = "Took screenshot successfully";
message.Content = "Took screenshot completed";
return true;
}

View file

@ -0,0 +1,35 @@
namespace BotSharp.Plugin.WebDriver.Functions;
public class ScrollPageFn : IFunctionCallback
{
public string Name => "scroll_page";
private readonly IServiceProvider _services;
private readonly IWebBrowser _browser;
public ScrollPageFn(IServiceProvider services,
IWebBrowser browser)
{
_services = services;
_browser = browser;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var convService = _services.GetRequiredService<IConversationService>();
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
message.Data = await _browser.ScrollPageAsync(new BrowserActionParams(agent, args, convService.ConversationId, message.MessageId));
message.Content = "Scrolled";
var webDriverService = _services.GetRequiredService<WebDriverService>();
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
message.Data = await _browser.ScreenshotAsync(convService.ConversationId, path);
return true;
}
}

View file

@ -39,4 +39,7 @@ public class BrowsingContextIn
[JsonPropertyName("question")]
public string? Question { get; set; }
[JsonPropertyName("direction")]
public string? Direction { get; set; }
}

View file

@ -37,6 +37,20 @@
"required": [ "url" ]
}
},
{
"name": "scroll_page",
"description": "Scroll page down or up",
"parameters": {
"type": "object",
"properties": {
"direction": {
"type": "string",
"description": "down, up, left, right"
}
},
"required": [ "direction" ]
}
},
{
"name": "take_screenshot",
"description": "Tak screenshot to show current page screen",