-
-
Notifications
You must be signed in to change notification settings - Fork 119
added FindByTestId #1802
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
Merged
Merged
added FindByTestId #1802
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace Bunit.TestIds; | ||
|
|
||
| /// <summary> | ||
| /// Represents a failure to find an element in the searched target | ||
| /// using the specified test id. | ||
| /// </summary> | ||
| public sealed class TestIdNotFoundException : Exception | ||
| { | ||
| /// <summary> | ||
| /// Gets the test id used to search with. | ||
| /// </summary> | ||
| public string? TestId { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TestIdNotFoundException"/> class. | ||
| /// </summary> | ||
| /// <param name="testId">The test id that was searched for.</param> | ||
| public TestIdNotFoundException(string? testId = null) | ||
|
jimSampica marked this conversation as resolved.
Outdated
|
||
| : base($"Unable to find an element with the Test ID '{testId}'.") | ||
| { | ||
| TestId = testId; | ||
| } | ||
| } | ||
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,19 @@ | ||
| namespace Bunit.TestIds; | ||
|
|
||
| /// <summary> | ||
| /// Allows overrides of behavior for FindByTestId method | ||
| /// </summary> | ||
| public record class ByTestIdOptions | ||
| { | ||
| internal static readonly ByTestIdOptions Default = new(); | ||
|
|
||
| /// <summary> | ||
| /// The StringComparison used for comparing the desired Test ID to the resulting HTML. Defaults to Ordinal (case sensitive). | ||
| /// </summary> | ||
| public StringComparison ComparisonType { get; set; } = StringComparison.Ordinal; | ||
|
|
||
| /// <summary> | ||
| /// The name of the attribute used for finding Test IDs. Defaults to "data-testid". | ||
| /// </summary> | ||
| public string TestIdAttribute { get; set; } = "data-testid"; | ||
| } |
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,42 @@ | ||
| using AngleSharp.Dom; | ||
| using Bunit.TestIds; | ||
|
|
||
| namespace Bunit; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for querying <see cref="IRenderedComponent{TComponent}" /> by Test ID | ||
| /// </summary> | ||
| public static class TestIdQueryExtensions | ||
| { | ||
| /// <summary> | ||
| /// Returns the first element with the specified Test ID. | ||
| /// </summary> | ||
| /// <param name="renderedComponent">The rendered fragment to search.</param> | ||
| /// <param name="testId">The Test ID to search for (e.g. "myTestId" in <span data-testid="myTestId">).</param> | ||
| /// <param name="configureOptions">Method used to override the default behavior of FindByTestId.</param> | ||
| /// <returns>The first element matching the specified role and options.</returns> | ||
| /// <exception cref="TestIdNotFoundException">Thrown when no element matching the provided testId is found.</exception> | ||
| public static IElement FindByTestId(this IRenderedComponent<IComponent> renderedComponent, string testId, Action<ByTestIdOptions>? configureOptions = null) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(renderedComponent); | ||
| ArgumentNullException.ThrowIfNull(testId); | ||
|
|
||
| var options = ByTestIdOptions.Default; | ||
| if (configureOptions is not null) | ||
| { | ||
| options = options with { }; | ||
| configureOptions.Invoke(options); | ||
| } | ||
|
|
||
| var elems = renderedComponent.Nodes.TryQuerySelectorAll($"[{options.TestIdAttribute}]"); | ||
|
|
||
| foreach (var elem in elems) | ||
| { | ||
| var attr = elem.GetAttribute(options.TestIdAttribute); | ||
| if (attr is not null && attr.Equals(testId, options.ComparisonType)) | ||
| return elem; | ||
| } | ||
|
|
||
| throw new TestIdNotFoundException(testId); | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
tests/bunit.web.query.tests/TestIds/TestIdQueryExtensionsTests.cs
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,102 @@ | ||
| namespace Bunit.TestIds; | ||
|
|
||
| public class TestIdQueryExtensionsTests : BunitContext | ||
| { | ||
| [Fact(DisplayName = "Should find span element with matching testid value")] | ||
| public void Test001() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent($"""<span data-testid="myTestId"><span>""")); | ||
|
|
||
| var elem = cut.FindByTestId("myTestId"); | ||
|
|
||
| elem.ShouldNotBeNull(); | ||
| elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); | ||
| elem.GetAttribute("data-testid").ShouldBe("myTestId"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Should throw exception when testid does not exist in the DOM")] | ||
| public void Test002() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent("""<span data-testid="testId"><span>""")); | ||
|
|
||
| Should.Throw<TestIdNotFoundException>(() => cut.FindByTestId("myTestId")).TestId.ShouldBe("myTestId"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Should throw exception when testid casing is different from DOM")] | ||
| public void Test003() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent("""<span data-testid="testId"><span>""")); | ||
|
|
||
| Should.Throw<TestIdNotFoundException>(() => cut.FindByTestId("MYTESTID")).TestId.ShouldBe("MYTESTID"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Should find first div element with matching testid value")] | ||
| public void Test004() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent($""" | ||
| <div data-testid="myTestId"></div> | ||
| <span data-testid="myTestId"><span> | ||
| """)); | ||
|
|
||
| var elem = cut.FindByTestId("myTestId"); | ||
|
|
||
| elem.ShouldNotBeNull(); | ||
| elem.NodeName.ShouldBe("DIV", StringCompareShould.IgnoreCase); | ||
| elem.GetAttribute("data-testid").ShouldBe("myTestId"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Should find first non-child div element with matching testid value")] | ||
| public void Test005() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent($""" | ||
| <div data-testid="myTestId"> | ||
| <span data-testid="myTestId"><span> | ||
| </div> | ||
| """)); | ||
|
|
||
| var elem = cut.FindByTestId("myTestId"); | ||
|
|
||
| elem.ShouldNotBeNull(); | ||
| elem.NodeName.ShouldBe("DIV", StringCompareShould.IgnoreCase); | ||
| elem.GetAttribute("data-testid").ShouldBe("myTestId"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Should find span element with matching testid attribute name and value")] | ||
| public void Test006() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent($"""<span data-testidattr="myTestId"><span>""")); | ||
|
|
||
| var elem = cut.FindByTestId("myTestId", opts => opts.TestIdAttribute = "data-testidattr"); | ||
|
|
||
| elem.ShouldNotBeNull(); | ||
| elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); | ||
| elem.GetAttribute("data-testidattr").ShouldBe("myTestId"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Should find span element with equivalent case-insensitive testid value")] | ||
| public void Test007() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent("""<span data-testid="myTestId"><span>""")); | ||
|
|
||
| var elem = cut.FindByTestId("MYTESTID", opts => opts.ComparisonType = StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| elem.ShouldNotBeNull(); | ||
| elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); | ||
| elem.GetAttribute("data-testid").ShouldBe("myTestId"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Should find span element with equivalent case-sensitive testid value")] | ||
| public void Test008() | ||
| { | ||
| var cut = Render<Wrapper>(ps => ps.AddChildContent(""" | ||
| <span data-testid="myTestId"><span> | ||
| <span data-testid="MYTESTID"><span> | ||
| """)); | ||
|
|
||
| var elem = cut.FindByTestId("MYTESTID"); | ||
|
|
||
| elem.ShouldNotBeNull(); | ||
| elem.NodeName.ShouldBe("SPAN", StringCompareShould.IgnoreCase); | ||
| elem.GetAttribute("data-testid").ShouldBe("MYTESTID"); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.