BotSharp/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs

111 lines
3.9 KiB
C#
Raw Normal View History

2024-02-01 03:29:13 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
2024-06-10 15:51:14 +00:00
public async Task<BrowserActionResult> GoToPage(MessageInfo message, PageActionArgs args)
2024-02-01 03:29:13 +00:00
{
2024-03-08 13:17:36 +00:00
var result = new BrowserActionResult();
2024-06-30 13:05:57 +00:00
var context = await _instance.GetContext(message.ContextId);
2024-02-10 18:14:18 +00:00
try
{
// Check if the page is already open
2024-06-21 18:23:18 +00:00
/*if (!args.OpenNewTab && context.Pages.Count > 0)
{
2024-05-31 19:26:27 +00:00
foreach (var p in context.Pages)
{
2024-06-10 15:51:14 +00:00
if (p.Url == args.Url)
2024-05-31 19:26:27 +00:00
{
2024-06-10 15:51:14 +00:00
// Disable this due to performance issue, some page is too large
// result.Body = await p.ContentAsync();
2024-05-31 19:26:27 +00:00
result.IsSuccess = true;
2024-06-15 04:38:38 +00:00
// await p.BringToFrontAsync();
2024-05-31 19:26:27 +00:00
return result;
}
}
2024-06-21 18:23:18 +00:00
}*/
2024-07-22 20:45:01 +00:00
var page = args.OpenNewTab ? await _instance.NewPage(message, _services) :
2024-08-04 22:48:52 +00:00
_instance.GetPage(message.ContextId, pattern: args.Url);
page.Response += async (sender, e) =>
{
if (e.Headers.ContainsKey("content-type") &&
e.Headers["content-type"].Contains("application/json") &&
(e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr"))
{
Serilog.Log.Information($"{e.Request.Method}: {e.Url}");
JsonElement? json = null;
try
{
if (e.Status == 200 && e.Ok)
{
json = await e.JsonAsync();
}
else
{
Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}");
}
var webPageResponseHooks = _services.GetServices<IWebPageResponseHook>();
foreach (var hook in webPageResponseHooks)
{
hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json));
}
}
catch (Exception ex)
{
Serilog.Log.Error(ex.ToString());
}
}
};
if (!args.OpenNewTab && page != null && page.Url != "about:blank")
{
Serilog.Log.Information($"goto existing page: {args.Url}");
result.IsSuccess = true;
return result;
}
2024-06-21 18:23:18 +00:00
2024-07-09 12:15:03 +00:00
Serilog.Log.Information($"goto page: {args.Url}");
2024-08-04 22:48:52 +00:00
2024-06-26 12:48:15 +00:00
var response = await page.GotoAsync(args.Url, new PageGotoOptions
{
Timeout = args.Timeout
});
2024-04-09 03:38:56 +00:00
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
2024-06-26 12:48:15 +00:00
if (args.WaitForNetworkIdle)
{
await page.WaitForLoadStateAsync(LoadState.NetworkIdle, new PageWaitForLoadStateOptions
{
Timeout = args.Timeout
});
}
2024-02-10 18:14:18 +00:00
2024-08-04 22:48:52 +00:00
if (args.WaitTime > 0)
{
await Task.Delay(args.WaitTime * 1000);
}
2024-03-08 13:17:36 +00:00
if (response.Status == 200)
{
2024-05-31 03:24:34 +00:00
// Disable this due to performance issue, some page is too large
// result.Body = await page.InnerHTMLAsync("body");
2024-03-08 13:17:36 +00:00
result.IsSuccess = true;
}
else
{
2024-04-08 15:09:08 +00:00
result.Message = response.StatusText;
2024-03-08 13:17:36 +00:00
}
2024-02-10 18:14:18 +00:00
}
catch (Exception ex)
{
2024-04-08 15:09:08 +00:00
result.Message = ex.Message;
2024-03-08 13:17:36 +00:00
result.StackTrace = ex.StackTrace;
2024-02-10 18:14:18 +00:00
_logger.LogError(ex.Message);
}
2024-03-08 13:17:36 +00:00
return result;
2024-02-01 03:29:13 +00:00
}
}