You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/site/docs/verification/semantic-html-comparison.md
+20-89Lines changed: 20 additions & 89 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,14 @@ uid: semantic-html-comparison
3
3
title: Customizing the Semantic HTML Comparison
4
4
---
5
5
6
-
# Semantic HTML markup comparison
6
+
# Customizing the Semantic HTML Comparison
7
7
8
8
This library includes comparison and assert helpers that uses the [AngleSharp Diffing](https://github.com/AngleSharp/AngleSharp.Diffing/) library to perform semantic HTML comparison.
9
9
10
10
On this page we will go through how the comparison works, and what options you have to affect the comparison process.
11
11
12
-
> **NOTE:** The semantic HTML comparison is available in both C# and Razor tests with the <xref:Bunit.Fixture> component, and is always used in Razor tests with the <xref:Bunit.SnapshotTest> component.
12
+
> [!NOTE]
13
+
> The semantic HTML comparison is available in both C# and Razor tests with the <xref:Bunit.Fixture> component, and is always used in Razor tests with the <xref:Bunit.SnapshotTest> component.
13
14
14
15
## Why semantic comparison is needed for stable tests
15
16
@@ -24,7 +25,7 @@ Just performing string comparison of two strings containing HTML markup can brea
24
25
25
26
The [AngleSharp Diffing](https://github.com/AngleSharp/AngleSharp.Diffing/) library handles all those cases, so your tests are more stable.
26
27
27
-
## Customizing the Semantic HTML Comparison
28
+
## Customizing Options
28
29
29
30
The [AngleSharp Diffing](https://github.com/AngleSharp/AngleSharp.Diffing/) library also allows us to customize the comparison process, by added special attributes to the _"control" markup_, i.e. the expected markup we want to use in verification.
30
31
@@ -58,7 +59,8 @@ There are the customization options you have available to you:
58
59
</header>
59
60
```
60
61
61
-
**NOTE:** The default for `<pre>` and `<script>` elements is the `Preserve` option. To change that, use the `diff:whitespace` attribute, for example:
62
+
> [!NOTE]
63
+
> The default for `<pre>` and `<script>` elements is the `Preserve` option. To change that, use the `diff:whitespace` attribute, for example:
If we want to verify the markup is rendered correctly, and for example use RegEx to verify the `id` attribute (it might be generated) and ignore the `<small>` element, we can do it like this in C# based tests:
// Here we use the HTML diffing library to assert that the rendered HTML
125
-
// from CUT is semantically the same as the expected HTML string above.
126
-
cut.MarkupMatches(expectedHtml);
127
-
}
128
-
```
129
-
130
-
In a Razor based test, the example looks like this:
131
-
132
-
```cshtml
133
-
<Fixture Test="Test1">
134
-
<ComponentUnderTest>
135
-
<Heading />
136
-
</ComponentUnderTest>
137
-
</Fixture>
138
-
@code {
139
-
void Test1(IRazorTestContext context)
140
-
{
141
-
// Arrange - Gets the Heading component
142
-
var cut = context.GetComponentUnderTest<Heading>();
143
-
144
-
// Assert
145
-
// Here we specify expected HTML from CUT.
146
-
var expectedHtml = @"<h3 id:regex=""heading-\d{4}"" required>
147
-
Heading text
148
-
<small diff:ignore></small>
149
-
</h3>";
150
-
151
-
// Here we use the HTML diffing library to assert that the rendered HTML
152
-
// from CUT is semantically the same as the expected HTML string above.
153
-
cut.MarkupMatches(expectedHtml);
154
-
}
155
-
}
156
-
```
157
-
158
-
In a Snapshot test, the example looks like this:
159
-
160
-
```html
161
-
<SnapshotTestDescription="Helpful description of the test case">
162
-
<TestInput>
163
-
<Heading />
164
-
</TestInput>
165
-
<ExpectedOutput>
166
-
<h3id:regex="heading-\d{4}"required>
167
-
Heading text
168
-
<smalldiff:ignore></small>
169
-
</h3>
170
-
</ExpectedOutput>
171
-
</SnapshotTest>
172
-
```
173
-
174
-
## Different ways of getting the differences
175
-
176
-
This section is coming soon. For now, see examples on the [C# test examples](/docs/CSharp-test-examples.html) page where the methods are demonstrated. Look for examples using these methods:
0 commit comments