Fix open page return value.
This commit is contained in:
parent
4f840c7940
commit
78fa62dc1a
|
|
@ -2,7 +2,7 @@ namespace BotSharp.Plugin.WebDriver.Drivers;
|
|||
|
||||
public interface IWebBrowser
|
||||
{
|
||||
Task LaunchBrowser(string? url);
|
||||
Task<bool> LaunchBrowser(string? url);
|
||||
Task<string> ScreenshotAsync(string path);
|
||||
Task<bool> InputUserText(BrowserActionParams actionParams);
|
||||
Task<bool> InputUserPassword(BrowserActionParams actionParams);
|
||||
|
|
@ -11,7 +11,7 @@ public interface IWebBrowser
|
|||
Task<bool> ChangeListValue(BrowserActionParams actionParams);
|
||||
Task<bool> CheckRadioButton(BrowserActionParams actionParams);
|
||||
Task<bool> ChangeCheckbox(BrowserActionParams actionParams);
|
||||
Task<bool> GoToPage(BrowserActionParams actionParams);
|
||||
Task<bool> GoToPage(string url);
|
||||
Task<string> ExtractData(BrowserActionParams actionParams);
|
||||
Task<T> EvaluateScript<T>(string script);
|
||||
Task CloseBrowser();
|
||||
|
|
|
|||
|
|
@ -1,11 +1,23 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
||||
|
||||
public partial class PlaywrightWebDriver
|
||||
{
|
||||
public async Task<bool> GoToPage(BrowserActionParams actionParams)
|
||||
public async Task<bool> GoToPage(string url)
|
||||
{
|
||||
await _instance.Page.GotoAsync(actionParams.Context.Url);
|
||||
await _instance.Page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
|
||||
return true;
|
||||
try
|
||||
{
|
||||
var response = await _instance.Page.GotoAsync(url);
|
||||
await _instance.Page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
|
||||
|
||||
return response.Status == 200;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
||||
|
||||
public partial class PlaywrightWebDriver
|
||||
{
|
||||
public async Task LaunchBrowser(string? url)
|
||||
public async Task<bool> LaunchBrowser(string? url)
|
||||
{
|
||||
await _instance.InitInstance();
|
||||
|
||||
|
|
@ -16,15 +18,23 @@ public partial class PlaywrightWebDriver
|
|||
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
{
|
||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||
url = webDriverService.ReplaceToken(url);
|
||||
|
||||
var response = await page.GotoAsync(url, new PageGotoOptions
|
||||
try
|
||||
{
|
||||
Timeout = 15 * 1000
|
||||
});
|
||||
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
|
||||
var response = await page.GotoAsync(url, new PageGotoOptions
|
||||
{
|
||||
Timeout = 15 * 1000
|
||||
});
|
||||
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
|
||||
return response.Status == 200;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,14 +19,19 @@ public class GoToPageFn : IFunctionCallback
|
|||
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||
await _browser.GoToPage(new BrowserActionParams(agent, args, message.MessageId));
|
||||
message.Content = $"Page {args.Url} is open.";
|
||||
|
||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||
var url = webDriverService.ReplaceToken(args.Url);
|
||||
|
||||
url = url.Replace("https://https://", "https://");
|
||||
|
||||
var result = await _browser.GoToPage(url);
|
||||
message.Content = result ? $"Page {url} is open." : $"Page {url} open failed.";
|
||||
|
||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||
|
||||
message.Data = await _browser.ScreenshotAsync(path);
|
||||
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,14 +17,26 @@ public class OpenBrowserFn : IFunctionCallback
|
|||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
|
||||
await _browser.LaunchBrowser(args.Url);
|
||||
message.Content = string.IsNullOrEmpty(args.Url) ? $"Launch browser with blank page successfully." : $"Open website {args.Url} successfully.";
|
||||
|
||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||
var url = webDriverService.ReplaceToken(args.Url);
|
||||
|
||||
url = url.Replace("https://https://", "https://");
|
||||
var result = await _browser.LaunchBrowser(url);
|
||||
|
||||
if (result)
|
||||
{
|
||||
message.Content = string.IsNullOrEmpty(url) ? $"Launch browser with blank page successfully." : $"Open website {url} successfully.";
|
||||
}
|
||||
else
|
||||
{
|
||||
message.Content = "Launch browser failed.";
|
||||
}
|
||||
|
||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||
|
||||
message.Data = await _browser.ScreenshotAsync(path);
|
||||
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue