An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Verify.AngleSharp

[![Discussions](https://img.shields.io/badge/Verify-Discussions-yellow?svg=true&label=)](https://github.com/orgs/VerifyTests/discussions)
[![Build status](https://ci.appveyor.com/api/projects/status/ff4ms9mevndkui7l?svg=true)](https://ci.appveyor.com/project/SimonCropp/Verify-AngleSharp)
[![NuGet Status](https://img.shields.io/nuget/v/Verify.AngleSharp.svg)](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.

[![Entity Framework Extensions](https://raw.githubusercontent.com/VerifyTests/Verify.AngleSharp/refs/heads/main/docs/zzz.png)](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:


```html

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




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


```html



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


```html



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:


```html



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:


```html



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:


```html



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:


```html





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:


```html





My Heading


```
snippet source | anchor