Merge pull request #1078 from yileicn/master

modify LocateElement logger level
This commit is contained in:
Haiping 2025-06-17 10:56:43 -05:00 committed by GitHub
commit d4f9e1c4ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 4 deletions

View file

@ -30,7 +30,7 @@ public class ElementLocatingArgs
public bool FailIfMultiple { get; set; } public bool FailIfMultiple { get; set; }
[JsonPropertyName("ignore_if_not_found")] [JsonPropertyName("ignore_if_not_found")]
public bool IgnoreIfNotFound { get; set; } public bool IgnoreIfNotFound { get; set; } = true;
/// <summary> /// <summary>
/// Draw outline around the element /// Draw outline around the element

View file

@ -23,16 +23,19 @@ public partial class PlaywrightWebDriver
} }
ILocator locator = page.Locator("body"); ILocator locator = page.Locator("body");
int count = 0; int count = 0;
var keyword = string.Empty;
// check if selector is specified // check if selector is specified
if (location.Selector != null) if (location.Selector != null)
{ {
keyword = location.Selector;
locator = locator.Locator(location.Selector); locator = locator.Locator(location.Selector);
count = await locator.CountAsync(); count = await locator.CountAsync();
} }
if (location.Tag != null) if (location.Tag != null)
{ {
keyword = location.Tag;
locator = page.Locator(location.Tag); locator = page.Locator(location.Tag);
count = await locator.CountAsync(); count = await locator.CountAsync();
} }
@ -40,13 +43,15 @@ public partial class PlaywrightWebDriver
// try attribute // try attribute
if (!string.IsNullOrEmpty(location.AttributeName)) if (!string.IsNullOrEmpty(location.AttributeName))
{ {
locator = locator.Locator($"[{location.AttributeName}='{location.AttributeValue}']"); keyword = $"[{location.AttributeName}='{location.AttributeValue}']";
locator = locator.Locator(keyword);
count = await locator.CountAsync(); count = await locator.CountAsync();
} }
// Retrieve the page raw html and infer the element path // Retrieve the page raw html and infer the element path
if (!string.IsNullOrEmpty(location.Text)) if (!string.IsNullOrEmpty(location.Text))
{ {
keyword = location.Text;
var text = location.Text.Replace("(", "\\(").Replace(")", "\\)"); var text = location.Text.Replace("(", "\\(").Replace(")", "\\)");
var regexExpression = location.MatchRule.ToLower() switch var regexExpression = location.MatchRule.ToLower() switch
{ {
@ -69,6 +74,7 @@ public partial class PlaywrightWebDriver
if (location.Index >= 0) if (location.Index >= 0)
{ {
keyword = $"Index:{location.Index}";
locator = locator.Nth(location.Index); locator = locator.Nth(location.Index);
count = await locator.CountAsync(); count = await locator.CountAsync();
} }
@ -77,12 +83,12 @@ public partial class PlaywrightWebDriver
{ {
if (location.IgnoreIfNotFound) if (location.IgnoreIfNotFound)
{ {
result.Message = $"Can't locate element by keyword {location.Text} and Ignored"; result.Message = $"Can't locate element by keyword {keyword} and Ignored";
_logger.LogWarning(result.Message); _logger.LogWarning(result.Message);
} }
else else
{ {
result.Message = $"Can't locate element by keyword {location.Text}"; result.Message = $"Can't locate element by keyword {keyword}";
_logger.LogError(result.Message); _logger.LogError(result.Message);
} }