-
-
Notifications
You must be signed in to change notification settings - Fork 119
feat: add FindByRole #1816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
feat: add FindByRole #1816
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using AngleSharp.Dom; | ||
| using Bunit.Web.AngleSharp; | ||
|
|
||
| namespace Bunit; | ||
|
|
||
| internal sealed class ByRoleElementFactory : IElementWrapperFactory | ||
| { | ||
| private readonly IRenderedComponent<IComponent> testTarget; | ||
| private readonly AriaRole role; | ||
| private readonly ByRoleOptions options; | ||
|
|
||
| public Action? OnElementReplaced { get; set; } | ||
|
|
||
| public ByRoleElementFactory(IRenderedComponent<IComponent> testTarget, AriaRole role, ByRoleOptions options) | ||
| { | ||
| this.testTarget = testTarget; | ||
| this.role = role; | ||
| this.options = options; | ||
| testTarget.OnMarkupUpdated += FragmentsMarkupUpdated; | ||
| } | ||
|
|
||
| private void FragmentsMarkupUpdated(object? sender, EventArgs args) | ||
| => OnElementReplaced?.Invoke(); | ||
|
|
||
| public TElement GetElement<TElement>() where TElement : class, IElement | ||
| { | ||
| var element = testTarget.FindByRoleInternal(role, options) as TElement; | ||
|
|
||
| return element ?? throw new ElementRemovedFromDomException(role.ToRoleString()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using AngleSharp.Dom; | ||
|
|
||
| namespace Bunit.Roles; | ||
|
|
||
| internal static class AccessibleNameCalculator | ||
| { | ||
| internal static string? GetAccessibleName(IElement element, INodeList rootNodes) | ||
| { | ||
| // 1. aria-labelledby — resolve each referenced ID, join with spaces | ||
| var labelledBy = element.GetAttribute("aria-labelledby"); | ||
| if (!string.IsNullOrEmpty(labelledBy)) | ||
| { | ||
| var ids = labelledBy.Split(' ', StringSplitOptions.RemoveEmptyEntries); | ||
| var parts = new List<string>(); | ||
| foreach (var id in ids) | ||
| { | ||
| var referenced = rootNodes.TryQuerySelector($"#{id}"); | ||
| if (referenced is not null) | ||
| { | ||
| var text = referenced.TextContent.Trim(); | ||
| if (!string.IsNullOrEmpty(text)) | ||
| parts.Add(text); | ||
| } | ||
| } | ||
|
|
||
| if (parts.Count > 0) | ||
| return string.Join(" ", parts); | ||
| } | ||
|
|
||
| // 2. aria-label attribute | ||
| var ariaLabel = element.GetAttribute("aria-label"); | ||
| if (!string.IsNullOrEmpty(ariaLabel)) | ||
| return ariaLabel.Trim(); | ||
|
|
||
| // 3. For IMG, AREA, INPUT[type=image]: alt attribute | ||
| var isImageInput = element.NodeName == "INPUT" && | ||
| string.Equals(element.GetAttribute("type"), "image", StringComparison.OrdinalIgnoreCase); | ||
| if (element.NodeName is "IMG" or "AREA" || isImageInput) | ||
| { | ||
| var alt = element.GetAttribute("alt"); | ||
| if (!string.IsNullOrEmpty(alt)) | ||
| return alt.Trim(); | ||
| } | ||
|
|
||
| // 4. For form controls: associated <label> | ||
| if (element.NodeName is "INPUT" or "SELECT" or "TEXTAREA" or "METER" or "OUTPUT" or "PROGRESS") | ||
| { | ||
| var labelName = GetLabelName(element, rootNodes); | ||
| if (labelName is not null) | ||
| return labelName; | ||
| } | ||
|
|
||
| // 5. Text content (for buttons, links, headings, etc.) | ||
| var textContent = element.TextContent.Trim(); | ||
| if (!string.IsNullOrEmpty(textContent)) | ||
| return textContent; | ||
|
|
||
| // 6. title attribute (fallback) | ||
| var title = element.GetAttribute("title"); | ||
| if (!string.IsNullOrEmpty(title)) | ||
| return title.Trim(); | ||
|
|
||
| // 7. placeholder attribute (fallback for inputs) | ||
| if (element.NodeName is "INPUT" or "TEXTAREA") | ||
| { | ||
| var placeholder = element.GetAttribute("placeholder"); | ||
| if (!string.IsNullOrEmpty(placeholder)) | ||
| return placeholder.Trim(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static string? GetLabelName(IElement element, INodeList rootNodes) | ||
| { | ||
| // Check for label via "for" attribute matching element's id | ||
| var id = element.GetAttribute("id"); | ||
| if (!string.IsNullOrEmpty(id)) | ||
| { | ||
| var labels = rootNodes.TryQuerySelectorAll($"label[for='{id}']"); | ||
| foreach (var label in labels) | ||
| { | ||
| var text = label.TextContent.Trim(); | ||
| if (!string.IsNullOrEmpty(text)) | ||
| return text; | ||
| } | ||
| } | ||
|
|
||
| // Check for wrapping <label> | ||
| var parent = element.ParentElement; | ||
| while (parent is not null) | ||
| { | ||
| if (parent.NodeName == "LABEL") | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've got these magic strings in a few places... centralize? |
||
| { | ||
| var text = GetTextContentExcluding(parent, element).Trim(); | ||
| if (!string.IsNullOrEmpty(text)) | ||
| return text; | ||
| } | ||
|
|
||
| parent = parent.ParentElement; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static string GetTextContentExcluding(IElement container, IElement excluded) | ||
| { | ||
| var parts = new List<string>(); | ||
| CollectTextNodes(container, excluded, parts); | ||
| return string.Join("", parts); | ||
| } | ||
|
|
||
| private static void CollectTextNodes(INode node, IElement excluded, List<string> parts) | ||
| { | ||
| foreach (var child in node.ChildNodes) | ||
| { | ||
| if (child == excluded) | ||
| continue; | ||
|
|
||
| if (child.NodeType == NodeType.Text) | ||
| { | ||
| parts.Add(child.TextContent); | ||
| } | ||
| else if (child.NodeType == NodeType.Element) | ||
| { | ||
| CollectTextNodes(child, excluded, parts); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like we should consider our strategy pattern here.