2024-01-03 19:40:41 +00:00
|
|
|
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
|
|
|
|
|
|
|
|
|
public partial class PlaywrightWebDriver
|
|
|
|
|
{
|
2024-04-09 03:38:56 +00:00
|
|
|
public async Task<BrowserActionResult> LaunchBrowser(string contextId, string? url, bool openIfNotExist = true)
|
2024-01-03 19:40:41 +00:00
|
|
|
{
|
2024-03-08 13:17:36 +00:00
|
|
|
var result = new BrowserActionResult()
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true
|
|
|
|
|
};
|
2024-04-09 03:38:56 +00:00
|
|
|
var context = await _instance.InitInstance(contextId);
|
2024-01-03 19:40:41 +00:00
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
2024-04-09 03:38:56 +00:00
|
|
|
// Check if the page is already open
|
|
|
|
|
foreach (var p in context.Pages)
|
|
|
|
|
{
|
|
|
|
|
if (p.Url == url)
|
|
|
|
|
{
|
|
|
|
|
await p.BringToFrontAsync();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var page = await _instance.NewPage(contextId);
|
2024-04-16 04:48:22 +00:00
|
|
|
|
|
|
|
|
try
|
2024-02-04 22:55:46 +00:00
|
|
|
{
|
2024-04-16 04:48:22 +00:00
|
|
|
var response = await page.GotoAsync(url, new PageGotoOptions
|
2024-02-10 18:14:18 +00:00
|
|
|
{
|
2024-04-16 04:48:22 +00:00
|
|
|
Timeout = 15 * 1000
|
|
|
|
|
});
|
|
|
|
|
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
|
|
|
|
|
result.IsSuccess = response.Status == 200;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.Message = ex.Message;
|
|
|
|
|
result.StackTrace = ex.StackTrace;
|
|
|
|
|
_logger.LogError(ex.Message);
|
2024-02-04 22:55:46 +00:00
|
|
|
}
|
2024-01-03 19:40:41 +00:00
|
|
|
}
|
2024-02-10 18:14:18 +00:00
|
|
|
|
2024-03-08 13:17:36 +00:00
|
|
|
return result;
|
2024-01-03 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
}
|