-
Hello, I'm using Bunit to test some pages and before it has worked great but for some reason I'm running into this issue. I create a TestContext and render my page. If I put find th in its own variable I can see it contains "<th>Title</th>". My test line is |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
Uhm that's weird. What version of bUnit are you using and what version dotnet? |
Beta Was this translation helpful? Give feedback.
-
It should definitely not do that. Can you try to change the markup such that it is a |
Beta Was this translation helpful? Give feedback.
-
It looks like the root of this problem is with AngelSharp, at least the way bUnit is using it under the hood. AngleSharp throws away the table related elements away and just keeps text content. The following test fail: using Xunit;
using AngleSharp;
using AngleSharp.Html.Parser;
using System.Threading.Tasks;
namespace AngleSharpTableTagParsing
{
public class UnitTest1
{
[Theory]
[InlineData("h1")] // passes
[InlineData("p")] // passes
[InlineData("table")] // fails
[InlineData("thead")] // fails
[InlineData("tbody")] // fails
[InlineData("th")] // fails
[InlineData("td")] // fails
[InlineData("tr")] // fails
public async Task ParsingTagsCorrectly(string tag)
{
var config = Configuration.Default.WithCss();
var context = BrowsingContext.New(config);
var parser = context.GetService<IHtmlParser>();
var document = await context.OpenNewAsync();
var markup = $"<{tag}>Title</{tag}>";
var nodes = parser.ParseFragment(markup, document.Body);
Assert.Equal(AngleSharp.Dom.NodeType.Element, nodes[0].NodeType);
}
}
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AngleSharp" Version="0.14.0" />
<PackageReference Include="AngleSharp.Css" Version="0.14.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project> Am I using AngleSharp wrong in this case, @FlorianRappl? |
Beta Was this translation helpful? Give feedback.
-
Thanks. This is expected by AngleSharp as the W3C HTML5 spec parses based on context. Without a context (i.e., just using A source code like You can test these things easily, e.g., in https://www.w3schools.com/html/tryit.asp?filename=tryhtml_basic <!DOCTYPE html>
<html>
<body>
<script>
document.body.innerHTML = '<table><td>hello</td></table>';
alert(document.body.innerHTML);
</script>
</body>
</html> vs <!DOCTYPE html>
<html>
<body>
<script>
document.body.innerHTML = '<td>hello</td>';
alert(document.body.innerHTML);
</script>
</body>
</html> vs <!DOCTYPE html>
<html>
<body>
<script>
document.body.innerHTML = ''; // just for nicer output
var table = document.body.appendChild(document.createElement('table'));
table.innerHTML = '<td>hello</td>';
alert(document.body.innerHTML);
</script>
</body>
</html> So you'd essentially need to either detect or allow setting the context. If somebody wants to have just "td" then it must be set to a table context. From the perspective of AngleSharp / the parser everything is "as expected". Now what that means for bUnit we still need to figure out. To be concluded. |
Beta Was this translation helpful? Give feedback.
-
Thanks @FlorianRappl for helping clearing this up. This is sort of a "works as unexpected" 🤷♂️😉 I will add an issue to see if I can get a good solution for this for the special cases around TR, TD, TH, TBODY and THEAD. There might be other elements that the HTML parser does not expect in the BODY context. So I will have to investigate further. But for now, @brychart, you will have to assert a different way than using MarkupMatches. Maybe use |
Beta Was this translation helpful? Give feedback.
Thanks @FlorianRappl for helping clearing this up. This is sort of a "works as unexpected" 🤷♂️😉
I will add an issue to see if I can get a good solution for this for the special cases around TR, TD, TH, TBODY and THEAD. There might be other elements that the HTML parser does not expect in the BODY context. So I will have to investigate further.
But for now, @brychart, you will have to assert a different way than using MarkupMatches. Maybe use
cut.Find("th").TextContent.Should.Be("Title")
.