Finding elements in ChildComponents
When trying to find elements in AddChildContent the system will error (first test example). You have to instead look directly in the cut (second test example)
Say you had a parent element (HeadlessDisclosure) that is a div and cascades itself to a child (HeadlessDisclosureButton) which is a button. I want to grab the button element with the Find(cssSelector).
The expected razor
<HeadlessDisclosure>
<HeadlessDisclosureButton />
</HeadlessDisclosure>
would result in
Here is the first test (that fails). The Node of the disclosure has 1 element (the div), but not direct children. Instead, if you look in the Node's content, you will see the button.
var disclosure = RenderComponent<HeadlessDisclosure>(parameters => parameters
.AddChildContent<HeadlessDisclosureButton>()
);
var button = disclosure.Find("button"); // Bunit.ElementNotFoundException : No elements were found that matches the selector 'button'
// do stuff with button
Test that works:
var disclosure = RenderComponent<HeadlessDisclosure>();
var cut = RenderComponent<HeadlessDisclosureButton>(parameters => parameters
.AddCascadingValue(disclosure.Instance)
);
var button = cut.Find("button");
// do stuff with button
Expected behavior:
I want the top test to be able to find elements
Version info:
- bUnit version:
<PackageReference Include="bunit.web" Version="1.2.49" />
- .NET Runtime and Blazor version:
<TargetFramework>net6.0</TargetFramework>
- OS type and version: Windows
Additional Info
Here is a screen grab of the markup of disclosure. So I know it is getting the correct markup.

Finding elements in ChildComponents
When trying to find elements in
AddChildContentthe system will error (first test example). You have to instead look directly in thecut(second test example)Say you had a parent element (
HeadlessDisclosure) that is adivand cascades itself to a child (HeadlessDisclosureButton) which is abutton. I want to grab thebuttonelement with theFind(cssSelector).The expected razor
would result in
Here is the first test (that fails). The Node of the
disclosurehas 1 element (thediv), but not directchildren. Instead, if you look in theNode'scontent, you will see thebutton.Test that works:
Expected behavior:
I want the top test to be able to find elements
Version info:
<PackageReference Include="bunit.web" Version="1.2.49" /><TargetFramework>net6.0</TargetFramework>Additional Info
Here is a screen grab of the markup of

disclosure. So I know it is getting the correct markup.