Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ All notable changes to **bUnit** will be documented in this file. The project ad

## [Unreleased]

### Added
- Added `FindByAllByLabel` to `bunit.web.query` package. By [@linkdotnet](https://github.com/linkdotnet).

### Fixed
- Updated `AngleSharp.Diffing` to fix a bug related to unknown HTML elements. Reported by [@md-at-slashwhy](https://github.com/md-at-slashwhy).

## [2.1.1] - 2025-11-21

### Changed
Expand Down
18 changes: 9 additions & 9 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<!-- Shared code analyzers used for all projects in the solution -->
<ItemGroup Condition="!$(MSBuildProjectName.EndsWith('samples'))">
<GlobalPackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" IncludeAssets="Runtime;Build;Native;contentFiles;Analyzers"/>
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="10.15.0.120848" PrivateAssets="All" IncludeAssets="Runtime;Build;Native;contentFiles;Analyzers"/>
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="10.16.1.129956" PrivateAssets="All" IncludeAssets="Runtime;Build;Native;contentFiles;Analyzers"/>
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />
<PackageVersion Include="Meziantou.Polyfill" Version="1.0.60" />
<PackageVersion Include="Meziantou.Polyfill" Version="1.0.68" />
</ItemGroup>

<ItemGroup Label="Shared">
Expand All @@ -17,8 +17,8 @@

<PackageVersion Include="Serilog" Version="4.3.0"/>
<PackageVersion Include="Serilog.Expressions" Version="5.0.0"/>
<PackageVersion Include="AngleSharp.Diffing" Version="1.1.0"/>
<PackageVersion Include="AngleSharp" Version="1.3.0"/>
<PackageVersion Include="AngleSharp.Diffing" Version="1.1.1"/>
<PackageVersion Include="AngleSharp" Version="1.4.0"/>
<PackageVersion Include="AngleSharp.Css" Version="1.0.0-beta.157"/>
</ItemGroup>

Expand Down Expand Up @@ -81,18 +81,18 @@
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageVersion Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.2" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="10.0.0" />
<PackageVersion Include="Shouldly" Version="4.3.0"/>
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0"/>
<PackageVersion Include="Verify.XunitV3" Version="31.0.2"/>
<PackageVersion Include="Verify.XunitV3" Version="31.8.0"/>
<PackageVersion Include="Xunit.Combinatorial" Version="2.0.24"/>
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.4" />
<PackageVersion Include="nunit" Version="4.4.0" />
<PackageVersion Include="xunit" Version="2.9.3"/>
<PackageVersion Include="xunit.v3" Version="3.2.0"/>
<PackageVersion Include="xunit.v3.extensibility.core" Version="3.2.0" />
<PackageVersion Include="xunit.v3.assert" Version="3.2.0"/>
<PackageVersion Include="xunit.v3" Version="3.2.1"/>
<PackageVersion Include="xunit.v3.extensibility.core" Version="3.2.1" />
<PackageVersion Include="xunit.v3.assert" Version="3.2.1"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5"/>
</ItemGroup>

Expand Down
123 changes: 0 additions & 123 deletions bunit.sln

This file was deleted.

44 changes: 44 additions & 0 deletions bunit.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Solution>
<Folder Name="/.items/">
<File Path=".config/dotnet-tools.json" />
<File Path=".editorconfig" />
<File Path=".gitattributes" />
<File Path=".gitignore" />
<File Path="Directory.Build.props" />
<File Path="Directory.Packages.props" />
<File Path="global.json" />
<File Path="version.json" />
</Folder>
<Folder Name="/.text/">
<File Path="CHANGELOG.md" />
<File Path="MIGRATION.md" />
<File Path="README.md" />
</Folder>
<Folder Name="/.workflows/">
<File Path=".github/workflows/ci.yml" />
<File Path=".github/workflows/docs-deploy.yml" />
<File Path=".github/workflows/prepare-release.yml" />
<File Path=".github/workflows/release.yml" />
</Folder>
<Folder Name="/src/">
<File Path="src/.editorconfig" />
<File Path="src/Directory.Build.props" />
<Project Path="src/bunit.generators.internal/bunit.generators.internal.csproj" />
<Project Path="src/bunit.generators/bunit.generators.csproj" />
<Project Path="src/bunit.template/bunit.template.csproj">
<Build Project="false" />
</Project>
<Project Path="src/bunit.web.query/bunit.web.query.csproj" />
<Project Path="src/bunit/bunit.csproj" />
</Folder>
<Folder Name="/tests/">
<File Path="tests/.editorconfig" />
<File Path="tests/Directory.Build.props" />
<File Path="tests/run-tests.ps1" />
<File Path="tests/xunit.runner.json" />
<Project Path="tests/bunit.generators.tests/bunit.generators.tests.csproj" />
<Project Path="tests/bunit.testassets/bunit.testassets.csproj" />
<Project Path="tests/bunit.tests/bunit.tests.csproj" />
<Project Path="tests/bunit.web.query.tests/bunit.web.query.tests.csproj" />
</Folder>
</Solution>
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The following tools are required to build and view the documentation locally:

### View the documentation

1. Build the `bunit.sln` solution in the root folder in release configuration `dotnet build -c Release`.
1. Build the `bunit.slxn` solution in the root folder in release configuration `dotnet build -c Release`.
2. From `docs/site` run `docfx metadata` to generate the documentation site's metadata.
3. After that run `docfx build` to generate the documentation site.
4. From `docs/` run `serve-docs.cmd`. This will start up a local web server (using `dotnet serve`), hosting the generated documentation site.
Expand Down
44 changes: 44 additions & 0 deletions src/bunit.web.query/Labels/LabelQueryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AngleSharp.Dom;
using Bunit.Labels.Strategies;
using Bunit.Web.AngleSharp;

namespace Bunit;

Expand Down Expand Up @@ -35,6 +36,25 @@ public static IElement FindByLabelText(this IRenderedComponent<IComponent> rende
return FindByLabelTextInternal(renderedComponent, labelText, options) ?? throw new LabelNotFoundException(labelText);
}

/// <summary>
/// Returns all elements (i.e. input, select, textarea, etc. elements) associated with the given label text.
/// </summary>
/// <param name="renderedComponent">The rendered fragment to search.</param>
/// <param name="labelText">The text of the label to search (i.e. the InnerText of the Label, such as "First Name" for a `<label>First Name</label>`)</param>
/// <param name="configureOptions">Method used to override the default behavior of FindAllByLabelText.</param>
/// <returns>A read-only collection of elements matching the label text. Returns an empty collection if no matches are found.</returns>
public static IReadOnlyList<IElement> FindAllByLabelText(this IRenderedComponent<IComponent> renderedComponent, string labelText, Action<ByLabelTextOptions>? configureOptions = null)
{
var options = ByLabelTextOptions.Default;
if (configureOptions is not null)
{
options = options with { };
configureOptions.Invoke(options);
}

return FindAllByLabelTextInternal(renderedComponent, labelText, options);
}

internal static IElement? FindByLabelTextInternal(this IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
{
foreach (var strategy in LabelTextQueryStrategies)
Expand All @@ -47,4 +67,28 @@ public static IElement FindByLabelText(this IRenderedComponent<IComponent> rende

return null;
}

internal static IReadOnlyList<IElement> FindAllByLabelTextInternal(this IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
{
var results = new List<IElement>();

foreach (var strategy in LabelTextQueryStrategies)
{
results.AddRange(strategy.FindElements(renderedComponent, labelText, options));
}

var seen = new HashSet<IElement>();
var distinctResults = new List<IElement>();

foreach (var element in results)
{
var underlyingElement = element.Unwrap();
if (seen.Add(underlyingElement))
{
distinctResults.Add(element);
}
}

return distinctResults;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ namespace Bunit.Labels.Strategies;

internal interface ILabelTextQueryStrategy
{
IElement? FindElement(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options);
IElement? FindElement(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
=> FindElements(renderedComponent, labelText, options).FirstOrDefault();

IEnumerable<IElement> FindElements(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bunit.Labels.Strategies;

internal sealed class LabelTextUsingAriaLabelStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
public IEnumerable<IElement> FindElements(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
{
var caseSensitivityQualifier = options.ComparisonType switch
{
Expand All @@ -15,11 +15,11 @@ internal sealed class LabelTextUsingAriaLabelStrategy : ILabelTextQueryStrategy
_ => ""
};

var element = renderedComponent.Nodes.TryQuerySelector($"[aria-label='{labelText}'{caseSensitivityQualifier}]");
var elements = renderedComponent.Nodes.TryQuerySelectorAll($"[aria-label='{labelText}'{caseSensitivityQualifier}]");

if (element is null)
return null;

return element.WrapUsing(new ByLabelTextElementFactory(renderedComponent, labelText, options));
foreach (var element in elements)
{
yield return element.WrapUsing(new ByLabelTextElementFactory(renderedComponent, labelText, options));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ namespace Bunit.Labels.Strategies;

internal sealed class LabelTextUsingAriaLabelledByStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
public IEnumerable<IElement> FindElements(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
{
var elementsWithAriaLabelledBy = renderedComponent.Nodes.TryQuerySelectorAll("[aria-labelledby]");

foreach (var element in elementsWithAriaLabelledBy)
{
var labelElement = renderedComponent.Nodes.TryQuerySelector($"#{element.GetAttribute("aria-labelledby")}");
if (labelElement is not null && labelElement.GetInnerText().Equals(labelText, options.ComparisonType))
return element.WrapUsing(new ByLabelTextElementFactory(renderedComponent, labelText, options));
yield return element.WrapUsing(new ByLabelTextElementFactory(renderedComponent, labelText, options));
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ namespace Bunit.Labels.Strategies;

internal sealed class LabelTextUsingForAttributeStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
public IEnumerable<IElement> FindElements(IRenderedComponent<IComponent> renderedComponent, string labelText, ByLabelTextOptions options)
{
var matchingLabel = renderedComponent.Nodes.TryQuerySelectorAll("label")
.SingleOrDefault(l => l.TextContent.Trim().Equals(labelText, options.ComparisonType));
var matchingLabels = renderedComponent.Nodes.TryQuerySelectorAll("label")
.Where(l => l.TextContent.Trim().Equals(labelText, options.ComparisonType));

if (matchingLabel is null)
return null;
foreach (var matchingLabel in matchingLabels)
{
var forAttribute = matchingLabel.GetAttribute("for");
if (string.IsNullOrEmpty(forAttribute))
continue;

var matchingElement = renderedComponent.Nodes.TryQuerySelector($"#{matchingLabel.GetAttribute("for")}");

if (matchingElement is null)
return null;

return matchingElement.WrapUsing(new ByLabelTextElementFactory(renderedComponent, labelText, options));
var matchingElement = renderedComponent.Nodes.TryQuerySelector($"#{forAttribute}");
if (matchingElement is not null)
yield return matchingElement.WrapUsing(new ByLabelTextElementFactory(renderedComponent, labelText, options));
}
}
}
Loading
Loading