https://github.com/verifytests/verify.anglesharp
Extends Verify to allow comparison of html files via AngleSharp.
https://github.com/verifytests/verify.anglesharp
Last synced: 11 months ago
JSON representation
Extends Verify to allow comparison of html files via AngleSharp.
- Host: GitHub
- URL: https://github.com/verifytests/verify.anglesharp
- Owner: VerifyTests
- License: mit
- Created: 2020-04-27T00:08:47.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-04-14T11:23:04.000Z (about 1 year ago)
- Last Synced: 2025-04-14T12:29:15.554Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 658 KB
- Stars: 6
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license.txt
- Code of conduct: code_of_conduct.md
Awesome Lists containing this project
README
#
Verify.AngleSharp
[](https://github.com/orgs/VerifyTests/discussions)
[](https://ci.appveyor.com/project/SimonCropp/Verify-AngleSharp)
[](https://www.nuget.org/packages/Verify.AngleSharp/)
Extends [Verify](https://github.com/VerifyTests/Verify) with Html verification utilities via [AngleSharp](https://github.com/AngleSharp/AngleSharp).
**See [Milestones](../../milestones?state=closed) for release notes.**
## Sponsors
### Entity Framework Extensions
[Entity Framework Extensions](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.AngleSharp) is a major sponsor and is proud to contribute to the development this project.
[](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.AngleSharp)
## NuGet
* https://nuget.org/packages/Verify.AngleSharp
## Comparer Usage
Extends [Verify](https://github.com/VerifyTests/Verify) to allow [comparison](https://github.com/VerifyTests/Verify/blob/master/docs/comparer.md) of htm and html files via [AngleSharp](https://github.com/AngleSharp/AngleSharp.Diffing).
### Initialize
Call `VerifyAngleSharpDiffing.Initialize()` once at assembly load time.
Initialize takes an optional `Action` to control settings at a global level:
```cs
[ModuleInitializer]
public static void Init() =>
VerifyAngleSharpDiffing.Initialize();
```
snippet source | anchor
### Verify html
Given an existing verified file:
My First Heading
My first paragraph.
```
snippet source | anchor
And a test:
```cs
[Test]
public Task Sample()
{
var html =
"""
My First Heading
My first paragraph.
""";
return Verify(html, "html");
}
```
snippet source | anchor
Note that the input html differs from the verified html, but not in a semantically significant way. Hence this test will pass.
### Diff results
If the comparison fails, the resulting differences will be included in the test result displayed to the user.
For example if, in the above html, `
My First Heading
` changes to `First Heading
` then the following will be printed in the test results:
```
Comparer result:
* Node Diff
Path: h1(0) > #text(0)
Received: First Heading
Verified: My First Heading
```
### Test level settings
Settings can also be controlled for a specific test.
```cs
var settings = new VerifySettings();
settings.AngleSharpDiffingSettings(
action =>
{
static FilterDecision SpanFilter(
in ComparisonSource source,
FilterDecision decision)
{
if (source.Node.NodeName == "SPAN")
{
return FilterDecision.Exclude;
}
return decision;
}
var options = action.AddDefaultOptions();
options.AddFilter(SpanFilter);
});
```
snippet source | anchor
### Global settings
```cs
VerifyAngleSharpDiffing.Initialize(
action =>
{
static FilterDecision SpanFilter(
in ComparisonSource source,
FilterDecision decision)
{
if (source.Node.NodeName == "SPAN")
{
return FilterDecision.Exclude;
}
return decision;
}
var options = action.AddDefaultOptions();
options.AddFilter(SpanFilter);
});
```
snippet source | anchor
### Verify svg
Given an existing verified file:
SVG
```
snippet source | anchor
And a test:
```cs
[Test]
public Task SvgSample()
{
var svg =
"""
SVG
""";
return Verify(svg, "svg");
}
```
snippet source | anchor
Note that the input svg differs from the verified svg, but not in a semantically significant way. Hence this test will pass.
## Pretty Print
Html can be pretty printed.
```cs
[Test]
public Task PrettyPrintHtml()
{
var html = """
My First Heading
My first paragraph.
""";
return Verify(html, "html")
.PrettyPrintHtml();
}
```
snippet source | anchor
Results in
My First Heading
My first paragraph.
```
snippet source | anchor
To apply this to all `html` files use `HtmlPrettyPrint.All();`
### Manipulate Html
Nodes can be manipulated as part of the pretty print:
```cs
[Test]
public Task PrettyPrintHtmlWithNodeManipulation()
{
var html = """
My First Heading
My first paragraph.
""";
return Verify(html, "html")
.PrettyPrintHtml(
nodes =>
{
foreach (var node in nodes.QuerySelectorAll("h1"))
{
node.Remove();
}
});
}
```
snippet source | anchor
Results in
My first paragraph.
```
snippet source | anchor
## AngleSharp helpers
### ScrubEmptyDivs
```cs
[Test]
public Task ScrubEmptyDivs()
{
var html = """
My First Heading
""";
return Verify(html, "html")
.PrettyPrintHtml(nodes => nodes.ScrubEmptyDivs());
}
```
snippet source | anchor
Results in:
My First Heading
```
snippet source | anchor
### ScrubAttributes
#### Removal
```cs
[Test]
public Task ScrubAttributes()
{
var html = """
My First Heading
""";
return Verify(html, "html")
.PrettyPrintHtml(nodes => nodes.ScrubAttributes("id"));
}
```
snippet source | anchor
Results in:
My First Heading
```
snippet source | anchor
#### Replace Value
```cs
[Test]
public Task ScrubAttributeWithNewValue()
{
var html = """
My First Heading
""";
return Verify(html, "html")
.PrettyPrintHtml(
nodes => nodes.ScrubAttributes(x =>
{
if (x.Name == "id")
{
return "new value";
}
return null;
}));
}
```
snippet source | anchor
Results in:
My First Heading
```
snippet source | anchor
### ScrubAspCacheBusterTagHelper
```cs
[Test]
public Task ScrubAspCacheBusterTagHelper()
{
var html = """
My Heading
""";
return Verify(html, "html")
.PrettyPrintHtml(nodes => nodes.ScrubAspCacheBusterTagHelper());
}
```
snippet source | anchor
Results in:
My Heading
```
snippet source | anchor
### ScrubBrowserLink
```cs
[Test]
public Task ScrubBrowserLink()
{
var html = """
My Heading
""";
return Verify(html, "html")
.PrettyPrintHtml(nodes => nodes.ScrubBrowserLink());
}
```
snippet source | anchor
Results in:
My Heading
```
snippet source | anchor