2024-02-01 03:29:13 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2024-01-03 19:40:41 +00:00
|
|
|
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
|
|
|
|
|
|
|
|
|
public partial class PlaywrightWebDriver
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly PlaywrightInstance _instance;
|
2024-02-01 03:29:13 +00:00
|
|
|
private readonly ILogger _logger;
|
2024-01-23 23:14:57 +00:00
|
|
|
public PlaywrightInstance Instance => _instance;
|
2024-01-03 19:40:41 +00:00
|
|
|
|
2024-02-01 03:29:13 +00:00
|
|
|
public PlaywrightWebDriver(IServiceProvider services, PlaywrightInstance instance, ILogger<PlaywrightWebDriver> logger)
|
2024-01-03 19:40:41 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_instance = instance;
|
2024-02-01 03:29:13 +00:00
|
|
|
_logger = logger;
|
2024-01-03 19:40:41 +00:00
|
|
|
}
|
2024-01-27 14:58:51 +00:00
|
|
|
|
|
|
|
|
private ILocator Locator(HtmlElementContextOut context)
|
|
|
|
|
{
|
|
|
|
|
ILocator element = default;
|
|
|
|
|
if (!string.IsNullOrEmpty(context.ElementId))
|
|
|
|
|
{
|
|
|
|
|
element = _instance.Page.Locator($"#{context.ElementId}");
|
|
|
|
|
}
|
2024-02-01 03:29:13 +00:00
|
|
|
else if (!string.IsNullOrEmpty(context.ElementName))
|
|
|
|
|
{
|
|
|
|
|
var role = context.TagName switch
|
|
|
|
|
{
|
|
|
|
|
"input" => AriaRole.Textbox,
|
|
|
|
|
"textarea" => AriaRole.Textbox,
|
|
|
|
|
"button" => AriaRole.Button,
|
|
|
|
|
_ => AriaRole.Generic
|
|
|
|
|
};
|
|
|
|
|
element = _instance.Page.Locator($"[name='{context.ElementName}']");
|
|
|
|
|
|
|
|
|
|
if (element.CountAsync().Result == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError($"Can't locate element {role} {context.ElementName}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-27 14:58:51 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Index < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Can't locate the web element {context.Index}.");
|
|
|
|
|
}
|
|
|
|
|
element = _instance.Page.Locator(context.TagName).Nth(context.Index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return element;
|
|
|
|
|
}
|
2024-01-03 19:40:41 +00:00
|
|
|
}
|